import { proxyCustomElement, HTMLElement, createEvent, h, Host } from '@stencil/core/internal/client';
import { _ as __ } from './default-i18n.js';
import './index2.js';
import { a as apiFetch } from './fetch.js';
import { d as defineCustomElement$9 } from './sc-alert2.js';
import { d as defineCustomElement$8 } from './sc-button2.js';
import { d as defineCustomElement$7 } from './sc-dialog2.js';
import { d as defineCustomElement$6 } from './sc-form2.js';
import { d as defineCustomElement$5 } from './sc-form-control2.js';
import { d as defineCustomElement$4 } from './sc-icon2.js';
import { d as defineCustomElement$3 } from './sc-input2.js';
import { d as defineCustomElement$2 } from './sc-spinner2.js';
import { d as defineCustomElement$1 } from './sc-tooltip2.js';

const scLoginProviderCss = ":host{display:block}";

const ScLoginProvider = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.__attachShadow();
    this.scSetLoggedIn = createEvent(this, "scSetLoggedIn", 7);
    this.scSetCustomer = createEvent(this, "scSetCustomer", 7);
  }
  /** Listen for open event. */
  handleLoginPrompt() {
    this.open = true;
  }
  /** Focus on first input. */
  handleLoginDialogChange(val) {
    if (val) {
      setTimeout(() => {
        this.loginForm.querySelector('sc-input').triggerFocus();
      }, 100);
    }
  }
  handleLoggedInChange(val, prev) {
    if (prev === false && val) {
      this.notice = true;
    }
  }
  handleOrderChange(val, prev) {
    if ((val === null || val === void 0 ? void 0 : val.updated_at) !== (prev === null || prev === void 0 ? void 0 : prev.updated_at)) {
      this.notice = false;
    }
  }
  /** Handle form submit. */
  async handleFormSubmit(e) {
    e.preventDefault();
    e.stopImmediatePropagation();
    this.error = null;
    const { login, password } = await e.target.getFormJson();
    try {
      this.loading = true;
      const { name, email } = (await apiFetch({
        method: 'POST',
        path: 'surecart/v1/login',
        data: {
          login,
          password,
        },
      }));
      this.scSetLoggedIn.emit(true);
      this.scSetCustomer.emit({ name, email });
      this.open = false;
    }
    catch (e) {
      console.error(e);
      this.error = (e === null || e === void 0 ? void 0 : e.message) || __('Something went wrong', 'surecart');
    }
    finally {
      this.loading = false;
    }
  }
  render() {
    return (h(Host, null, !!this.notice && (h("sc-alert", { type: "success", open: true, style: { marginBottom: 'var(--sc-form-row-spacing)' }, closable: true }, h("span", { slot: "title" }, __('Welcome back!', 'surecart')), __('You have logged in successfully.', 'surecart'))), h("slot", null), !this.loggedIn && (h("sc-dialog", { label: __('Login to your account', 'surecart'), open: this.open, onScRequestClose: () => (this.open = false) }, h("sc-form", { ref: el => (this.loginForm = el), onScFormSubmit: e => {
        e.preventDefault();
        e.stopImmediatePropagation();
      }, onScSubmit: e => this.handleFormSubmit(e) }, !!this.error && (h("sc-alert", { type: "danger", open: !!this.error }, this.error)), h("sc-input", { label: __('Email or Username', 'surecart'), type: "text", name: "login", required: true, autofocus: this.open }), h("sc-input", { label: __('Password', 'surecart'), type: "password", name: "password", required: true }), h("sc-button", { type: "primary", full: true, loading: this.loading, submit: true }, __('Login', 'surecart')))))));
  }
  static get watchers() { return {
    "open": ["handleLoginDialogChange"],
    "loggedIn": ["handleLoggedInChange"],
    "order": ["handleOrderChange"]
  }; }
  static get style() { return scLoginProviderCss; }
}, [1, "sc-login-provider", {
    "loggedIn": [4, "logged-in"],
    "order": [16],
    "notice": [32],
    "open": [32],
    "loading": [32],
    "error": [32]
  }, [[0, "scLoginPrompt", "handleLoginPrompt"]]]);
function defineCustomElement() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["sc-login-provider", "sc-alert", "sc-button", "sc-dialog", "sc-form", "sc-form-control", "sc-icon", "sc-input", "sc-spinner", "sc-tooltip"];
  components.forEach(tagName => { switch (tagName) {
    case "sc-login-provider":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, ScLoginProvider);
      }
      break;
    case "sc-alert":
      if (!customElements.get(tagName)) {
        defineCustomElement$9();
      }
      break;
    case "sc-button":
      if (!customElements.get(tagName)) {
        defineCustomElement$8();
      }
      break;
    case "sc-dialog":
      if (!customElements.get(tagName)) {
        defineCustomElement$7();
      }
      break;
    case "sc-form":
      if (!customElements.get(tagName)) {
        defineCustomElement$6();
      }
      break;
    case "sc-form-control":
      if (!customElements.get(tagName)) {
        defineCustomElement$5();
      }
      break;
    case "sc-icon":
      if (!customElements.get(tagName)) {
        defineCustomElement$4();
      }
      break;
    case "sc-input":
      if (!customElements.get(tagName)) {
        defineCustomElement$3();
      }
      break;
    case "sc-spinner":
      if (!customElements.get(tagName)) {
        defineCustomElement$2();
      }
      break;
    case "sc-tooltip":
      if (!customElements.get(tagName)) {
        defineCustomElement$1();
      }
      break;
  } });
}
defineCustomElement();

export { ScLoginProvider as S, defineCustomElement as d };
