'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-58ea899e.js');
const component = require('./component-1e352960.js');
const globalSlimmerRegistry = require('./global-slimmer-registry-b3bce7e0.js');
const componentUsageRuntimeMetrics = require('./component-usage-runtime-metrics-389d1548.js');
const internalIconManager = require('./internal-icon-manager-66c48800.js');
const iconManager = require('./icon-manager-e855446d.js');
const designToken = require('./design-token-3bc36220.js');
const staticSubjectTitle = require('./static-subject-title-5829a298.js');
const observer = require('./observer-f49483e8.js');
const changeManager = require('./change-manager-a297e4d2.js');
require('./create-public-api-runtime-metrics-slimmer-122350c3.js');
require('./dynamic-slimmer-f31fdfd7.js');
require('./misc-3d30df91.js');
require('./type-guard-c1195e50.js');
require('./breakpoints-ebde4837.js');
require('./ui-32120990.js');
require('./typography-4560e7f1.js');
require('./internal-translation-controller-f0da24b2.js');
require('./responsive-format-facade-9647775d.js');
require('./breakpoints-1cc2c4c6.js');
require('./number-4816603d.js');
require('./pixel-98f2a10c.js');
require('./array-249289f2.js');
require('./feature-flag-controller-5fa21654.js');
require('./index-3ca118c4.js');
require('./state-border-formatter-1a483a9b.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 globalSlimmerRegistry.VegaSlimmer {
  render() {
    const icon = internalIconManager.VegaInternalIconManager.getIconKey(this.country);
    const hasIcon = iconManager.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([
  globalSlimmerRegistry.MapToComponentField()
], VegaFlagIconRenderer.prototype, "size", void 0);
__decorate$2([
  globalSlimmerRegistry.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-d4928e55.js'); })).CountryFlags;
    internalIconManager.VegaInternalIconManager.register(countryFlags);
    changeManager.ChangeManager.notify(staticSubjectTitle.FLAG_ICON_LOADED, {});
  }
  catch (err) {
    /* istanbul ignore next */
    globalSlimmerRegistry.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 globalSlimmerRegistry.VegaSlimmer {
  constructor() {
    super(...arguments);
    /**
     * 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) {
      changeManager.ChangeManager.unregister(staticSubjectTitle.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.Observer(this.isAcceptObserver.bind(this), this.listenerCallback.bind(this));
    changeManager.ChangeManager.register(staticSubjectTitle.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 = internalIconManager.VegaInternalIconManager.getIconKey(countryCode);
    return internalIconManager.VegaInternalIconManager.hasIcon(icon);
  }
}
(() => {
  void dynamicLoadTheCountryFlags();
})();
__decorate$1([
  globalSlimmerRegistry.MapToComponentField()
], VegaFlagIconController.prototype, "host", void 0);
__decorate$1([
  globalSlimmerRegistry.MapToComponentField()
], VegaFlagIconController.prototype, "country", void 0);
__decorate$1([
  globalSlimmerRegistry.MapToComponentMethod('componentWillLoad')
], VegaFlagIconController.prototype, "componentWillLoad", null);
__decorate$1([
  globalSlimmerRegistry.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([
  globalSlimmerRegistry.InjectVegaSlimmer()
], VegaFlagIcon.prototype, "renderer", void 0);
__decorate([
  globalSlimmerRegistry.InjectVegaSlimmer()
], VegaFlagIcon.prototype, "vegaFlagIconController", void 0);
__decorate([
  globalSlimmerRegistry.InjectVegaSlimmer()
], VegaFlagIcon.prototype, "vegaComponentUsageRuntimeMetricsSlimmer", void 0);
VegaFlagIcon.style = vegaFlagIconCss;

exports.vega_flag_icon = VegaFlagIcon;
