/*!
 * (C) Gasco https://gasco-web-component.herokuapp.com/ - MIT License
 */
import { Build, Component, Element, Event, Host, Prop, State, Watch, h } from '@stencil/core';
import { inheritAriaAttributes, inheritAttributes } from '../../utils/helpers';
import { createColorClasses } from '../../utils/theme';
export class GascoInputCode {
  constructor() {
    this.isComposing = false;
    this.arrayInput = new Array();
    this.inheritedAttributes = {};
    this.inputId = 'gasco-input-code-';
    this.fireFocusEvents = true;
    this.hasFocus = false;
    /**
     * We can make everything automatic after completing the code for default is false.
     */
    this.autosubmit = false;
    /**
     * This Boolean attributes lets you specify that a from control should have input focus when the page loads.
     */
    this.autofocus = false;
    /**
     * If `true`, the user cannot interact with the input.
     */
    this.disabled = false;
    /**
     * The type of control to display. The default type is `text`.
     */
    this.type = 'text';
    /**
     * The value of the input.
     */
    this.value = '';
    /**
     * If the value of the type attribute is `text` or `numeric`, this attribute specifies the maximum number of characters that the user can enter.
     * Defaulf `4`
     */
    this.maxlength = 4;
    /**
     * The name of the control, which is submitted with the form data.
     */
    this.name = this.inputId;
    /**
     * With secure what you are looking for is that the fields are not seen.
     * for default is true
     */
    this.secure = true;
    /**
     * A simple count for the loop input
     */
    this.count = 0;
    //? METHODs
    this.onComposition = (v) => {
      this.isComposing = v;
    };
    this.onInput = (ev) => {
      this.count += 1;
      const input = ev.target;
      if (input) {
        this.value = input.value;
        this.handleInput(input);
      }
      this.gascoInput.emit(ev);
    };
    this.onBlur = (ev) => {
      this.hasFocus = false;
      this.emitStyle();
      if (this.fireFocusEvents) {
        this.gascoBlur.emit(ev);
      }
    };
    this.onFocus = (ev) => {
      this.hasFocus = true;
      this.emitStyle();
      if (this.fireFocusEvents) {
        this.gascoFocus.emit(ev);
      }
    };
  }
  //? WATCHs
  disabledChanged() {
    this.emitStyle();
  }
  getValue() {
    return typeof this.value === 'number' ? this.value.toString() : (this.value || '').toString();
  }
  emitStyle() {
    this.gascoStyle.emit({
      interactive: true,
      input: true,
      'has-value': this.hasValue(),
      'has-focus': this.hasFocus,
      'interactive-disabled': this.disabled,
    });
  }
  ;
  handleInput(input) {
    this.el.childNodes.forEach((_, index) => {
      var _a;
      if (this.count - 1 === index) {
        const child = (_a = this.el.childNodes[index + 1]) === null || _a === void 0 ? void 0 : _a.firstChild;
        child === null || child === void 0 ? void 0 : child.focus();
        this.arrayInput[index] = input.value;
      }
    });
    if (input.value && !this.isComposing) { }
    if (this.autosubmit && this.arrayInput.join('').length === this.maxlength) {
      this.gascoCodeDone.emit({ detail: this.arrayInput.join('') });
    }
    this.emitStyle();
    this.gascoChange.emit({ value: typeof this.value !== 'number' ? this.value : this.value.toString() });
  }
  hasValue() {
    return this.getValue().length > 0;
  }
  ;
  componentWillLoad() {
    for (let i = 0; i < this.maxlength; i++) {
      this.arrayInput.push('');
    }
    this.inheritedAttributes = Object.assign(Object.assign({}, inheritAriaAttributes(this.el)), inheritAttributes(this.el, ['tabindex', 'title']));
  }
  connectedCallback() {
    this.emitStyle();
    if (Build.isBrowser) {
      document.dispatchEvent(new CustomEvent('gascoInputDidLoad', {
        detail: this.el,
      }));
    }
  }
  componentDidLoad() {
    const nativeInput = this.el.childNodes[0].firstChild;
    if (nativeInput) {
      nativeInput.addEventListener('compositionstart', this.onComposition.bind(true));
      nativeInput.addEventListener('compositionend', this.onComposition.bind(false));
    }
  }
  disconnectedCallback() {
    if (Build.isBrowser) {
      document.dispatchEvent(new CustomEvent('gascoInputDidUnload', {
        detail: this.el,
      }));
    }
    const nativeInput = this.el.childNodes[0].firstChild;
    if (nativeInput) {
      nativeInput.removeEventListener('compositionstart', this.onComposition.bind(true));
      nativeInput.removeEventListener('compositionend', this.onComposition.bind(false));
    }
  }
  render() {
    const finalSize = this.size === undefined && false ? 'small' : this.size;
    return (h(Host, { "aria-disabled": this.disabled ? 'true' : null, class: createColorClasses(this.color, {
        'has-value': this.hasValue(),
        'has-focus': this.hasFocus,
        [`input-${finalSize}`]: finalSize !== undefined,
      }) }, this.arrayInput.map((value, index) => (h("div", { class: "native-input-code" },
      h("input", Object.assign({ class: "native-input code", "aria-labelledby": this.name ? this.name + index : null, disabled: this.disabled, autoFocus: this.autofocus && index === 0, inputMode: this.inputmode, maxLength: 1, name: this.name + index, pattern: this.pattern, type: this.type, value: value, placeholder: this.secure ? '*' : '0', onInput: this.onInput, onBlur: this.onBlur, onFocus: this.onFocus }, this.inheritedAttributes)))))));
  }
  static get is() { return "gasco-input-code"; }
  static get encapsulation() { return "scoped"; }
  static get originalStyleUrls() { return {
    "$": ["gasco-input-code.md.scss"]
  }; }
  static get styleUrls() { return {
    "$": ["gasco-input-code.md.css"]
  }; }
  static get properties() { return {
    "fireFocusEvents": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "fire-focus-events",
      "reflect": false,
      "defaultValue": "true"
    },
    "autosubmit": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "We can make everything automatic after completing the code for default is false."
      },
      "attribute": "autosubmit",
      "reflect": false,
      "defaultValue": "false"
    },
    "color": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "Color",
        "resolved": "\"danger\" | \"dark\" | \"light\" | \"medium\" | \"primary\" | \"secondary\" | \"success\" | \"tertiary\" | \"warning\" | string & Record<never, never>",
        "references": {
          "Color": {
            "location": "import",
            "path": "../../interface"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": "The color to use from your application's color palette.\nDefault options are: `primary`, `secondary`, `tertiary`, `success`, `warning`, `danger`, `light`, and `dark`."
      },
      "attribute": "color",
      "reflect": true
    },
    "autofocus": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "This Boolean attributes lets you specify that a from control should have input focus when the page loads."
      },
      "attribute": "autofocus",
      "reflect": false,
      "defaultValue": "false"
    },
    "disabled": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "If `true`, the user cannot interact with the input."
      },
      "attribute": "disabled",
      "reflect": false,
      "defaultValue": "false"
    },
    "type": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "'text' | 'number'",
        "resolved": "\"number\" | \"text\"",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "The type of control to display. The default type is `text`."
      },
      "attribute": "type",
      "reflect": false,
      "defaultValue": "'text'"
    },
    "size": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "'small' | 'default' | 'large'",
        "resolved": "\"default\" | \"large\" | \"small\"",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": "The Input size."
      },
      "attribute": "size",
      "reflect": true
    },
    "value": {
      "type": "any",
      "mutable": true,
      "complexType": {
        "original": "string | number | null",
        "resolved": "number | string",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": "The value of the input."
      },
      "attribute": "value",
      "reflect": false,
      "defaultValue": "''"
    },
    "inputmode": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "'none' | 'text'",
        "resolved": "\"none\" | \"text\"",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": "A hint to the browser for which keyboard to display.\nPossible values: `none`, `text`."
      },
      "attribute": "inputmode",
      "reflect": false
    },
    "maxlength": {
      "type": "number",
      "mutable": false,
      "complexType": {
        "original": "number",
        "resolved": "number",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": "If the value of the type attribute is `text` or `numeric`, this attribute specifies the maximum number of characters that the user can enter.\nDefaulf `4`"
      },
      "attribute": "maxlength",
      "reflect": false,
      "defaultValue": "4"
    },
    "name": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "The name of the control, which is submitted with the form data."
      },
      "attribute": "name",
      "reflect": false,
      "defaultValue": "this.inputId"
    },
    "pattern": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": "A regular expression that the value is checked against. The pattern must match the entire value, not just some subset. Use the title attribute to describe the pattern to help the user. This attribute applies when the value of the type attribute is `text`, `search`, `tel`, `url`, `email`, `\"date\"`, or `password`, otherwise it is ignored. When the type attribute is `date`, `pattern` will only be used in browsers that do not support the `date` input type natively."
      },
      "attribute": "pattern",
      "reflect": false
    },
    "secure": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "With secure what you are looking for is that the fields are not seen.\nfor default is true"
      },
      "attribute": "secure",
      "reflect": false,
      "defaultValue": "true"
    }
  }; }
  static get states() { return {
    "hasFocus": {},
    "count": {}
  }; }
  static get events() { return [{
      "method": "gascoCodeDone",
      "name": "gascoCodeDone",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }, {
      "method": "gascoInput",
      "name": "gascoInput",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": "Emitted when a keyboard input occurred."
      },
      "complexType": {
        "original": "InputEvent",
        "resolved": "InputEvent",
        "references": {
          "InputEvent": {
            "location": "global"
          }
        }
      }
    }, {
      "method": "gascoChange",
      "name": "gascoChange",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": "Emitted when the value has changed."
      },
      "complexType": {
        "original": "InputChangeEventDetail",
        "resolved": "InputChangeEventDetail",
        "references": {
          "InputChangeEventDetail": {
            "location": "import",
            "path": "../../interface"
          }
        }
      }
    }, {
      "method": "gascoBlur",
      "name": "gascoBlur",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": "Emitted when the input loses focus."
      },
      "complexType": {
        "original": "FocusEvent",
        "resolved": "FocusEvent",
        "references": {
          "FocusEvent": {
            "location": "global"
          }
        }
      }
    }, {
      "method": "gascoFocus",
      "name": "gascoFocus",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": "Emitted when the input has focus."
      },
      "complexType": {
        "original": "FocusEvent",
        "resolved": "FocusEvent",
        "references": {
          "FocusEvent": {
            "location": "global"
          }
        }
      }
    }, {
      "method": "gascoStyle",
      "name": "gascoStyle",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [{
            "name": "internal",
            "text": undefined
          }],
        "text": "Emitted when the styles change."
      },
      "complexType": {
        "original": "StyleEventDetail",
        "resolved": "StyleEventDetail",
        "references": {
          "StyleEventDetail": {
            "location": "import",
            "path": "../../interface"
          }
        }
      }
    }]; }
  static get elementRef() { return "el"; }
  static get watchers() { return [{
      "propName": "disabled",
      "methodName": "disabledChanged"
    }]; }
}
