import { s as m } from "./dom.0RpWet6y.js";
const n = /* @__PURE__ */ new WeakMap(), h = /* @__PURE__ */ new WeakMap(), l = /* @__PURE__ */ new WeakMap(), c = /* @__PURE__ */ new WeakSet(), f = /* @__PURE__ */ new WeakMap();
class v {
  constructor(e, s) {
    this.handleFormData = (t) => {
      const o = this.options.disabled(this.host), r = this.options.name(this.host), i = this.options.value(this.host), a = this.host.tagName.toLowerCase() === "cx-button";
      this.host.isConnected && !o && !a && typeof r == "string" && r.length > 0 && typeof i < "u" && (Array.isArray(i) ? i.forEach((d) => {
        t.formData.append(
          r,
          d.toString()
        );
      }) : t.formData.append(
        r,
        i.toString()
      ));
    }, this.handleFormSubmit = (t) => {
      const o = this.options.disabled(this.host), r = this.options.checkValidity;
      if (!this.form)
        return;
      const i = n.get(this.form);
      if (!i)
        return;
      const a = m(Array.from(i));
      this.form && !this.form.noValidate && a.forEach((d) => {
        this.setUserInteracted(d, !0);
      }), this.form && !this.form.noValidate && !o && !r(this.host) && (t.preventDefault(), t.stopImmediatePropagation());
    }, this.handleFormReset = () => {
      this.options.setValue(this.host, this.options.defaultValue(this.host)), this.setUserInteracted(this.host, !1), f.set(this.host, []);
    }, this.handleInteraction = (t) => {
      const o = f.get(this.host);
      o.includes(t.type) || o.push(t.type), o.length === this.options.assumeInteractionOn.length && this.setUserInteracted(this.host, !0);
    }, this.checkFormValidity = () => {
      if (this.form && !this.form.noValidate) {
        const t = this.form.querySelectorAll("*"), o = n.get(this.form), r = o ? m(Array.from(o)) : [], i = [
          .../* @__PURE__ */ new Set([...r, ...t])
        ];
        for (const a of i)
          if (typeof a.checkValidity == "function" && !a.checkValidity())
            return !1;
      }
      return !0;
    }, this.reportFormValidity = () => {
      if (this.form && !this.form.noValidate) {
        const t = this.form.querySelectorAll("*"), o = n.get(this.form), r = o ? m(Array.from(o)) : [], i = [
          .../* @__PURE__ */ new Set([...r, ...t])
        ];
        for (const a of i)
          if (typeof a.reportValidity == "function" && !a.reportValidity())
            return !1;
      }
      return !0;
    }, (this.host = e).addController(this), this.options = {
      assumeInteractionOn: ["cx-input"],
      checkValidity: (t) => typeof t.checkValidity == "function" ? t.checkValidity() : !0,
      defaultValue: (t) => t.defaultValue,
      disabled: (t) => t.disabled ?? !1,
      form: (t) => {
        if (t.form && t.form instanceof HTMLFormElement)
          return t.form;
        const o = t.form;
        if (o) {
          const i = t.getRootNode().querySelector(`#${o}`);
          if (i)
            return i;
        }
        return t.closest("form");
      },
      name: (t) => t.name,
      reportValidity: (t) => typeof t.reportValidity == "function" ? t.reportValidity() : !0,
      // eslint-disable-next-line
      // @ts-ignore
      setValue: (t, o) => t.value = o,
      value: (t) => t.value,
      ...s
    };
  }
  hostConnected() {
    const e = this.options.form(this.host);
    e && this.attachForm(e), f.set(this.host, []), this.options.assumeInteractionOn.forEach((s) => {
      this.host.addEventListener(s, this.handleInteraction);
    });
  }
  hostDisconnected() {
    this.detachForm(), f.delete(this.host), this.options.assumeInteractionOn.forEach((e) => {
      this.host.removeEventListener(e, this.handleInteraction);
    });
  }
  hostUpdated() {
    const e = this.options.form(this.host);
    e || this.detachForm(), e && this.form !== e && (this.detachForm(), this.attachForm(e)), this.host.hasUpdated && this.setValidity(this.host.validity.valid);
  }
  attachForm(e) {
    e ? (this.form = e, n.has(this.form) ? n.get(this.form).add(this.host) : n.set(this.form, /* @__PURE__ */ new Set([this.host])), this.form.addEventListener("formdata", this.handleFormData), this.form.addEventListener("submit", this.handleFormSubmit), this.form.addEventListener("reset", this.handleFormReset), h.has(this.form) || (h.set(this.form, this.form.reportValidity), this.form.reportValidity = () => this.reportFormValidity()), l.has(this.form) || (l.set(this.form, this.form.checkValidity), this.form.checkValidity = () => this.checkFormValidity())) : this.form = void 0;
  }
  detachForm() {
    if (!this.form)
      return;
    const e = n.get(this.form);
    e && (e.delete(this.host), this.form.removeEventListener("formdata", this.handleFormData), this.form.removeEventListener("submit", this.handleFormSubmit), this.form.removeEventListener("reset", this.handleFormReset), e.size <= 0 && (h.has(this.form) && (this.form.reportValidity = h.get(this.form), h.delete(this.form)), l.has(this.form) && (this.form.checkValidity = l.get(this.form), l.delete(this.form)), this.form = void 0));
  }
  setUserInteracted(e, s) {
    s ? c.add(e) : c.delete(e), e.requestUpdate();
  }
  doAction(e, s) {
    if (this.form) {
      const t = document.createElement("button");
      t.type = e, t.style.position = "absolute", t.style.width = "0", t.style.height = "0", t.style.clipPath = "inset(50%)", t.style.overflow = "hidden", t.style.whiteSpace = "nowrap", s && (t.name = s.name, t.value = s.value, [
        "formaction",
        "formenctype",
        "formmethod",
        "formnovalidate",
        "formtarget"
      ].forEach((o) => {
        s.hasAttribute(o) && t.setAttribute(o, s.getAttribute(o));
      })), this.form.append(t), t.click(), t.remove();
    }
  }
  /** Returns the associated `<form>` element, if one exists. */
  getForm() {
    return this.form ?? null;
  }
  /** Resets the form, restoring all the control to their default value */
  reset(e) {
    this.doAction("reset", e);
  }
  /** Submits the form, triggering validation and form data injection. */
  submit(e) {
    this.doAction("submit", e);
  }
  /**
   * Synchronously sets the form control's validity. Call this when you know the future validity but need to update
   * the host element immediately, i.e. before Lit updates the component in the next update.
   */
  setValidity(e) {
    const s = this.host, t = !!c.has(s), o = !!s.required;
    s.toggleAttribute("data-required", o), s.toggleAttribute("data-optional", !o), s.toggleAttribute("data-invalid", !e), s.toggleAttribute("data-valid", e), s.toggleAttribute("data-user-invalid", !e && t), s.toggleAttribute("data-user-valid", e && t);
  }
  /**
   * Updates the form control's validity based on the current value of `host.validity.valid`. Call this when anything
   * that affects constraint validation changes so the component receives the correct validity states.
   */
  updateValidity() {
    const e = this.host;
    this.setValidity(e.validity.valid);
  }
  /**
   * Dispatches a non-bubbling, cancelable custom event of type `cx-invalid`.
   * If the `cx-invalid` event will be cancelled then the original `invalid`
   * event (which may have been passed as argument) will also be cancelled.
   * If no original `invalid` event has been passed then the `cx-invalid`
   * event will be cancelled before being dispatched.
   */
  emitInvalidEvent(e) {
    const s = new CustomEvent(
      "cx-invalid",
      {
        bubbles: !1,
        cancelable: !0,
        composed: !1,
        detail: {}
      }
    );
    e || s.preventDefault(), this.host.dispatchEvent(s) || e?.preventDefault();
  }
}
const u = Object.freeze({
  badInput: !1,
  customError: !1,
  patternMismatch: !1,
  rangeOverflow: !1,
  rangeUnderflow: !1,
  stepMismatch: !1,
  tooLong: !1,
  tooShort: !1,
  typeMismatch: !1,
  valid: !0,
  valueMissing: !1
}), g = Object.freeze({
  ...u,
  valid: !1,
  valueMissing: !0
}), V = Object.freeze({
  ...u,
  customError: !0,
  valid: !1
});
export {
  v as F,
  g as a,
  V as c,
  n as f,
  u as v
};
