class FormSubmitController {
  constructor(input, options) {
    this.form = null;
    this.input = input;
    this.options = {
      form: (input) => { var _a, _b; return ((_b = (_a = this.closestElement('sc-form', input)) === null || _a === void 0 ? void 0 : _a.shadowRoot) === null || _b === void 0 ? void 0 : _b.querySelector('form')) || this.closestElement('form', input); },
      name: (input) => input.name,
      value: (input) => input.value,
      disabled: (input) => input.disabled,
      ...options,
    };
    this.form = this.options.form(this.input);
    this.handleFormData = this.handleFormData.bind(this);
  }
  closestElement(selector, el) {
    if (!el)
      return null;
    return (el && el != document && el != window && el.closest(selector)) || this.closestElement(selector, el.getRootNode().host);
  }
  addFormData() {
    if (!this.form)
      return;
    this.form.addEventListener('formdata', this.handleFormData);
  }
  removeFormData() {
    if (!this.form)
      return;
    this.form.removeEventListener('formdata', this.handleFormData);
  }
  handleFormData(event) {
    const name = this.options.name(this.input);
    const value = this.options.value(this.input);
    if (typeof name === 'string' && typeof value !== 'undefined') {
      if (Array.isArray(value)) {
        value.forEach(val => {
          if (val) {
            event.formData.append(name, val.toString());
          }
        });
      }
      else {
        if (value) {
          event.formData.append(name, value.toString());
        }
      }
    }
  }
}
const parseFormData = (data) => {
  var _a;
  const { email, name, first_name, last_name, phone, password, shipping_city, shipping_country, shipping_line_1, shipping_line_2, shipping_postal_code, shipping_state, billing_city, billing_country, billing_line_1, billing_line_2, billing_postal_code, billing_state, 'tax_identifier.number_type': tax_number_type, 'tax_identifier.number': tax_number, ...rest } = data;
  const shipping_address = {
    ...(shipping_city ? { city: shipping_city } : {}),
    ...(shipping_country ? { country: shipping_country } : {}),
    ...(shipping_line_1 ? { line_1: shipping_line_1 } : {}),
    ...(shipping_line_2 ? { line_2: shipping_line_2 } : {}),
    ...(shipping_postal_code ? { postal_code: shipping_postal_code } : {}),
    ...(shipping_state ? { state: shipping_state } : {}),
  };
  const billing_address = {
    ...(billing_city ? { city: billing_city } : {}),
    ...(billing_country ? { country: billing_country } : {}),
    ...(billing_line_1 ? { line_1: billing_line_1 } : {}),
    ...(billing_line_2 ? { line_2: billing_line_2 } : {}),
    ...(billing_postal_code ? { postal_code: billing_postal_code } : {}),
    ...(billing_state ? { state: billing_state } : {}),
  };
  return {
    ...(name ? { name } : {}),
    ...(email ? { email } : {}),
    ...(first_name ? { first_name } : {}),
    ...(last_name ? { last_name } : {}),
    ...(phone ? { phone } : {}),
    ...(password ? { password } : {}),
    ...(Object.keys(shipping_address || {}).length ? { shipping_address } : {}),
    ...(Object.keys(billing_address || {}).length ? { billing_address } : {}),
    ...(tax_number_type && tax_number ? { tax_identifier: { number: tax_number, number_type: tax_number_type } } : {}),
    ...(((_a = Object.keys(rest)) === null || _a === void 0 ? void 0 : _a.length) ? { metadata: rest } : {}),
  };
};
const reportChildrenValidity = async (element) => {
  const items = [...element.shadowRoot.querySelectorAll('*')].filter((el) => typeof el.reportValidity === 'function');
  for (const el of items) {
    const isValid = await el.reportValidity();
    if (!isValid) {
      return false;
    }
  }
  return true;
};

export { FormSubmitController as F, parseFormData as p, reportChildrenValidity as r };
