/*!
 * Copyright 2024-2026 Simply.IN Sp. z o.o.
 *
 * NOTICE OF LICENSE
 * Licensed under the EUPL-1.2 or later.
 * You may not use this work except in compliance with the Licence.
 * Copy of the Licence is available at: https://joinup.ec.europa.eu/software/page/eupl
 * It is bundled with this package in the file LICENSE.txt
 *
 * @author    Simply.IN Sp. z o.o.
 * @copyright 2024-2026 Simply.IN Sp. z o.o.
 * @license   https://joinup.ec.europa.eu/software/page/eupl
 */

const $ = window.jQuery;

$(document).ready(function () {
    $('#SIMPLYIN_DPD_CARRIER_ID, #SIMPLYIN_DHL_CARRIER_ID, #SIMPLYIN_FEDEX_CARRIER_ID, #SIMPLYIN_INPOST_CARRIER_ID, #SIMPLYIN_ORLEN_CARRIER_ID, #SIMPLYIN_UPS_CARRIER_ID, #SIMPLYIN_DEFAULT_CARRIER_ID')
        .select2({ width: '300px' });

    // Secret key show/hide. The field is a native password input and is empty by
    // default (a blank submit keeps the current key), so we only toggle its type.
    const $key = $('#SIMPLYIN_SECRET_KEY');
    $('.simplyin-toggle').on('click', function () {
        const reveal = $key.attr('type') === 'password';
        $key.attr('type', reveal ? 'text' : 'password');
        $(this).find('.material-icons').text(reveal ? 'visibility_off' : 'visibility');
    });

    // Color pickers: keep the native swatch and the hex text field in sync.
    $('.simplyin-color').each(function () {
        const $swatch = $(this).find('.simplyin-color-swatch');
        const $hex = $(this).find('.simplyin-color-hex');
        if (!$swatch.length || !$hex.length) return;

        $swatch.on('input change', function () {
            $hex.val($swatch.val().toUpperCase());
        });
        $hex.on('input change', function () {
            let value = $hex.val().trim();
            if (/^#?[0-9a-fA-F]{6}$/.test(value)) {
                if (value.charAt(0) !== '#') value = '#' + value;
                $swatch.val(value);
            }
        });
    });
});
