import { isDemo } from "@saasquatch/component-boilerplate";
import { withHooks } from "@saasquatch/stencil-hooks";
import { Component, h, Prop, State } from "@stencil/core";
import deepmerge from "deepmerge";
import { RequiredPropsError } from "../../utils/RequiredPropsError";
import { getMissingProps, getProps } from "../../utils/utils";
import { InputFieldView } from "./sqm-input-field-view";
import { useInputField } from "./useInputField";
/**
 * @uiName Form Input Field
 * @validParents ["sqm-portal-register","sqm-portal-registration-form", "sqm-portal-google-registration-form"]
 * @exampleGroup Microsite Components
 * @example Form Input Field - <sqm-input-field input-label="Field Label" field-type="text" error-message="Cannot be empty"></sqm-input-field>
 */
export class InputField {
  constructor() {
    this.ignored = true;
    /**
     * @uiName Input type
     * @uiType string
     * @uiEnum ["text", "date", "tel"]
     * @uiEnumNames ["Text", "Date", "Phone Number"]
     */
    this.fieldType = "text";
    /**
     * @uiName Required error message
     */
    this.errorMessage = "Cannot be empty";
    /**
     * @uiName Optional
     * @default
     */
    this.fieldOptional = false;
    withHooks(this);
  }
  disconnectedCallback() { }
  render() {
    const content = {
      ...getProps(this),
    };
    const missingProps = getMissingProps([
      {
        attribute: "field-name",
        value: this.fieldName,
      },
    ]);
    if (!isDemo() && missingProps) {
      return (h(RequiredPropsError, { missingProps: missingProps, heading: "An error occured while loading this form", subheading: "A technical problem prevented this input field from loading. Please contact us with the link to this page.", description: "Values for the following attributes are missing:" }));
    }
    const { states } = isDemo() ? useInputFieldDemo(this) : useInputField();
    return h(InputFieldView, { states: states, content: content });
  }
  static get is() { return "sqm-input-field"; }
  static get properties() { return {
    "fieldName": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "text": "Input name attribute",
            "name": "uiName"
          }, {
            "text": undefined,
            "name": "required"
          }],
        "text": "This name is used as the key for this form field on submission. The name must be unique within this specific form."
      },
      "attribute": "field-name",
      "reflect": false
    },
    "fieldLabel": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "text": "Input label",
            "name": "uiName"
          }],
        "text": ""
      },
      "attribute": "field-label",
      "reflect": false
    },
    "fieldType": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "\"text\" | \"date\" | \"tel\"",
        "resolved": "\"date\" | \"tel\" | \"text\"",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "text": "Input type",
            "name": "uiName"
          }, {
            "text": "string",
            "name": "uiType"
          }, {
            "text": "[\"text\", \"date\", \"tel\"]",
            "name": "uiEnum"
          }, {
            "text": "[\"Text\", \"Date\", \"Phone Number\"]",
            "name": "uiEnumNames"
          }],
        "text": ""
      },
      "attribute": "field-type",
      "reflect": false,
      "defaultValue": "\"text\""
    },
    "errorMessage": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "text": "Required error message",
            "name": "uiName"
          }],
        "text": ""
      },
      "attribute": "error-message",
      "reflect": false,
      "defaultValue": "\"Cannot be empty\""
    },
    "fieldOptional": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [{
            "text": "Optional",
            "name": "uiName"
          }, {
            "text": undefined,
            "name": "default"
          }],
        "text": ""
      },
      "attribute": "field-optional",
      "reflect": false,
      "defaultValue": "false"
    },
    "demoData": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "DemoData<InputFieldViewProps>",
        "resolved": "{ states?: { registrationFormState?: RegistrationFormState; }; content?: { fieldName: string; fieldLabel: string; fieldType: \"text\" | \"date\" | \"tel\"; fieldOptional?: boolean; errorMessage: string; }; }",
        "references": {
          "DemoData": {
            "location": "import",
            "path": "../../global/demo"
          },
          "InputFieldViewProps": {
            "location": "import",
            "path": "./sqm-input-field-view"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [{
            "text": undefined,
            "name": "undocumented"
          }, {
            "text": "object",
            "name": "uiType"
          }],
        "text": ""
      }
    }
  }; }
  static get states() { return {
    "ignored": {}
  }; }
}
function useInputFieldDemo(props) {
  return deepmerge({
    states: {
      registrationFormState: {},
    },
  }, props.demoData || {}, { arrayMerge: (_, a) => a });
}
