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 { LeadInputFieldView, } from "./sqm-lead-input-field-view";
import { useLeadInputField } from "./useLeadInputField";
/**
 * @uiName Lead Form Input Field
 * @validParents ["sqm-lead-form"]
 */
export class LeadInputField {
  constructor() {
    this.ignored = true;
    /**
     * @uiName Optional label
     */
    this.optionalLabel = "(optional)";
    /**
     * @uiName Input type
     * @uiType string
     * @uiEnum ["text", "date", "tel"]
     * @uiEnumNames ["Text", "Date", "Phone Number"]
     */
    this.fieldType = "text";
    /**
     * The message to be displayed when a required field is not filled.
     *
     * @uiName Required field message
     * @uiWidget textArea
     */
    this.requiredFieldErrorMessage = "Please enter a {fieldLabel}";
    /**
     * @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()
      ? useLeadInputFieldDemo(this)
      : useLeadInputField();
    return (h(LeadInputFieldView, { states: states, content: content }));
  }
  static get is() { return "sqm-lead-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
    },
    "optionalLabel": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "text": "Optional label",
            "name": "uiName"
          }],
        "text": ""
      },
      "attribute": "optional-label",
      "reflect": false,
      "defaultValue": "\"(optional)\""
    },
    "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\""
    },
    "requiredFieldErrorMessage": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "text": "Required field message",
            "name": "uiName"
          }, {
            "text": "textArea",
            "name": "uiWidget"
          }],
        "text": "The message to be displayed when a required field is not filled."
      },
      "attribute": "required-field-error-message",
      "reflect": false,
      "defaultValue": "\"Please enter a {fieldLabel}\""
    },
    "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<LeadInputFieldViewProps>",
        "resolved": "{ states?: { leadFormState?: LeadFormState; }; content?: { fieldName: string; fieldLabel: string; optionalLabel: string; fieldType: \"text\" | \"date\" | \"tel\"; fieldOptional?: boolean; requiredFieldErrorMessage: string; }; }",
        "references": {
          "DemoData": {
            "location": "import",
            "path": "../../global/demo"
          },
          "LeadInputFieldViewProps": {
            "location": "import",
            "path": "./sqm-lead-input-field-view"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [{
            "text": undefined,
            "name": "undocumented"
          }, {
            "text": "object",
            "name": "uiType"
          }],
        "text": ""
      }
    }
  }; }
  static get states() { return {
    "ignored": {}
  }; }
}
function useLeadInputFieldDemo(props) {
  return deepmerge({
    states: {
      leadForState: {},
    },
    content: {
      fieldName: props.fieldName,
      fieldLabel: props.fieldLabel,
      fieldType: props.fieldType,
      requiredFieldErrorMessage: props.requiredFieldErrorMessage,
    },
  }, props.demoData || {}, { arrayMerge: (_, a) => a });
}
