<?php
/**
 * Livewire Component
 *
 * @version     1.0.$Revision:$
 * @version     SVN: $Id:$
 * @package     bplan-base/globals
 * @subpackage  LivewireComponents
 * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 * @copyright   Copyright (C) 2025, 2026 bplan-solutions GmbH & Co. KG <https://www.bplan-solutions.de/>
 * /Δ\
 */

namespace BplanBase\Globals\Livewire\Global\Modals;


use BplanBase\Globals\Enums\LanguageContext;
use BplanBase\Globals\Repositories\LanguageRepository;
use Illuminate\Database\Eloquent\Collection;
use Livewire\Attributes\On;
use Livewire\Component;


/**
 * Livewire Component
 *
 * @version     1.1.0 / 2026-04-24
 * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 */
class MultiLanguageJson extends Component
{


/* +++ TRAITS +++ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */


/* +++ CLASS CONSTANTS +++ ++++++++++++++++++++++++++++++++++++++++++++++++++ */


/* +++ OBJECT MEMBERS +++ +++++++++++++++++++++++++++++++++++++++++++++++++++ */


    /**
     *
     */
    public array $labels = [];


    /**
     * @var     LanguageContext
     */
    public string|LanguageContext $languageContext = LanguageContext::Both;


    /**
     *
     */
    public ?Collection $languages;


    /**
     *
     */
    public bool $multiline = false;


    /**
     *
     */
    public ?string $target;


    /**
     *
     */
    public string $windowTitle = 'Labels';


/* +++ CLASS MEMBERS +++ ++++++++++++++++++++++++++++++++++++++++++++++++++++ */


/* +++ OBJECT METHODS +++ +++++++++++++++++++++++++++++++++++++++++++++++++++ */


    /**
     *
     * @version     1.0.0 / 2025-04-18
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     */
    public function createMultiLanguageJson()
    {
        $this->validate();

        $this->dispatch('multi-language-json-created', json: $this->labels, target: $this->target);

    } // createMultiLanguageJson()


    /**
     *
     * @version     1.0.0 / 2025-04-18
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     */
    #[On('show-multi-language-json-modal')]
    public function init(?array $labels, string $target, string $windowTitle = 'Labels')
    {
        if (isset($labels) && !empty($labels)) {
            $this->labels = $labels;
        }
        $this->target = $target;
        $this->windowTitle = $windowTitle;

    } // init()


    /**
     *
     * @version     1.1.0 / 2026-03-12
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     */
    public function mount()
    {
        $this->languages = LanguageRepository::getByContext($this->languageContext);

    } // mount()


    /**
     *
     * @version     1.0.0 / 2025-04-18
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     */
    public function render()
    {
        return view('globals::livewire.global.modals.multi-language-json');

    } // render()


    /**
     *
     * @version     1.0.0 / 2025-04-18
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     */
    public function resetModal()
    {
        $this->labels = [];

    } // resetModal()


    /**
     *
     * @version     1.1.0 / 2026-03-12
     * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
     */
    public function rules()
    {
        $rules = [];

        $locale = config('app.locale');
        $fallbackLocale = config('app.fallback_locale');

        $rules["labels.{$locale}"] = 'required';
        $rules["labels.{$fallbackLocale}"] = 'required';

        return $rules;
    }


/* +++ CLASS METHODS +++ ++++++++++++++++++++++++++++++++++++++++++++++++++++ */


} // class MultiLanguageJson extends Component {}
