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 { FORM_ELEMENT_VALUE_CHANGE } from '../../../../helpers/change-manager/subject/static-subject/static-subject-title';
import ChangeManager from '../../../../helpers/change-manager/change-manager';
import { AsYouType, formatIncompletePhoneNumber, getCountryCallingCode, parseIncompletePhoneNumber, } from '../../../../polyfill/libphonenumber';
import { CountrySelectController } from './country-select-controller';
/**
 * The `VegaInputPhoneNumberValueController` class in TypeScript manages input phone number values and
 * country codes, providing methods to watch, set, and retrieve calling code prefixes.
 */
export class VegaInputPhoneNumberValueController extends VegaSlimmer {
  constructor() {
    super(...arguments);
    /**
     * Format value to nation number
     *
     * @param {string} value - value will be formatted
     * @returns {string} formatted national number.Example: (302) 333-4444
     */
    this.formatNationalNumber = (value) => {
      var _a;
      if (!this.countrySelectRenderer.getValueUpdatedByCountryCodeChange()) {
        this.countrySelectRenderer.setValueUpdatedByCountryCodeChange(false);
        const formatter = new AsYouType(this.countryCode);
        formatter.input(value);
        const newCountry = (_a = formatter.country) !== null && _a !== void 0 ? _a : this.countryCode;
        this.countrySelectController.setCountryCode(newCountry);
        return formatIncompletePhoneNumber(this.removeCallingCodePrefix(value, newCountry), newCountry);
      }
      else {
        this.countrySelectRenderer.setValueUpdatedByCountryCodeChange(false);
        return formatIncompletePhoneNumber(this.removeCallingCodePrefix(value, this.countryCode), this.countryCode);
      }
    };
  }
  /**
   * componentWillLoad
   */
  componentWillLoad() {
    this.updateValue(this.value);
  }
  /**
   * watch emptyValueEnabled change
   */
  watchEmptyValueEnabled() {
    this.updateValue(this.value);
  }
  /**
   * The `watchValue` function updates a value and emits change events based on certain conditions.
   *
   * @param {string} newValue - The `newValue` parameter represents the new value that is being watched
   * for changes.
   * @param {string} oldValue - The `oldValue` parameter in the `watchValue` function represents the
   * previous value before the update. It is used to compare with the new value (`newValue`) to determine
   * if there has been a change in the value.
   */
  watchValue(newValue, oldValue) {
    this.updateValue(newValue);
    if ((oldValue || (this.emptyValueEnabled && oldValue === '')) && oldValue !== this.value) {
      this.changeEventEmitter.emit(this.value);
      ChangeManager.notify(FORM_ELEMENT_VALUE_CHANGE, {
        host: this.host,
        detail: this.value,
      });
    }
  }
  /**
   * set value of vega-input-phone-number
   *
   * @param {string} value new value
   */
  setValue(value) {
    this.value = value;
  }
  /**
   * The function `getCallingCodePrefix` returns the calling code prefix for a given country code.
   *
   * @param {VegaCountryCode} countryCode - VegaCountryCode
   * @returns {string} The `getCallingCodePrefix` function returns a string that includes the country calling code
   * prefix for the specified `VegaCountryCode`.
   */
  getCallingCodePrefix(countryCode) {
    return `+${getCountryCallingCode(countryCode)}`;
  }
  /**
   * The `updateValue` function in TypeScript parses and formats a phone number input, handles errors,
   * and updates the value accordingly.
   *
   * @param {Nullable<string>} value - The `updateValue` function takes a `value` parameter of type string. This
   * function is responsible for updating the phone number value based on the input provided.
   */
  updateValue(value) {
    if (this.shouldSetEmptyValue(value)) {
      this.value = '';
      this.phoneNumberInputValue = '';
      return;
    }
    if (!value) {
      this.countrySelectController.setCountryCode(CountrySelectController.defaultCountryCode);
      this.value = `+${getCountryCallingCode(CountrySelectController.defaultCountryCode)}`;
      return;
    }
    const newValue = parseIncompletePhoneNumber(value);
    this.phoneNumberInputValue = this.formatNationalNumber(newValue);
    this.trySetValue(this.phoneNumberInputValue, () => parseIncompletePhoneNumber(newValue));
  }
  /**
   * Should set value of phone number to empty string
   *
   * @param {string} inputValue - The value of phone number input
   * @returns {boolean} -
   */
  shouldSetEmptyValue(inputValue) {
    return !inputValue && this.emptyValueEnabled;
  }
  /**
   * The function removes the calling code prefix from a given value based on the specified country code.
   *
   * @param {string} value - The `value` parameter is a string that represents a phone number with a
   * calling code prefix.
   * @param {VegaCountryCode} countryCode - VegaCountryCode
   * @returns {string} The `removeCallingCodePrefix` function is returning a substring of the `value` string
   * starting from the length of the calling code prefix for the specified `country`.
   */
  removeCallingCodePrefix(value, countryCode) {
    const prefix = this.getCallingCodePrefix(countryCode);
    if (value.startsWith(prefix)) {
      return value.replace(this.getCallingCodePrefix(countryCode), '').trim();
    }
    else {
      return value.replace('+', '').trim();
    }
  }
  /**
   * Set value
   *
   * @param {string} inputValue - The value of phone number input.
   * @param {Function} formatValue - Format value callback
   */
  trySetValue(inputValue, formatValue) {
    this.value = this.shouldSetEmptyValue(inputValue) ? '' : formatValue();
  }
}
__decorate([
  MapToComponentField()
], VegaInputPhoneNumberValueController.prototype, "host", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaInputPhoneNumberValueController.prototype, "value", void 0);
__decorate([
  MapToComponentField()
], VegaInputPhoneNumberValueController.prototype, "countryCode", void 0);
__decorate([
  MapToComponentField()
], VegaInputPhoneNumberValueController.prototype, "countrySelectController", void 0);
__decorate([
  MapToComponentField()
], VegaInputPhoneNumberValueController.prototype, "changeEventEmitter", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaInputPhoneNumberValueController.prototype, "phoneNumberInputValue", void 0);
__decorate([
  MapToComponentField()
], VegaInputPhoneNumberValueController.prototype, "emptyValueEnabled", void 0);
__decorate([
  MapToComponentField()
], VegaInputPhoneNumberValueController.prototype, "countrySelectRenderer", void 0);
__decorate([
  MapToComponentMethod('componentWillLoad')
], VegaInputPhoneNumberValueController.prototype, "componentWillLoad", null);
__decorate([
  MapToComponentMethod('watchEmptyValueEnabled')
], VegaInputPhoneNumberValueController.prototype, "watchEmptyValueEnabled", null);
__decorate([
  MapToComponentMethod('watchValue')
], VegaInputPhoneNumberValueController.prototype, "watchValue", null);
