'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-9c866aeb.js');
const fetch = require('./fetch-02d6cb7b.js');
const defaultI18n = require('./default-i18n-d4248548.js');
require('./index-88baf240.js');
require('./add-query-args-17c551b6.js');
require('./index-04dc658f.js');

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

const ScPasswordNag = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    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 fetch.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) || defaultI18n.__('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: defaultI18n.__('Passwords do not match.', 'surecart') };
      }
      if (this.enableValidation && !this.validatePassword(password)) {
        throw { message: defaultI18n.__('Passwords should at least 6 characters and contain one special character.', 'surecart') };
      }
      await fetch.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) || defaultI18n.__('Something went wrong', 'surecart');
      this.loading = false;
    }
  }
  render() {
    if (this.success) {
      return (index.h("sc-alert", { type: "success", open: true }, index.h("span", { slot: "title" }, defaultI18n.__('Succcess!', 'surecart')), defaultI18n.__('You have successfully set your password.', 'surecart')));
    }
    return (index.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 ? (index.h("sc-dashboard-module", { class: "customer-details" }, index.h("span", { slot: "heading" }, defaultI18n.__('Set A Password', 'surecart'), " "), index.h("sc-button", { type: "text", size: "small", slot: "end", onClick: () => (this.set = false) }, index.h("sc-icon", { name: "x", slot: "prefix" }), defaultI18n.__('Cancel', 'surecart')), index.h("sc-card", null, index.h("sc-form", { onScFormSubmit: e => this.handleSubmit(e) }, index.h("sc-input", { label: defaultI18n.__('New Password', 'surecart'), name: "password", type: "password", required: true, ref: el => (this.input = el) }), index.h("sc-input", { label: defaultI18n.__('Confirm New Password', 'surecart'), name: "password_confirm", type: "password", required: true }), index.h("div", null, index.h("sc-button", { type: "primary", full: true, submit: true, busy: this.loading }, defaultI18n.__('Update Password', 'surecart'))))))) : (index.h(index.Fragment, null, index.h("slot", { name: "title", slot: "title" }, defaultI18n.__('Reminder', 'surecart')), index.h("slot", null, defaultI18n.__('You have not yet set a password. Please set a password for your account.', 'surecart')), index.h("sc-flex", { "justify-content": "flex-start" }, index.h("sc-button", { size: "small", type: "primary", onClick: () => (this.set = true) }, defaultI18n.__('Set A Password', 'surecart')), index.h("sc-button", { size: "small", type: "text", onClick: () => this.dismiss() }, defaultI18n.__('Dismiss', 'surecart'))))), this.loading && index.h("sc-block-ui", { spinner: true })));
  }
  static get watchers() { return {
    "set": ["handleSetChange"]
  }; }
};
ScPasswordNag.style = scPasswordNagCss;

exports.sc_password_nag = ScPasswordNag;
