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 { DropdownFieldView, } from "./sqm-dropdown-field-view";
import { useDropdownField } from "./useDropdownField";
/**
 * @uiName Form Dropdown Field
 * @validParents ["sqm-portal-register","sqm-portal-registration-form", "sqm-portal-google-registration-form"]
 * @slots [{"name":"", "title":"Drop Down Option"}]
 * @exampleGroup Microsite Components
 * @example Form Dropdown Field - <sqm-dropdown-field dropdown-label="Select an option" error-message="Select an option"><sl-menu-item value="option-1">Option 1</sl-menu-item><sl-menu-item value="option-2">Option 2</sl-menu-item><sl-menu-item value="option-3">Option 3</sl-menu-item></sqm-dropdown-field>
 */
export class DropdownField {
  constructor() {
    this.ignored = true;
    /**
     * @uiName Dropdown label
     */
    this.dropdownLabel = "Select an option";
    /**
     * @uiName Unselected error message
     */
    this.errorMessage = "Select an option";
    /**
     * @uiName Optional
     * @default
     */
    this.dropdownOptional = 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()
      ? useDropdownFieldDemo(this)
      : useDropdownField();
    return (h(DropdownFieldView, { states: states, content: content }));
  }
  static get is() { return "sqm-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\""
    },
    "errorMessage": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "text": "Unselected error message",
            "name": "uiName"
          }],
        "text": ""
      },
      "attribute": "error-message",
      "reflect": false,
      "defaultValue": "\"Select an option\""
    },
    "dropdownOptional": {
      "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": "dropdown-optional",
      "reflect": false,
      "defaultValue": "false"
    },
    "demoData": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "DemoData<DropdownFieldViewProps>",
        "resolved": "{ states?: { registrationFormState?: RegistrationFormState; }; content?: { dropdownName: string; dropdownLabel: string; dropdownOptional?: boolean; errorMessage: string; selectOptions?: VNode | VNode[]; }; }",
        "references": {
          "DemoData": {
            "location": "import",
            "path": "../../global/demo"
          },
          "DropdownFieldViewProps": {
            "location": "import",
            "path": "./sqm-dropdown-field-view"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [{
            "text": undefined,
            "name": "undocumented"
          }, {
            "text": "object",
            "name": "uiType"
          }],
        "text": ""
      }
    }
  }; }
  static get states() { return {
    "ignored": {}
  }; }
}
function useDropdownFieldDemo(props) {
  return deepmerge({
    states: {
      registrationFormState: {},
    },
  }, props.demoData || {}, { arrayMerge: (_, a) => a });
}
