<?php
declare(strict_types=1);

namespace WP_AUX\Services;

use WHOOP\Entities\Elements\A;
use WP_AUX\Entities\AuxFramework\Partials\IconEntity;
use WP_AUX\Enum\SharePlatformsEnum;

class ShareService
{
  public static function getShareUrl(SharePlatformsEnum $platform, string $shareUrl, ?string $shareTitle = null): string
  {
    $url = strtr($platform->shareUrlFormat(), [
      '{url}' => urlencode($shareUrl),
      '{title}' => urlencode($shareTitle),
    ]);

    return $url;
  }

  public static function getShareLink(SharePlatformsEnum $platform, string $shareUrl, ?string $shareTitle = null): A
  {
    $link = A::create(null, [
      'href' => self::getShareUrl($platform, $shareUrl, $shareTitle),
      'aria-label' => $platform->shareLabel()
    ]);

    $icon = IconEntity::create($platform->icon(), false);

    $link->appendElement($icon);

    return $link;
  }
}
