/*
 * @package    ECLCDEK shipping plugin СДЭК to RadicalMart
 * @subpackage    ECLCDEK
 * @version    1.0.1
 * @author Econsult Lab.
 * @copyright  Copyright (c) 2025 Econsult Lab. All rights reserved.
 * @license    GNU/GPL license: https://www.gnu.org/copyleft/gpl.html
 * @link       https://econsultlab.ru
 */

/**
 * Класс настроек метода доставки в админке
 * @since 1.0.0
 */
class ECLCDEKSettings extends ECLCDEK {
    /**
     * Параметры инициализации
     * @type {object}
     * @since 1.0.0
     */
    #params;

    constructor(params) {
        const _defaultParams = {
            common: {
                core: {
                    debug: true,
                    ajaxLoader: null,
                    disabled_class: "uk-disabled",
                },
                city_field_id: "jform_params_from_city",
                country_field_id: "jform_params_from_country",
                region_field_id: "jform_params_from_region",
                calculationParams: {},
                task: "settings",
                client: "admin",
                deliveryType: "office",
                result: "",
                widgetParams: null,
            }
        }
        params ??= _defaultParams;
        let _loader = document.body.getElementsByTagName('joomla-core-loader');
        if (_loader.length === 0) {
            _loader = document.body.appendChild(document.createElement('joomla-core-loader'));
            params.common.core.ajaxLoader = _loader;
        } else {
            params.common.core.ajaxLoader = _loader[0];
        }
        super(params.common);
        this.#params = params;
        this.className = "ECLCDEKSettings";
        this.debug('constructor', "params", params);
        let _addr = "";
        let _msg = {
            type: "",
            text: "",
            formatted: ""
        };
        try {
            _addr = JSON.parse(this.results_block.result_input.value);
        } catch (err) {
            _addr = "";
        }

        if (!_addr) {
            _msg = {
                type: "warning",
                text: Joomla.Text._("PLG_RADICALMART_SHIPPING_ECLCDEK_SETTINGS_MESSAGE_NEED_ADDRESS"),
                formatted: ""
            };
            this.initializeMap();
        }
        let _result = this.getBaseResult();
        _result.disabled_btns.map_btn = !!_addr;
        _result.disabled_btns.reset_btn = !_addr;
        _result.output_params.result = _addr;
        _result.message = _msg;

        this.current_results = _result;

        this.getResultFormatted(_result);
    }

    /**
     * @inheritDoc
     *
     * @since 1.0.0
     */
    preProcessAddress(address) {
        let _result = super.preProcessAddress(address);
        _result.client = this.#params.common.client;
        _result.task = this.#params.common.task;
        return _result;
    }

    /**
     * @inheritDoc
     * @since 1.0.0
     */
    onReadyMap(_this) {
        super.onReadyMap(_this);
        let _result = this.getBaseResult();
        _result.disabled_btns = {
            map_btn: false,
            reset_btn: true,
        };
        _result.message = {
            type: "primary",
            text: Joomla.Text._("PLG_RADICALMART_SHIPPING_ECLCDEK_SETTINGS_MESSAGE_READY_TO_CHOSE_ADDRESS"),
            formatted: ""
        };
        this.successCallBack = this.updateResult;
        this.errorCallBack = this.showError;
        this.getResultFormatted(_result);
    }

    /**
     * @inheritDoc
     *
     * @returns {Result}
     *
     * @since 1.0.0
     */
    getBaseResult() {
        return super.getBaseResult();
    }
}