<?php

/**
 * Internationalization setup.
 *
 * @package northmill-online-shared
 *
 * @phpcs:disable PEAR.NamingConventions.ValidClassName.StartWithCapital
 */
declare (strict_types=1);
namespace Northmill_Online_Shared_Sync\Northmill_Online_Shared\Modules;

use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Modules\i18n\Models\Abstract_Translation_Model;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Modules\i18n\Models\Plugin_Translation_Model;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Modules\i18n\Models\Theme_Translation_Model;
defined('ABSPATH') || exit;
/**
 * Abstract i18n module.
 */
class i18n_Module extends Abstract_Module
{
    /**
     * An array to store translations to be set up.
     *
     * @var Abstract_Translation_Model[] $translations
     */
    protected array $translations = array();
    /**
     * Set up module.
     */
    public function setup(): void
    {
        $this->setup_shared_translations();
        $this->setup_package_translations();
    }
    /**
     * Set up the internal translations.
     *
     * @return void
     */
    protected function setup_shared_translations(): void
    {
        $this->add_translation(array('textdomain' => 'northmill-online-shared', 'path' => $this->shared->package_info->paths->get_shared_rel_path() . 'languages', 'mofile_prefix' => 'northmill-online-shared'));
    }
    /**
     * Set up the internal translations.
     *
     * @return void
     */
    protected function setup_package_translations(): void
    {
        $package_translations = $this->shared->get_translations();
        if (!empty($package_translations)) {
            foreach ($package_translations as $translation) {
                $this->add_translation($translation);
            }
        }
    }
    /**
     * Adds a translation to the translation array.
     *
     * @param array|string $translation The translation to be added.
     *        Either:
     *            Associative array:
     *                Key 'textdomain' (required) representing the text domain.
     *                Key 'path' (optional, defaults from class constant) representing the translation file path, relative to package root.
     *            String:
     *                Text domain. Path defaults from class constant.
     *
     * @return void
     */
    public function add_translation(array|string $translation): void
    {
        $translation = $this->shared->package_info->is_plugin() ? new Plugin_Translation_Model($translation) : new Theme_Translation_Model($translation);
        $this->translations[$translation->get_textdomain()] = $translation;
    }
    /**
     * Get the translations.
     *
     * @return Abstract_Translation_Model[]
     */
    public function get_translations(): array
    {
        return $this->translations;
    }
}
