'use strict';

var index = require('./index-Bc25FxCt.js');
var component = require('./component-f6SRjQa4.js');
var componentUsageRuntimeMetrics = require('./component-usage-runtime-metrics-CTEgPajK.js');
var designToken = require('./design-token-C_KFCGA0.js');
require('./responsive-format-facade-HA8sO-0_.js');
require('./array-CyqWaFqX.js');
require('./state-border-formatter-B6OKYNVo.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 index.VegaSlimmer {
    constructor() {
        super();
    }
    render() {
        const icon = index.VegaInternalIconManager.getIconKey(this.country);
        const hasIcon = index.VegaIconManager.hasIcon(icon);
        const height = designToken.formatAndValidResponsiveToken(this.size, 'widthAndHeight');
        const width = `calc(${height} * 4 / 3)`;
        if (!hasIcon)
            return (index.h("vega-skeleton", { type: "image", corners: "rounded-2", height: height, width: width }));
        return index.h("vega-icon", { icon: icon, size: this.size, style: { '--vega-icon-width': width } });
    }
}
__decorate$2([
    index.MapToComponentField()
], VegaFlagIconRenderer.prototype, "size", void 0);
__decorate$2([
    index.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 Promise.resolve().then(function () { return require('./country-flags-DI9mOSXC.js'); })).CountryFlags;
        index.VegaInternalIconManager.register(countryFlags);
        index.ChangeManager.notify(index.FLAG_ICON_LOADED, {});
    }
    catch (err) {
        /* istanbul ignore next */
        index.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 index.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 = () => {
            index.forceUpdate(this.host);
        };
    }
    /**
     * componentWillLoad
     */
    componentWillLoad() {
        if (!this.country)
            return;
        const hasIcon = this.isFlagForCountryCodeLoaded(this.country);
        if (!hasIcon)
            this.registerObserver();
    }
    /**
     * disconnectedCallback
     */
    unRegisterObserver() {
        if (this.observer) {
            index.ChangeManager.unregister(index.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 index.Observer(this.isAcceptObserver.bind(this), this.listenerCallback.bind(this));
        index.ChangeManager.register(index.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 = index.VegaInternalIconManager.getIconKey(countryCode);
        return index.VegaInternalIconManager.hasIcon(icon);
    }
}
(() => {
    void dynamicLoadTheCountryFlags();
})();
__decorate$1([
    index.MapToComponentField()
], VegaFlagIconController.prototype, "host", void 0);
__decorate$1([
    index.MapToComponentField()
], VegaFlagIconController.prototype, "country", void 0);
__decorate$1([
    index.MapToComponentMethod('componentWillLoad')
], VegaFlagIconController.prototype, "componentWillLoad", null);
__decorate$1([
    index.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) {
        index.registerInstance(this, hostRef);
        this.globalSlimmers = {};
        this.renderer = new VegaFlagIconRenderer();
        this.vegaFlagIconController = new VegaFlagIconController();
        this.vegaComponentUsageRuntimeMetricsSlimmer = new componentUsageRuntimeMetrics.VegaComponentUsageRuntimeMetricsSlimmer();
        /**
         * Specifies the width and height of the icon.
         *
         * @vegaVersion 2.46.0
         */
        this.size = 'size-16';
    }
    render() {
        return component.sanitizeVegaComponent(index.h(index.Host, null, this.renderer.render()), this.host);
    }
    get host() { return index.getElement(this); }
};
__decorate([
    componentUsageRuntimeMetrics.InjectVegaGlobalSlimmer()
], VegaFlagIcon.prototype, "globalSlimmers", void 0);
__decorate([
    index.InjectVegaSlimmer()
], VegaFlagIcon.prototype, "renderer", void 0);
__decorate([
    index.InjectVegaSlimmer()
], VegaFlagIcon.prototype, "vegaFlagIconController", void 0);
__decorate([
    index.InjectVegaSlimmer()
], VegaFlagIcon.prototype, "vegaComponentUsageRuntimeMetricsSlimmer", void 0);
VegaFlagIcon.style = vegaFlagIconCss();

exports.vega_flag_icon = VegaFlagIcon;
