import { isDemo, navigation } from "@saasquatch/component-boilerplate";
import { withHooks } from "@saasquatch/stencil-hooks";
import { Component, h, Prop, State } from "@stencil/core";
import deepmerge from "deepmerge";
import { PortalLoginView } from "./sqm-portal-login-view";
import { usePortalLogin } from "./usePortalLogin";
import { createStyleSheet } from "../../styling/JSS";
/**
 * @uiName Microsite Login
 * @validParents ["sqm-portal-container","div","sqm-divided-layout","sqm-brand", "template"]
 */
export class PortalLogin {
  constructor() {
    this.ignored = true;
    /**
     * Redirect participants to this page after they successfully login.
     *
     * @uiName Login redirect
     * @uiWidget pageSelect
     */
    this.nextPage = "/";
    /**
     * @uiName Email field label
     */
    this.emailLabel = "Email";
    /**
     * @uiName Password field label
     */
    this.passwordLabel = "Password";
    /**
     * @uiName Submit button text
     */
    this.submitLabel = "Sign In";
    /**
     * Redirect participants to this page to reset their password
     * @uiName Forgot password redirect
     * @uiWidget pageSelect
     */
    this.forgotPasswordPath = "/forgotPassword";
    /**
     * @uiName Forgot password text
     */
    this.forgotPasswordLabel = "Forgot Password?";
    /**
     * @uiName Register button text
     */
    this.registerLabel = "Register";
    /**
     * Redirect participants to this page to start registration.
     * @uiName Register button redirect
     * @uiWidget pageSelect
     */
    this.registerPath = "/register";
    /**
     * @uiName Heading label
     * @uiWidget textArea
     */
    this.pageLabel = "Sign in to your account";
    /**
     * Displayed when the login fails due to a network error. The participant can try refreshing the page.
     * @uiName Network error message
     */
    this.networkErrorMessage = "An error occurred while logging you in. Please refresh the page and try again.";
    withHooks(this);
  }
  disconnectedCallback() { }
  render() {
    const { states, callbacks } = isDemo()
      ? useLoginDemo(this)
      : usePortalLogin(this);
    const styles = {
      RegisterButton: {
        "&::part(label)": {
          padding: "0",
        },
      },
    };
    const sheet = createStyleSheet(styles);
    const styleString = sheet.toString();
    const content = {
      forgotPasswordButton: (h("slot", { name: "forgotPassword" },
        h("a", { part: "forgot-password-link", onClick: () => navigation.push(states.forgotPasswordPath) }, this.forgotPasswordLabel))),
      googleButton: null,
      secondaryButton: (h("slot", { name: "secondaryButton" },
        h("style", null, styleString),
        h("sl-button", { exportparts: "base: secondarybutton-base", type: "text", disabled: states.loading, onClick: () => navigation.push(states.registerPath) }, this.registerLabel))),
      emailLabel: this.emailLabel,
      passwordLabel: this.passwordLabel,
      submitLabel: this.submitLabel,
      pageLabel: this.pageLabel,
    };
    return (h(PortalLoginView, { states: states, callbacks: callbacks, content: content }));
  }
  static get is() { return "sqm-portal-login"; }
  static get encapsulation() { return "shadow"; }
  static get properties() { return {
    "nextPage": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "text": "Login redirect",
            "name": "uiName"
          }, {
            "text": "pageSelect",
            "name": "uiWidget"
          }],
        "text": "Redirect participants to this page after they successfully login."
      },
      "attribute": "next-page",
      "reflect": false,
      "defaultValue": "\"/\""
    },
    "emailLabel": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "text": "Email field label",
            "name": "uiName"
          }],
        "text": ""
      },
      "attribute": "email-label",
      "reflect": false,
      "defaultValue": "\"Email\""
    },
    "passwordLabel": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "text": "Password field label",
            "name": "uiName"
          }],
        "text": ""
      },
      "attribute": "password-label",
      "reflect": false,
      "defaultValue": "\"Password\""
    },
    "submitLabel": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "text": "Submit button text",
            "name": "uiName"
          }],
        "text": ""
      },
      "attribute": "submit-label",
      "reflect": false,
      "defaultValue": "\"Sign In\""
    },
    "forgotPasswordPath": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "text": "Forgot password redirect",
            "name": "uiName"
          }, {
            "text": "pageSelect",
            "name": "uiWidget"
          }],
        "text": "Redirect participants to this page to reset their password"
      },
      "attribute": "forgot-password-path",
      "reflect": false,
      "defaultValue": "\"/forgotPassword\""
    },
    "forgotPasswordLabel": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "text": "Forgot password text",
            "name": "uiName"
          }],
        "text": ""
      },
      "attribute": "forgot-password-label",
      "reflect": false,
      "defaultValue": "\"Forgot Password?\""
    },
    "registerLabel": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "text": "Register button text",
            "name": "uiName"
          }],
        "text": ""
      },
      "attribute": "register-label",
      "reflect": false,
      "defaultValue": "\"Register\""
    },
    "registerPath": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "text": "Register button redirect",
            "name": "uiName"
          }, {
            "text": "pageSelect",
            "name": "uiWidget"
          }],
        "text": "Redirect participants to this page to start registration."
      },
      "attribute": "register-path",
      "reflect": false,
      "defaultValue": "\"/register\""
    },
    "pageLabel": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "text": "Heading label",
            "name": "uiName"
          }, {
            "text": "textArea",
            "name": "uiWidget"
          }],
        "text": ""
      },
      "attribute": "page-label",
      "reflect": false,
      "defaultValue": "\"Sign in to your account\""
    },
    "networkErrorMessage": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "text": "Network error message",
            "name": "uiName"
          }],
        "text": "Displayed when the login fails due to a network error. The participant can try refreshing the page."
      },
      "attribute": "network-error-message",
      "reflect": false,
      "defaultValue": "\"An error occurred while logging you in. Please refresh the page and try again.\""
    },
    "demoData": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "DemoData<PortalLoginViewProps>",
        "resolved": "{ states?: { error: string; loading: boolean; forgotPasswordPath: string; registerPath: string; }; content?: { googleButton?: VNode; forgotPasswordButton?: any; secondaryButton?: any; emailLabel?: string; passwordLabel?: string; submitLabel?: string; pageLabel?: string; }; }",
        "references": {
          "DemoData": {
            "location": "import",
            "path": "../../global/demo"
          },
          "PortalLoginViewProps": {
            "location": "import",
            "path": "./sqm-portal-login-view"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [{
            "text": undefined,
            "name": "undocumented"
          }, {
            "text": "object",
            "name": "uiType"
          }],
        "text": ""
      }
    }
  }; }
  static get states() { return {
    "ignored": {}
  }; }
}
function useLoginDemo(props) {
  return deepmerge({
    states: {
      error: "",
      loading: false,
      forgotPasswordPath: "/forgotPassword",
      registerPath: "/register",
      showLoginForm: "manual",
    },
    callbacks: {
      googleSubmit: async () => {
        console.log("google submit");
      },
      submit: async (_event) => {
        console.log("submit");
      },
    },
    content: {
      googleButton: null,
    },
  }, props.demoData || {}, { arrayMerge: (_, a) => a });
}
