var __decorate = (this && this.__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;
};
import { MapToComponentField, MapToComponentMethod, VegaSlimmer } from 'vega-slimmer/core';
import { VegaInternalIconManager } from '../../../../helpers/icon/internal-icon-manager';
import { LogUtility } from '../../../../utils/log';
import { FLAG_ICON_LOADED } from '../../../../helpers/change-manager/subject/static-subject/static-subject-title';
import { Observer } from '../../../../helpers/change-manager/observer/observer';
import changeManager from '../../../../helpers/change-manager/change-manager';
import { forceUpdate } from '@stencil/core';
/**
 * The function `dynamicLoadTheCountryFlags` will dynamic import and register flags of all countries at once
 */
export async function dynamicLoadTheCountryFlags() {
  try {
    const countryFlags = (await import('../../assets/country-flags')).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.
 */
export class VegaFlagIconController extends 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 = () => {
      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([
  MapToComponentField()
], VegaFlagIconController.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], VegaFlagIconController.prototype, "country", void 0);
__decorate([
  MapToComponentMethod('componentWillLoad')
], VegaFlagIconController.prototype, "componentWillLoad", null);
__decorate([
  MapToComponentMethod('disconnectedCallback')
], VegaFlagIconController.prototype, "unRegisterObserver", null);
