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 { LeadDropdownFieldView, } from "./sqm-lead-dropdown-field-view";
import { useLeadDropdownField } from "./useLeadDropdownField";
/**
 * @uiName Lead Form Dropdown Field
 * @validParents ["sqm-lead-form"]
 * @slots [{"name":"", "title":"Dropdown Option"}]
 */
export class LeadDropdownField {
  constructor() {
    this.ignored = true;
    /**
     * @uiName Dropdown label
     */
    this.dropdownLabel = "Select an option";
    /**
     * The message to be displayed when a required field is not filled.
     *
     * @uiName Required field message
     * @uiWidget textArea
     */
    this.requiredFieldErrorMessage = "Please select a {dropdownLabel}";
    /**
     * @uiName Optional label
     */
    this.optionalLabel = "(optional)";
    /**
     * @uiName Optional
     * @default
     */
    this.dropdownOptional = false;
    /**
     * @uiName Multiple selection
     * @default
     */
    this.multiple = false;
    withHooks(this);
  }
  disconnectedCallback() { }
  render() {
    const missingProps = getMissingProps([
      {
        attribute: "dropdown-name",
        value: this.dropdownName,
      },
    ]);
    if (!isDemo() && missingProps) {
      return (h(RequiredPropsError, { missingProps: missingProps, heading: "An error occured while loading this form", subheading: "A technical problem prevented this drop down field from loading. Please contact us with the link to this page.", description: "Values for the following attributes are missing:" },
        h("slot", null)));
    }
    const content = {
      ...getProps(this),
      selectOptions: h("slot", null),
    };
    const { states } = isDemo()
      ? useLeadDropdownFieldDemo(this)
      : useLeadDropdownField();
    return (h(LeadDropdownFieldView, { states: states, content: content }));
  }
  static get is() { return "sqm-lead-dropdown-field"; }
  static get properties() { return {
    "dropdownName": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "text": "Dropdown 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": "dropdown-name",
      "reflect": false
    },
    "dropdownLabel": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "text": "Dropdown label",
            "name": "uiName"
          }],
        "text": ""
      },
      "attribute": "dropdown-label",
      "reflect": false,
      "defaultValue": "\"Select an option\""
    },
    "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 select a {dropdownLabel}\""
    },
    "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)\""
    },
    "dropdownOptional": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "text": "Optional",
            "name": "uiName"
          }, {
            "text": undefined,
            "name": "default"
          }],
        "text": ""
      },
      "attribute": "dropdown-optional",
      "reflect": false,
      "defaultValue": "false"
    },
    "multiple": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "text": "Multiple selection",
            "name": "uiName"
          }, {
            "text": undefined,
            "name": "default"
          }],
        "text": ""
      },
      "attribute": "multiple",
      "reflect": false,
      "defaultValue": "false"
    },
    "demoData": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "DemoData<DropdownFieldViewProps>",
        "resolved": "{ states?: { leadFormState?: LeadFormState; }; content?: { dropdownName: string; dropdownLabel: string; dropdownOptional: boolean; optionalLabel: string; multiple: boolean; requiredFieldErrorMessage?: string; selectOptions?: VNode | VNode[]; }; }",
        "references": {
          "DemoData": {
            "location": "import",
            "path": "../../global/demo"
          },
          "DropdownFieldViewProps": {
            "location": "import",
            "path": "./sqm-lead-dropdown-field-view"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [{
            "text": undefined,
            "name": "undocumented"
          }, {
            "text": "object",
            "name": "uiType"
          }],
        "text": ""
      }
    }
  }; }
  static get states() { return {
    "ignored": {}
  }; }
}
function useLeadDropdownFieldDemo(props) {
  return deepmerge({
    states: {
      leadFormState: {},
    },
    content: {
      dropdownName: props.dropdownName,
      dropdownLabel: props.dropdownLabel,
      requiredFieldErrorMessage: props.requiredFieldErrorMessage,
      dropdownOptional: props.dropdownOptional,
    },
  }, props.demoData || {}, { arrayMerge: (_, a) => a });
}
