import { aD as MapToComponentField, aF as VegaSlimmer, bU as VegaInternalIconManager, G as VegaIconManager, b1 as h, aE as MapToComponentMethod, aH as ChangeManager, c2 as FLAG_ICON_LOADED, L as LogUtility, c0 as forceUpdate, aG as Observer, a$ as InjectVegaSlimmer, b0 as registerInstance, b2 as Host, b3 as getElement } from './index-BFV_-jmr.js';
import { s as sanitizeVegaComponent } from './component-Wf7aOiyT.js';
import { I as InjectVegaGlobalSlimmer, V as VegaComponentUsageRuntimeMetricsSlimmer } from './component-usage-runtime-metrics-CR3WLFrY.js';
import { f as formatAndValidResponsiveToken } from './design-token-Bpdc0Cm5.js';
import './responsive-format-facade-D07GU8Pt.js';
import './array-BJeX2G5G.js';
import './state-border-formatter-CpVvbIEm.js';

var __decorate$2 = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
        r = Reflect.decorate(decorators, target, key, desc);
    else
        for (var i = decorators.length - 1; i >= 0; i--)
            if (d = decorators[i])
                r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
class VegaFlagIconRenderer extends VegaSlimmer {
    constructor() {
        super();
    }
    render() {
        const icon = VegaInternalIconManager.getIconKey(this.country);
        const hasIcon = VegaIconManager.hasIcon(icon);
        const height = formatAndValidResponsiveToken(this.size, 'widthAndHeight');
        const width = `calc(${height} * 4 / 3)`;
        if (!hasIcon)
            return (h("vega-skeleton", { type: "image", corners: "rounded-2", height: height, width: width }));
        return h("vega-icon", { icon: icon, size: this.size, style: { '--vega-icon-width': width } });
    }
}
__decorate$2([
    MapToComponentField()
], VegaFlagIconRenderer.prototype, "size", void 0);
__decorate$2([
    MapToComponentField()
], VegaFlagIconRenderer.prototype, "country", void 0);

var __decorate$1 = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
        r = Reflect.decorate(decorators, target, key, desc);
    else
        for (var i = decorators.length - 1; i >= 0; i--)
            if (d = decorators[i])
                r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
/**
 * The function `dynamicLoadTheCountryFlags` will dynamic import and register flags of all countries at once
 */
async function dynamicLoadTheCountryFlags() {
    try {
        const countryFlags = (await import('./country-flags-SnxvXg-z.js')).CountryFlags;
        VegaInternalIconManager.register(countryFlags);
        ChangeManager.notify(FLAG_ICON_LOADED, {});
    }
    catch (err) {
        /* istanbul ignore next */
        LogUtility.error(`Error loading flag module:`, err);
    }
}
/**
 * The `VegaFlagIconController` class in TypeScript manage country flag and
 * country codes, dynamic import the flags of all countries and get the country icon by specific code.
 */
class VegaFlagIconController extends VegaSlimmer {
    constructor() {
        super();
        /**
         * Determines whether the observer should accept changes.
         *
         * @returns {boolean} Always returns true in the current implementation.
         */
        this.isAcceptObserver = () => {
            return true;
        };
        /**
         * This function listens whether the logo icon is imported asynchronously.
         */
        this.listenerCallback = () => {
            forceUpdate(this.host);
        };
    }
    /**
     * componentWillLoad
     */
    componentWillLoad() {
        if (!this.country)
            return;
        const hasIcon = this.isFlagForCountryCodeLoaded(this.country);
        if (!hasIcon)
            this.registerObserver();
    }
    /**
     * disconnectedCallback
     */
    unRegisterObserver() {
        if (this.observer) {
            ChangeManager.unregister(FLAG_ICON_LOADED, this.observer);
            this.observer = null;
        }
    }
    /**
     * The function creates an observer to observe the subject of FLAG_ICON_LOADED.
     */
    registerObserver() {
        this.observer = new Observer(this.isAcceptObserver.bind(this), this.listenerCallback.bind(this));
        ChangeManager.register(FLAG_ICON_LOADED, this.observer);
    }
    /**
     * Checks if the country icon exists by country code.
     *
     * @param {VegaCountryCode} countryCode - The `country` parameter is of type `VegaCountryCode`, which is likely a
     * custom data type representing a country code.
     * @returns {boolean} return the icon is exist or not.
     */
    isFlagForCountryCodeLoaded(countryCode) {
        const icon = VegaInternalIconManager.getIconKey(countryCode);
        return VegaInternalIconManager.hasIcon(icon);
    }
}
(() => {
    void dynamicLoadTheCountryFlags();
})();
__decorate$1([
    MapToComponentField()
], VegaFlagIconController.prototype, "host", void 0);
__decorate$1([
    MapToComponentField()
], VegaFlagIconController.prototype, "country", void 0);
__decorate$1([
    MapToComponentMethod('componentWillLoad')
], VegaFlagIconController.prototype, "componentWillLoad", null);
__decorate$1([
    MapToComponentMethod('disconnectedCallback')
], VegaFlagIconController.prototype, "unRegisterObserver", null);

const vegaFlagIconCss = () => `:host{display:inline-block}:host vega-icon,:host vega-skeleton,:host svg{display:block;border-radius:2px}:host vega-icon{width:var(--vega-icon-width, 24px)}`;

var __decorate = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
        r = Reflect.decorate(decorators, target, key, desc);
    else
        for (var i = decorators.length - 1; i >= 0; i--)
            if (d = decorators[i])
                r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
const VegaFlagIcon = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        this.globalSlimmers = {};
        this.renderer = new VegaFlagIconRenderer();
        this.vegaFlagIconController = new VegaFlagIconController();
        this.vegaComponentUsageRuntimeMetricsSlimmer = new VegaComponentUsageRuntimeMetricsSlimmer();
        /**
         * Specifies the width and height of the icon.
         *
         * @vegaVersion 2.46.0
         */
        this.size = 'size-16';
    }
    render() {
        return sanitizeVegaComponent(h(Host, null, this.renderer.render()), this.host);
    }
    get host() { return getElement(this); }
};
__decorate([
    InjectVegaGlobalSlimmer()
], VegaFlagIcon.prototype, "globalSlimmers", void 0);
__decorate([
    InjectVegaSlimmer()
], VegaFlagIcon.prototype, "renderer", void 0);
__decorate([
    InjectVegaSlimmer()
], VegaFlagIcon.prototype, "vegaFlagIconController", void 0);
__decorate([
    InjectVegaSlimmer()
], VegaFlagIcon.prototype, "vegaComponentUsageRuntimeMetricsSlimmer", void 0);
VegaFlagIcon.style = vegaFlagIconCss();

export { VegaFlagIcon as vega_flag_icon };
