<?php
/**
 * The plugin info module.
 *
 * @package northmill-online-shared
 *
 * @phpcs:disable Squiz.Commenting.FunctionComment.MissingParamTag
 */

declare(strict_types=1);

namespace Northmill_Online_Shared\Modules\Info;

use Northmill_Online_Shared\Abstract_Shared_Base;
use Northmill_Online_Shared\Modules\Info\Models\Info_Paths_Model;

defined( 'ABSPATH' ) || exit;

/**
 * The plugin info module.
 */
class Plugin_Info_Module extends Abstract_Package_Info_Module {

	/**
	 * Plugin basename.
	 *
	 * @var string
	 */
	public string $basename;

	/**
	 * {@inheritDoc}
	 */
	public function __construct( Abstract_Shared_Base $shared ) {
		parent::__construct(
			$shared
		);

		$this->basename = plugin_basename( $this->get_file_path() );
	}

	/**
	 * {@inheritDoc}
	 */
	protected function create_paths_model( string $package_file_path ): Info_Paths_Model {
		return new Info_Paths_Model(
			plugin_dir_url( $package_file_path ),
			plugin_dir_path( $package_file_path )
		);
	}

	/**
	 * {@inheritDoc}
	 */
	public function get_slug(): string {
		if ( ! isset( $this->slug ) ) {
			$this->slug = dirname( plugin_basename( $this->get_file_path() ) );
		}

		return $this->slug;
	}


	/**
	 * {@inheritDoc}
	 */
	public function is_plugin(): bool {
		return true;
	}

	/**
	 * {@inheritDoc}
	 */
	public static function get_package_data( string $file_path, bool $should_translate = false ): array {
		if ( ! function_exists( 'get_plugin_data' ) ) {
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
		}

		return get_plugin_data( $file_path, true, $should_translate );
	}

	/**
	 * {@inheritDoc}
	 */
	protected static function get_package_name(): string {
		$plugin_data = self::get_package_data( Abstract_Shared_Base::base_instance()->get_package_file_path() );
		return $plugin_data['Name'];
	}
}
