import { proxyCustomElement, HTMLElement, h, Fragment } from '@stencil/core/internal/client';
import { a as apiFetch } from './fetch.js';
import { _ as __ } from './default-i18n.js';
import './index2.js';
import { d as defineCustomElement$d } from './sc-alert2.js';
import { d as defineCustomElement$c } from './sc-block-ui2.js';
import { d as defineCustomElement$b } from './sc-button2.js';
import { d as defineCustomElement$a } from './sc-card2.js';
import { d as defineCustomElement$9 } from './sc-dashboard-module2.js';
import { d as defineCustomElement$8 } from './sc-flex2.js';
import { d as defineCustomElement$7 } from './sc-form2.js';
import { d as defineCustomElement$6 } from './sc-form-control2.js';
import { d as defineCustomElement$5 } from './sc-icon2.js';
import { d as defineCustomElement$4 } from './sc-input2.js';
import { d as defineCustomElement$3 } from './sc-spinner2.js';
import { d as defineCustomElement$2 } from './sc-tooltip2.js';

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

const ScPasswordNag$1 = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.__attachShadow();
    this.open = true;
    /** The type of alert. */
    this.type = 'primary';
    /** Ensures strong password validation. */
    this.enableValidation = true;
  }
  handleSetChange() {
    setTimeout(() => {
      this.input && this.input.triggerFocus();
    }, 50);
  }
  /** Dismiss the form. */
  async dismiss() {
    this.loading = true;
    this.error = '';
    try {
      await apiFetch({
        path: `wp/v2/users/me`,
        method: 'PATCH',
        data: {
          meta: {
            default_password_nag: false,
          },
        },
      });
      this.open = false;
    }
    catch (e) {
      this.error = (e === null || e === void 0 ? void 0 : e.message) || __('Something went wrong', 'surecart');
      this.loading = false;
    }
  }
  validatePassword(password) {
    const regex = new RegExp('^(?=.*?[#?!@$%^&*-]).{6,}$');
    if (regex.test(password))
      return true;
    return false;
  }
  /** Handle password submit. */
  async handleSubmit(e) {
    this.loading = true;
    this.error = '';
    try {
      const { password, password_confirm } = await e.target.getFormJson();
      if (password !== password_confirm) {
        throw { message: __('Passwords do not match.', 'surecart') };
      }
      if (this.enableValidation && !this.validatePassword(password)) {
        throw { message: __('Passwords should at least 6 characters and contain one special character.', 'surecart') };
      }
      await apiFetch({
        path: `wp/v2/users/me`,
        method: 'PATCH',
        data: {
          password,
          meta: {
            default_password_nag: false,
          },
        },
      });
      this.dismiss();
      this.success = true;
    }
    catch (e) {
      this.error = (e === null || e === void 0 ? void 0 : e.message) || __('Something went wrong', 'surecart');
      this.loading = false;
    }
  }
  render() {
    if (this.success) {
      return (h("sc-alert", { type: "success", open: true }, h("span", { slot: "title" }, __('Succcess!', 'surecart')), __('You have successfully set your password.', 'surecart')));
    }
    return (h("sc-alert", { type: this.type, open: this.open, exportparts: "base, icon, text, title, message, close-icon", style: { position: 'relative' } }, !!this.error && this.error, this.set ? (h("sc-dashboard-module", { class: "customer-details" }, h("span", { slot: "heading" }, __('Set A Password', 'surecart'), " "), h("sc-button", { type: "text", size: "small", slot: "end", onClick: () => (this.set = false) }, h("sc-icon", { name: "x", slot: "prefix" }), __('Cancel', 'surecart')), h("sc-card", null, h("sc-form", { onScFormSubmit: e => this.handleSubmit(e) }, h("sc-input", { label: __('New Password', 'surecart'), name: "password", type: "password", required: true, ref: el => (this.input = el) }), h("sc-input", { label: __('Confirm New Password', 'surecart'), name: "password_confirm", type: "password", required: true }), h("div", null, h("sc-button", { type: "primary", full: true, submit: true, busy: this.loading }, __('Update Password', 'surecart'))))))) : (h(Fragment, null, h("slot", { name: "title", slot: "title" }, __('Reminder', 'surecart')), h("slot", null, __('You have not yet set a password. Please set a password for your account.', 'surecart')), h("sc-flex", { "justify-content": "flex-start" }, h("sc-button", { size: "small", type: "primary", onClick: () => (this.set = true) }, __('Set A Password', 'surecart')), h("sc-button", { size: "small", type: "text", onClick: () => this.dismiss() }, __('Dismiss', 'surecart'))))), this.loading && h("sc-block-ui", { spinner: true })));
  }
  static get watchers() { return {
    "set": ["handleSetChange"]
  }; }
  static get style() { return scPasswordNagCss; }
}, [1, "sc-password-nag", {
    "open": [1028],
    "type": [513],
    "successUrl": [1, "success-url"],
    "enableValidation": [516, "enable-validation"],
    "set": [32],
    "loading": [32],
    "error": [32],
    "success": [32]
  }]);
function defineCustomElement$1() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["sc-password-nag", "sc-alert", "sc-block-ui", "sc-button", "sc-card", "sc-dashboard-module", "sc-flex", "sc-form", "sc-form-control", "sc-icon", "sc-input", "sc-spinner", "sc-tooltip"];
  components.forEach(tagName => { switch (tagName) {
    case "sc-password-nag":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, ScPasswordNag$1);
      }
      break;
    case "sc-alert":
      if (!customElements.get(tagName)) {
        defineCustomElement$d();
      }
      break;
    case "sc-block-ui":
      if (!customElements.get(tagName)) {
        defineCustomElement$c();
      }
      break;
    case "sc-button":
      if (!customElements.get(tagName)) {
        defineCustomElement$b();
      }
      break;
    case "sc-card":
      if (!customElements.get(tagName)) {
        defineCustomElement$a();
      }
      break;
    case "sc-dashboard-module":
      if (!customElements.get(tagName)) {
        defineCustomElement$9();
      }
      break;
    case "sc-flex":
      if (!customElements.get(tagName)) {
        defineCustomElement$8();
      }
      break;
    case "sc-form":
      if (!customElements.get(tagName)) {
        defineCustomElement$7();
      }
      break;
    case "sc-form-control":
      if (!customElements.get(tagName)) {
        defineCustomElement$6();
      }
      break;
    case "sc-icon":
      if (!customElements.get(tagName)) {
        defineCustomElement$5();
      }
      break;
    case "sc-input":
      if (!customElements.get(tagName)) {
        defineCustomElement$4();
      }
      break;
    case "sc-spinner":
      if (!customElements.get(tagName)) {
        defineCustomElement$3();
      }
      break;
    case "sc-tooltip":
      if (!customElements.get(tagName)) {
        defineCustomElement$2();
      }
      break;
  } });
}
defineCustomElement$1();

const ScPasswordNag = ScPasswordNag$1;
const defineCustomElement = defineCustomElement$1;

export { ScPasswordNag, defineCustomElement };
