import { isDemo } from "@saasquatch/component-boilerplate";
import { withHooks } from "@saasquatch/stencil-hooks";
import { useState } from "@saasquatch/universal-hooks";
import { Component, h, Prop, State } from "@stencil/core";
import deepmerge from "deepmerge";
import { getProps } from "../../utils/utils";
import { validateNewPassword } from "./passwordValidation";
import { PortalResetPasswordView, } from "./sqm-password-field-view";
import { usePasswordField, } from "./usePasswordField";
/**
 * @uiName Form Password Field
 * @validParents ["sqm-portal-register","sqm-portal-registration-form", "sqm-portal-google-registration-form"]
 * @exampleGroup Microsite Components
 * @example Form Password Field - <sqm-password-field field-label="Password"></sqm-password-field>
 */
export class PortalPasswordField {
  constructor() {
    this.ignored = true;
    /**
     * Label for password field
     *
     * @uiName Password label
     */
    this.fieldLabel = "Password";
    /**
     * Disable live password validation
     * @uiName Disable validation
     */
    this.disableValidation = false;
    /**
     * @uiName Password requirement met
     */
    this.meetsRequirementsText = "Password has met all requirements";
    /**
     * @uiName Password requirement failed
     */
    this.doesNotMeetRequirementsText = "Password must meet the following requirements:";
    /**
     * @uiName Minimum length text
     */
    this.minErrorText = "be a minimum of 8 characters";
    /**
     * @uiName Missing uppercase text
     */
    this.uppercaseErrorText = "contain at least 1 uppercase character";
    /**
     * @uiName Missing lowercase text
     */
    this.lowercaseErrorText = "contain at least 1 lowercase character";
    /**
     * @uiName Missing number or symbol text
     */
    this.hasErrorText = "contain at least 1 number or symbol";
    withHooks(this);
  }
  disconnectedCallback() { }
  render() {
    const { states, callbacks } = isDemo()
      ? usePasswordFieldDemo(getProps(this))
      : usePasswordField(getProps(this));
    return h(PortalResetPasswordView, { states: states, callbacks: callbacks });
  }
  static get is() { return "sqm-password-field"; }
  static get properties() { return {
    "fieldLabel": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "text": "Password label",
            "name": "uiName"
          }],
        "text": "Label for password field"
      },
      "attribute": "field-label",
      "reflect": false,
      "defaultValue": "\"Password\""
    },
    "disableValidation": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "text": "Disable validation",
            "name": "uiName"
          }],
        "text": "Disable live password validation"
      },
      "attribute": "disable-validation",
      "reflect": false,
      "defaultValue": "false"
    },
    "meetsRequirementsText": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "text": "Password requirement met",
            "name": "uiName"
          }],
        "text": ""
      },
      "attribute": "meets-requirements-text",
      "reflect": false,
      "defaultValue": "\"Password has met all requirements\""
    },
    "doesNotMeetRequirementsText": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "text": "Password requirement failed",
            "name": "uiName"
          }],
        "text": ""
      },
      "attribute": "does-not-meet-requirements-text",
      "reflect": false,
      "defaultValue": "\"Password must meet the following requirements:\""
    },
    "minErrorText": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "text": "Minimum length text",
            "name": "uiName"
          }],
        "text": ""
      },
      "attribute": "min-error-text",
      "reflect": false,
      "defaultValue": "\"be a minimum of 8 characters\""
    },
    "uppercaseErrorText": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "text": "Missing uppercase text",
            "name": "uiName"
          }],
        "text": ""
      },
      "attribute": "uppercase-error-text",
      "reflect": false,
      "defaultValue": "\"contain at least 1 uppercase character\""
    },
    "lowercaseErrorText": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "text": "Missing lowercase text",
            "name": "uiName"
          }],
        "text": ""
      },
      "attribute": "lowercase-error-text",
      "reflect": false,
      "defaultValue": "\"contain at least 1 lowercase character\""
    },
    "hasErrorText": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "text": "Missing number or symbol text",
            "name": "uiName"
          }],
        "text": ""
      },
      "attribute": "has-error-text",
      "reflect": false,
      "defaultValue": "\"contain at least 1 number or symbol\""
    },
    "demoData": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "PasswordFieldViewDemoProps",
        "resolved": "PasswordFieldViewDemoProps",
        "references": {
          "PasswordFieldViewDemoProps": {
            "location": "import",
            "path": "./usePasswordField"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [{
            "text": undefined,
            "name": "undocumented"
          }, {
            "text": "object",
            "name": "uiType"
          }],
        "text": ""
      }
    }
  }; }
  static get states() { return {
    "ignored": {}
  }; }
}
function usePasswordFieldDemo(props) {
  var _a, _b, _c, _d, _e, _f, _g;
  const [dynamicValidation, setDynamicValidation] = useState("");
  const [lastValidated, setLastValidated] = useState("");
  if (props.demoData && lastValidated != ((_a = props === null || props === void 0 ? void 0 : props.demoData) === null || _a === void 0 ? void 0 : _a.initValue)) {
    const validation = validateNewPassword(((_b = props === null || props === void 0 ? void 0 : props.demoData) === null || _b === void 0 ? void 0 : _b.initValue) || "", props);
    setDynamicValidation(((_c = props === null || props === void 0 ? void 0 : props.demoData) === null || _c === void 0 ? void 0 : _c.initValue) === "" ? "" : validation);
    setLastValidated((_d = props === null || props === void 0 ? void 0 : props.demoData) === null || _d === void 0 ? void 0 : _d.initValue);
  }
  function onInput(input) {
    const validation = validateNewPassword(input.target.value, props);
    setDynamicValidation(validation);
  }
  return deepmerge({
    states: {
      enableValidation: !props.disableValidation,
      dynamicValidation,
      registrationFormState: ((_f = (_e = props === null || props === void 0 ? void 0 : props.demoData) === null || _e === void 0 ? void 0 : _e.states) === null || _f === void 0 ? void 0 : _f.registrationFormState) || {},
      content: {
        fieldLabel: props.fieldLabel,
      },
    },
    callbacks: {
      onInput,
    },
  }, ((_g = props === null || props === void 0 ? void 0 : props.demoData) === null || _g === void 0 ? void 0 : _g.states) || {}, { arrayMerge: (_, a) => a });
}
