import {
  __decorateClass,
  __privateAdd,
  __privateGet,
  __privateSet
} from "../../chunks/chunk.QU3DSPNU.js";
var _internals;
import { property } from "lit/decorators.js";
import { GdsElement } from "../../gds-element.js";
import { watch } from "../../utils/decorators/index.js";
import "./form-request-submit-polyfill.js";
class GdsFormControlElement extends GdsElement {
  constructor() {
    super();
    __privateAdd(this, _internals);
    this.required = false;
    this.errorMessage = "";
    this.label = "";
    this.name = "";
    this.disabled = false;
    try {
      __privateSet(this, _internals, this.attachInternals());
    } catch (e) {
      __privateSet(this, _internals, {
        form: this.closest("form"),
        setFormValue: (value) => {
          this._internalValue = value;
        },
        setValidity: (validity, validationMessage) => {
          ;
          __privateGet(this, _internals).validity = validity;
          this.errorMessage = validationMessage || "";
        },
        validationMessage: "",
        validity: {
          badInput: false,
          customError: false,
          patternMismatch: false,
          rangeOverflow: false,
          rangeUnderflow: false,
          stepMismatch: false,
          tooLong: false,
          tooShort: false,
          typeMismatch: false,
          valueMissing: false,
          valid: true
        },
        willValidate: true,
        checkValidity: this.checkValidity.bind(this),
        reportValidity: this.reportValidity.bind(this)
      });
    }
  }
  connectedCallback() {
    super.connectedCallback();
    if (typeof this.attachInternals !== "function") {
      const form = this.closest("form");
      if (form) {
        form.addEventListener("submit", this._handleFormSubmit.bind(this));
        form.addEventListener("reset", this.formResetCallback.bind(this));
      }
    }
  }
  set invalid(value) {
    const oldValue = this.invalid;
    __privateGet(this, _internals).setValidity(
      {
        ...__privateGet(this, _internals).validity,
        customError: value,
        valid: !value
      },
      this.errorMessage || this.validationMessage || "   ",
      // @ts-expect-error - setValidity actually takes an element as the third argument, but the type definition is wrong.
      this._getValidityAnchor() || void 0
    );
    this.requestUpdate("invalid", oldValue);
    __privateGet(this, _internals).checkValidity();
  }
  get invalid() {
    return !__privateGet(this, _internals).validity.valid;
  }
  get value() {
    return this._internalValue;
  }
  set value(value) {
    this._internalValue = value;
    __privateGet(this, _internals).setFormValue(value);
  }
  /**
   * The form element that the form control is associated with.
   */
  get form() {
    return __privateGet(this, _internals).form;
  }
  get validity() {
    return __privateGet(this, _internals).validity;
  }
  get validationMessage() {
    return __privateGet(this, _internals).validationMessage;
  }
  get willValidate() {
    return __privateGet(this, _internals).willValidate;
  }
  checkValidity() {
    const anchor = this._getValidityAnchor();
    if (!anchor || !this.validator) return true;
    const oldValue = this.invalid;
    const validity = this.validator.validate(this) || [
      { ...this.validity, valid: true },
      ""
    ];
    this.errorMessage = validity[1] || this.errorMessage;
    __privateGet(this, _internals).setValidity(
      validity[0],
      validity[1],
      // @ts-expect-error - setValidity actually takes an element as the third argument, but the type definition is wrong.
      this._getValidityAnchor()
    );
    if (oldValue !== this.invalid) {
      this.requestUpdate("invalid", oldValue);
      this.dispatchCustomEvent("gds-validity-state", {
        detail: {
          valid: this.validity.valid,
          message: this.validationMessage
        },
        composed: true
      });
    }
    return __privateGet(this, _internals).checkValidity();
  }
  reportValidity() {
    return __privateGet(this, _internals).reportValidity();
  }
  __handleValueChange() {
    this.checkValidity();
  }
  formResetCallback() {
    if (typeof this.value === "string") this.value = "";
    else if (Array.isArray(this.value)) this.value = [];
    else this.value = void 0;
  }
  formAssociatedCallback(form) {
    form?.addEventListener("submit", this._handleFormSubmit.bind(this));
  }
  _handleFormSubmit(e) {
    this.checkValidity();
    if (!this.validity.valid) e.preventDefault();
  }
}
_internals = new WeakMap();
GdsFormControlElement.formAssociated = true;
__decorateClass([
  property({ attribute: false })
], GdsFormControlElement.prototype, "validator", 2);
__decorateClass([
  property({ type: Boolean })
], GdsFormControlElement.prototype, "required", 2);
__decorateClass([
  property({ attribute: "error-message" })
], GdsFormControlElement.prototype, "errorMessage", 2);
__decorateClass([
  property({
    type: Boolean,
    reflect: true
  })
], GdsFormControlElement.prototype, "invalid", 1);
__decorateClass([
  property()
], GdsFormControlElement.prototype, "label", 2);
__decorateClass([
  property()
], GdsFormControlElement.prototype, "value", 1);
__decorateClass([
  property({ reflect: true })
], GdsFormControlElement.prototype, "name", 2);
__decorateClass([
  property({
    type: Boolean,
    reflect: true
  })
], GdsFormControlElement.prototype, "disabled", 2);
__decorateClass([
  watch("value", { waitUntilFirstUpdate: true })
], GdsFormControlElement.prototype, "__handleValueChange", 1);
export {
  GdsFormControlElement
};
