'use strict';

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

const index = require('./index-9c866aeb.js');
const consumer = require('./consumer-3f5ce850.js');
const defaultI18n = require('./default-i18n-d4248548.js');
require('./index-88baf240.js');
require('./index-04dc658f.js');

const scOrderPasswordCss = ":host{display:block}.password{display:grid;gap:var(--sc-form-row-spacing, 0.75em)}";

const ScOrderPassword = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    /** The input's size. */
    this.size = 'medium';
    /** The input's value attribute. */
    this.value = '';
    /** Draws a pill-style input with rounded edges. */
    this.pill = false;
    /** Should we show the label */
    this.showLabel = true;
    /** The input's help text. */
    this.help = '';
    /** Disables the input. */
    this.disabled = false;
    /** Makes the input readonly. */
    this.readonly = false;
    /** Makes the input a required field. */
    this.required = false;
    /** The input's password confirmation attribute. */
    this.confirmation = false;
    /** Ensures strong password validation. */
    this.enableValidation = true;
  }
  async reportValidity() {
    var _a, _b, _c, _d, _e, _f, _g, _h;
    if (this.loggedIn)
      return true;
    (_b = (_a = this.input) === null || _a === void 0 ? void 0 : _a.setCustomValidity) === null || _b === void 0 ? void 0 : _b.call(_a, '');
    (_d = (_c = this.confirmInput) === null || _c === void 0 ? void 0 : _c.setCustomValidity) === null || _d === void 0 ? void 0 : _d.call(_c, '');
    // confirmation is enabled.
    if (this.confirmation) {
      if (((_e = this.confirmInput) === null || _e === void 0 ? void 0 : _e.value) && ((_f = this.input) === null || _f === void 0 ? void 0 : _f.value) !== ((_g = this.confirmInput) === null || _g === void 0 ? void 0 : _g.value)) {
        this.confirmInput.setCustomValidity(defaultI18n.__('Password does not match.', 'surecart'));
      }
    }
    if (this.enableValidation) {
      const validPassword = this.validatePassword((_h = this.input) === null || _h === void 0 ? void 0 : _h.value);
      if (!validPassword) {
        this.input.setCustomValidity(defaultI18n.__('Passwords should at least 6 characters and contain one special character.', 'surecart'));
      }
    }
    const valid = await this.input.reportValidity();
    if (!valid) {
      return false;
    }
    if (this.confirmInput) {
      return this.confirmInput.reportValidity();
    }
    return valid;
  }
  validatePassword(password) {
    const regex = new RegExp('^(?=.*?[#?!@$%^&*-]).{6,}$');
    if (regex.test(password))
      return true;
    return false;
  }
  render() {
    var _a;
    if (this.loggedIn) {
      return index.h(index.Host, { style: { display: 'none' } });
    }
    // if (this.emailExists) {
    //   return <Host style={{ display: 'none' }}></Host>;
    // }
    return (index.h("div", { class: "password" }, index.h("sc-input", { ref: el => (this.input = el), label: this.label, help: this.help, autofocus: this.autofocus, placeholder: this.placeholder, showLabel: this.showLabel, size: this.size ? this.size : 'medium', type: "password", name: "password", value: this.value, required: this.required, disabled: this.disabled }), this.confirmation && (index.h("sc-input", { ref: el => (this.confirmInput = el), label: (_a = this.confirmationLabel) !== null && _a !== void 0 ? _a : defaultI18n.__('Confirm Password', 'surecart'), help: this.confirmationHelp, placeholder: this.confirmationPlaceholder, size: this.size ? this.size : 'medium', type: "password", value: this.value, required: this.required, disabled: this.disabled }))));
  }
};
consumer.openWormhole(ScOrderPassword, ['loggedIn', 'emailExists'], false);
ScOrderPassword.style = scOrderPasswordCss;

exports.sc_order_password = ScOrderPassword;
