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 { h } from '@stencil/core';
import { MapToComponentField, VegaSlimmer } from 'vega-slimmer/core';
import { DomNodeSubjectObserverFactory } from '../../../../helpers/change-manager/subject/dom-node-subject/dom-node-subject-observer-factory';
import { VegaChange } from '../../../../helpers/event-manager/event-id/vega-event-id';
import { parseIncompletePhoneNumber } from '../../../../polyfill/libphonenumber';
import { VegaPhoneNumberInputStrategy } from '../../../../helpers/formatter/string-formatter/vega-phone-number-input-strategy';
export class PhoneNumberInputRenderer extends VegaSlimmer {
  constructor() {
    super(...arguments);
    this.phoneNumberMaskFormatter = (rawValue) => {
      const newValue = parseIncompletePhoneNumber(rawValue);
      return this.valueController.formatNationalNumber(newValue);
    };
  }
  /**
   * The function `getInputRef` returns the HTML input element reference stored in the `inputRef`
   * property.
   * @returns {HTMLVegaInputElement} An HTMLVegaInputElement, specifically the inputRef.
   */
  getInputRef() {
    return this.inputRef;
  }
  /**
   * The render function returns a JSX element with a vega-box containing a vega-input element with
   * various props and event handlers.
   *
   * @returns {HTMLElement} An HTMLElement containing a vega-box element with a vega-input element inside. The
   * vega-input element has various props such as size, disabled, placeholder, ref, value, and onKeyDown
   * set with corresponding values or functions.
   */
  render() {
    return (h("vega-box", { flexGrow: 1 },
      h("vega-input", { size: this.size, disabled: this.disabled, placeholder: this.placeholder, ref: (ref) => {
          this.inputRef = ref;
          DomNodeSubjectObserverFactory.addUniqueObserverToNode(ref, VegaChange, this.handleInputChange.bind(this));
        }, inputmode: "numeric", value: this.phoneNumberInputValue, maskConfig: {
          type: 'custom',
          strategy: () => new VegaPhoneNumberInputStrategy(this.phoneNumberMaskFormatter),
        } })));
  }
  /**
   * The handleInputChange function updates the input value with a new value including the calling code
   * prefix for a specific country.
   *
   * @param {CustomEvent} e - The parameter `e` in the `handleInputChange` function is a CustomEvent
   * object. It seems to contain a `detail` property that holds the new input value as a string.
   */
  handleInputChange(e) {
    const newInputValue = e.detail;
    /*
     * The below method is e2e-test covered in
     * @see{module:vega-input-phone-number-input-prevent-non-number}
     */
    /* istanbul ignore next */
    if (this.valueController.shouldSetEmptyValue(newInputValue)) {
      this.valueController.setValue('');
    }
    else {
      const value = newInputValue.startsWith('+')
        ? newInputValue
        : `${this.valueController.getCallingCodePrefix(this.countryCode)}` +
          newInputValue.replace(/\D/g, '');
      this.valueController.setValue(value);
    }
  }
}
__decorate([
  MapToComponentField()
], PhoneNumberInputRenderer.prototype, "size", void 0);
__decorate([
  MapToComponentField()
], PhoneNumberInputRenderer.prototype, "placeholder", void 0);
__decorate([
  MapToComponentField()
], PhoneNumberInputRenderer.prototype, "disabled", void 0);
__decorate([
  MapToComponentField()
], PhoneNumberInputRenderer.prototype, "value", void 0);
__decorate([
  MapToComponentField()
], PhoneNumberInputRenderer.prototype, "countryCode", void 0);
__decorate([
  MapToComponentField()
], PhoneNumberInputRenderer.prototype, "valueController", void 0);
__decorate([
  MapToComponentField()
], PhoneNumberInputRenderer.prototype, "countrySelectController", void 0);
__decorate([
  MapToComponentField()
], PhoneNumberInputRenderer.prototype, "phoneNumberInputValue", void 0);
