<?php

namespace Dev4Press\Plugin\coreSocial\Networks;

use Dev4Press\Plugin\coreSocial\Base\Network;
use Dev4Press\Plugin\coreSocial\Sharing\Post;
use Dev4Press\Plugin\coreSocial\Sharing\Term;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * @property string   $account
 * @property string[] $hashtags
 */
class Bluesky extends Network {
	protected string $network = 'bluesky';

	public function __construct() {
		parent::__construct();
	}

	public static function instance() : Bluesky {
		static $instance = null;

		if ( ! isset( $instance ) ) {
			$instance = new Bluesky();
		}

		return $instance;
	}

	public function get_share_link( string $url, string $title, string $image_url = '', string $item = 'post', int $item_id = 0 ) : string {
		$title = preg_replace( '/(?<!&)#/', ' ', $title );

		/**
		 * Filters the Bluesky account to include with the share block URL.
		 *
		 * @param string $account Account name, default is from plugin settings.
		 * @param int    $item_id ID of the item that will be shared.
		 */
		$account = apply_filters( 'coresocial_network_bluesky_link_account', $this->get_account( $item, $item_id ), $item_id );

		/**
		 * Filters the Bluesky hashtags to include with the share block URL.
		 *
		 * @param array $hashtags List of hashtags, default are from plugin settings.
		 * @param int   $item_id  ID of the item that will be shared.
		 */
		$hashtags = apply_filters( 'coresocial_network_bluesky_link_hashtags', $this->get_hashtags( $item, $item_id ), $item_id );

		$tags = array();
		$text = array(
			$title,
			$url,
		);

		if ( ! empty( $account ) ) {
			$text[] = 'via @' . $account;
		}

		if ( ! empty( $hashtags ) ) {
			foreach ( $hashtags as $hashtag ) {
				$tags[] = '#' . $hashtag;
			}

			$text[] = PHP_EOL . join( ' ', $tags );
		}

		return sprintf( 'https://bsky.app/intent/compose?text=%1$s', urlencode( join( ' ', $text ) ) );
	}

	private function get_account( string $item = 'post', int $item_id = 0 ) : string {
		$global = $this->account;
		$local  = '';

		if ( $item == 'post' && $item_id > 0 ) {
			$local = Post::instance( $item_id )->bluesky_account;
		} else if ( $item == 'term' && $item_id > 0 ) {
			$local = Term::instance( $item_id )->bluesky_account;
		}

		if ( ! empty( $local ) ) {
			$global = trim( $local );
		}

		return trim( $global, '@' );
	}

	private function get_hashtags( string $item = 'post', int $item_id = 0 ) : array {
		$global = $this->hashtags;
		$local  = '';
		$extra  = '';

		if ( $item == 'post' && $item_id > 0 ) {
			$local = Post::instance( $item_id )->bluesky_hashtags;
			$extra = Post::instance( $item_id )->bluesky_hashtags_list;
		} else if ( $item == 'term' && $item_id > 0 ) {
			$local = Term::instance( $item_id )->bluesky_hashtags;
			$extra = Term::instance( $item_id )->bluesky_hashtags_list;
		}

		if ( $local === 'local' ) {
			$global = array();
		}

		if ( ! empty( $extra ) ) {
			$extra = explode( ',', $extra );
			$extra = array_map( 'trim', $extra );

			$global = array_merge( $global, $extra );
		}

		$global = array_unique( $global );

		return array_filter( $global );
	}
}
