<?php

/**
 * Paths model for the info module.
 *
 * @package northmill-online-shared
 */
declare (strict_types=1);
namespace Northmill_Online_Shared_Sync\Northmill_Online_Shared\Modules\Info\Models;

use InvalidArgumentException;
defined('ABSPATH') || exit;
/**
 * Class Info_Paths_Model
 */
class Info_Package_Data_Model
{
    /**
     * The package version.
     *
     * @var string
     */
    private string $version;
    /**
     * The package name.
     *
     * @var string
     */
    private string $name;
    /**
     * The package author name.
     *
     * @var string
     */
    private string $author_name;
    /**
     * The package author uri.
     *
     * @var string
     */
    private string $author_uri;
    /**
     * Whether the plugin info has been translated.
     *
     * @var bool
     */
    private bool $is_info_translated = \false;
    /**
     * Info_Paths_Model constructor.
     *
     * @param string $file_path The path to the package file.
     * @param array  $get_package_data_callable The callable that retrieves the package data.
     *
     * @throws InvalidArgumentException If the get_package_data_callable is not callable.
     */
    public function __construct(private readonly string $file_path, private readonly array $get_package_data_callable)
    {
        if (!is_callable($get_package_data_callable)) {
            throw new InvalidArgumentException('The get_package_data_callable must be a callable.');
        }
        $this->update_data();
    }
    /**
     * Updates the package data.
     *
     * @param bool $did_init Whether the 'init' action has fired.
     *
     * @return void
     */
    private function update_data(bool $did_init = \false): void
    {
        $data = ($this->get_package_data_callable)($this->file_path, $did_init);
        $this->version = $data['Version'] ?? '';
        $this->name = $data['Name'] ?? '';
        $this->author_name = $data['Author'] ?? '';
        $this->author_uri = $data['AuthorURI'] ?? '';
        if ($did_init) {
            $this->is_info_translated = \true;
        }
    }
    /**
     * Update the package data if the 'init' action has fired and the data has not already been translated.
     *
     * @return void
     */
    private function maybe_update_data(): void
    {
        if ($this->is_info_translated) {
            return;
        }
        if (did_action('init')) {
            $this->update_data(\true);
        }
    }
    /**
     * Get the package data name.
     *
     * @return string
     */
    public function get_name(): string
    {
        $this->maybe_update_data();
        return $this->name;
    }
    /**
     * Get the package data version.
     *
     * @return string
     */
    public function get_version(): string
    {
        $this->maybe_update_data();
        return $this->version;
    }
    /**
     * Get the package data author name.
     *
     * @return string
     */
    public function get_author_name(): string
    {
        $this->maybe_update_data();
        return $this->author_name;
    }
    /**
     * Get the package data author uri.
     *
     * @return string
     */
    public function get_author_uri(): string
    {
        $this->maybe_update_data();
        return $this->author_uri;
    }
}
