var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { Component, Host, h, Element, Method, Prop, Watch, Event } from '@stencil/core';
import ChangeManager from '../../helpers/change-manager/change-manager';
import { FORM_ELEMENT_VALIDATE } from '../../helpers/change-manager/subject/static-subject/static-subject-title';
import { sanitizeVegaComponent } from '../../utils/component';
import { InjectVegaGlobalSlimmer, InjectVegaSlimmer, methodPlaceholder, } from 'vega-slimmer/core';
import { createEventEmitSlimmer, } from '../../helpers/event-manager/slimmers/event-emit-slimmer';
import { ChildNodesEventPreventSlimmer } from '../../helpers/event-manager/slimmers/child-nodes-event-prevent-slimmer';
import { VegaFormReset, VegaFormSubmit, VegaValidate, } from '../../helpers/event-manager/event-id/vega-event-id';
import { VegaComponentUsageRuntimeMetricsSlimmer } from '../../helpers/slimmers/component-usage-runtime-metrics';
import { createSubStateNotifySlimmer } from '../../helpers/slimmers/sub-state/sub-state-notify-slimmer';
import { VegaFormNestStateController } from './slimmers/controllers/vega-form-nest-state-controller';
import { VegaFormFieldValidationStateController } from './slimmers/controllers/vega-form-field-validation-state-controller';
import { NativeFormRenderer } from './slimmers/renderers/native-form-renderer';
import { VegaFormSubmitController } from './slimmers/controllers/vega-form-submit-controller';
import { VegaFormFieldController } from './slimmers/controllers/vega-form-field-controller';
import { VegaFormResetController } from './slimmers/controllers/vega-form-reset-controller';
/**
 * @vegaVersion 1.3.0
 */
export class VegaForm {
  constructor() {
    this.globalSlimmers = {};
    this.submitEventEmitter = createEventEmitSlimmer(VegaForm, VegaFormSubmit);
    this.resetEventEmitter = createEventEmitSlimmer(VegaForm, VegaFormReset);
    this.validateEventEmitter = createEventEmitSlimmer(VegaForm, VegaValidate);
    this.disableBlurValidationNotifySlimmer = createSubStateNotifySlimmer(VegaForm, 'disableBlurValidation', 'watchDisableBlurValidation');
    this.stateController = new VegaFormNestStateController();
    this.childEventPrevent = new ChildNodesEventPreventSlimmer([VegaFormSubmit, VegaFormReset], () => this.host.querySelector('vega-form'), {
      customPreventHandle: (e) => {
        e.stopPropagation();
      },
    });
    this.childFormValidateEventPrevent = new ChildNodesEventPreventSlimmer([VegaValidate], () => this.host, {
      customPreventHandle: (e) => {
        e.stopPropagation();
      },
    });
    this.vegaComponentUsageRuntimeMetricsSlimmer = new VegaComponentUsageRuntimeMetricsSlimmer();
    this.vegaFormFieldValidationStateController = new VegaFormFieldValidationStateController();
    this.nativeFormRenderer = new NativeFormRenderer();
    this.vegaFormSubmitController = new VegaFormSubmitController();
    this.vegaFormResetController = new VegaFormResetController();
    this.vegaFormFieldController = new VegaFormFieldController();
    /**
     * Indicates whether the current form-controlled elements are valid.
     *
     * Form-controlled elements are considered for validation if they
     *    have the `data-vega-form` property.
     *
     * Disabled and invisible elements are excluded from validation.
     *
     * If the value is `undefined`, it means there are no
     *    form-controlled elements that can be validated.
     *
     * **Additional Notes:**
     *   1. When depending on `isValid` for form validation, it is advised
     *      not to modify the following properties of the monitored DOM:
     *      - `disabled` property
     *      - Element visibility style
     *   2. As this property relies heavily on the `VegaValidate` event emitted,
     *      it is highly recommended to enable auto-validation for all
     *      form-controlled elements to ensure that the value reflects
     *      the most up-to-date form validation status.
     *
     * @vegaVersion 1.3.0
     */
    /* eslint-disable-next-line @stencil/strict-mutable */
    this.isValid = null;
    /**
     * Indicates whether the value of any current
     *    form-controlled elements has been changed.
     *
     *   1. When the value of any form-controlled element is changed,
     *      this parameter is set to `true`.
     *   2. When resetting the form, this parameter is set to `false`.
     *   3. When invoking the `setValue` method, this parameter is set to `false`.
     *
     * @vegaVersion 1.3.0
     */
    /* eslint-disable-next-line @stencil/strict-mutable */
    this.isTouched = false;
    /**
     * Specifies whether form validation should be disabled on blur events of form elements.
     *
     *  - This helps prevent validation on blur when clicking Submit inside an input,
     *        avoiding error messages that shift the button and block the click.
     *  - For nested forms, the parent form will override this property
     *        to ensure consistent validation behavior.
     *
     * @vegaVersion 2.47.0
     */
    /* eslint-disable-next-line @stencil/strict-mutable */
    this.disableBlurValidation = false;
    /**
     * Determines whether the `vega-form` content is wrapped in a native HTML <form> element.
     *
     * @vegaVersion 2.64.0
     */
    this.useNativeForm = false;
    /**
     * Specifies the HTTP method used for form submission.
     *   This property takes effect only when {@link useNativeForm} is set to `true`.
     *
     * @vegaVersion 2.64.0
     */
    this.method = 'get';
  }
  watchIsValid() {
    this.validateEventEmitter.emit(this.isValid);
    ChangeManager.notify(FORM_ELEMENT_VALIDATE, { host: this.host, detail: this.isValid });
  }
  watchDisableBlurValidation() {
    methodPlaceholder();
  }
  /**
   * Invoke this method asynchronously to retrieve the validation result for
   *    all the vega field elements nested within the `vega-form`.
   *
   * If an element is a `vega-form` itself, it will recursively
   *    invoke its `valid()` method.
   *
   * @param {boolean | 'rule'} showError determine whether the validation result should be reflected in UI
   * @return {Promise<FormValidateResult> } validation result
   * @vegaVersion 1.3.0
   */
  async valid(showError = true) {
    return methodPlaceholder(showError);
  }
  /**
   * Invoke this method asynchronously to retrieve the values for
   *    all the available vega field elements nested within the `vega-form`.
   *
   * @param {FormControlOption} [option] form control option to skip disabled/invisible components
   * @return {Promise<T>} object with key and field value as key-value pair
   * @vegaVersion 1.3.0
   */
  async getValue(option) {
    return methodPlaceholder(option);
  }
  /**
   * Invoke this method to prefill the `vega-form` with the given value.
   *
   * This action will also set the `isTouched` property to `false`.
   *
   * @param value to prefill into the vega-form
   * @param {FormControlOption} [option] form control option to skip disabled/invisible components
   * @vegaVersion 1.3.0
   */
  async setValue(value, option) {
    methodPlaceholder(value, option);
  }
  /**
   * Invoke this method to reset the vega field elements in the form to
   *    the default value provided, or an empty string if
   *    no default value is found.
   *
   * @param defaultValueMap map of the vega form field name to default value
   * @param {FormControlOption} [option] form control option to skip disabled/invisible components
   *
   * @vegaVersion 1.3.0
   */
  async reset(defaultValueMap = {}, option) {
    methodPlaceholder(defaultValueMap, option);
  }
  /**
   * Invoke this method to set validation states on specific form fields.
   *
   * @param {VegaFormFieldValidationState[]} states - An array of validation error objects.
   *
   * @vegaVersion 2.59.0
   */
  async setValidationStates(states) {
    methodPlaceholder(states);
  }
  render() {
    return sanitizeVegaComponent(h(Host, null, this.useNativeForm ? this.nativeFormRenderer.render() : h("slot", null)), this.host);
  }
  static get is() { return "vega-form"; }
  static get encapsulation() { return "shadow"; }
  static get originalStyleUrls() { return {
    "$": ["vega-form.scss"]
  }; }
  static get styleUrls() { return {
    "$": ["vega-form.css"]
  }; }
  static get properties() { return {
    "isValid": {
      "type": "boolean",
      "mutable": true,
      "complexType": {
        "original": "boolean | null",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1.3.0"
          }],
        "text": "Indicates whether the current form-controlled elements are valid.\n\nForm-controlled elements are considered for validation if they\n   have the `data-vega-form` property.\n\nDisabled and invisible elements are excluded from validation.\n\nIf the value is `undefined`, it means there are no\n   form-controlled elements that can be validated.\n\n**Additional Notes:**\n  1. When depending on `isValid` for form validation, it is advised\n     not to modify the following properties of the monitored DOM:\n     - `disabled` property\n     - Element visibility style\n  2. As this property relies heavily on the `VegaValidate` event emitted,\n     it is highly recommended to enable auto-validation for all\n     form-controlled elements to ensure that the value reflects\n     the most up-to-date form validation status."
      },
      "attribute": "is-valid",
      "reflect": false,
      "defaultValue": "null"
    },
    "isTouched": {
      "type": "boolean",
      "mutable": true,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1.3.0"
          }],
        "text": "Indicates whether the value of any current\n   form-controlled elements has been changed.\n\n  1. When the value of any form-controlled element is changed,\n     this parameter is set to `true`.\n  2. When resetting the form, this parameter is set to `false`.\n  3. When invoking the `setValue` method, this parameter is set to `false`."
      },
      "attribute": "is-touched",
      "reflect": false,
      "defaultValue": "false"
    },
    "disableBlurValidation": {
      "type": "boolean",
      "mutable": true,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "2.47.0"
          }],
        "text": "Specifies whether form validation should be disabled on blur events of form elements.\n\n - This helps prevent validation on blur when clicking Submit inside an input,\n       avoiding error messages that shift the button and block the click.\n - For nested forms, the parent form will override this property\n       to ensure consistent validation behavior."
      },
      "attribute": "disable-blur-validation",
      "reflect": false,
      "defaultValue": "false"
    },
    "useNativeForm": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "2.64.0"
          }],
        "text": "Determines whether the `vega-form` content is wrapped in a native HTML <form> element."
      },
      "attribute": "use-native-form",
      "reflect": false,
      "defaultValue": "false"
    },
    "action": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "2.64.0"
          }],
        "text": "Specifies the HTTP URL for form submission.\n   This property takes effect only when {@link useNativeForm} is set to `true`."
      },
      "attribute": "action",
      "reflect": false
    },
    "method": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "'post' | 'get'",
        "resolved": "\"get\" | \"post\"",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "2.64.0"
          }],
        "text": "Specifies the HTTP method used for form submission.\n  This property takes effect only when {@link useNativeForm} is set to `true`."
      },
      "attribute": "method",
      "reflect": false,
      "defaultValue": "'get'"
    }
  }; }
  static get events() { return [{
      "method": "vegaValidate",
      "name": "vegaValidate",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1.9.1"
          }],
        "text": "An event emitter notifying the change in the validation result."
      },
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      }
    }, {
      "method": "validate",
      "name": "validate",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [{
            "name": "eventSemantics",
            "text": "namespace:native"
          }, {
            "name": "vegaVersion",
            "text": "2.0.0"
          }],
        "text": "An event emitter notifying the change in the validation result."
      },
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      }
    }, {
      "method": "vegaFormSubmit",
      "name": "vegaFormSubmit",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1.24.0"
          }],
        "text": "An event emitter notifying the submission of the form."
      },
      "complexType": {
        "original": "unknown",
        "resolved": "unknown",
        "references": {}
      }
    }, {
      "method": "formSubmit",
      "name": "formSubmit",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [{
            "name": "eventSemantics",
            "text": "namespace:native"
          }, {
            "name": "vegaVersion",
            "text": "2.0.0"
          }],
        "text": "An event emitter notifying the submission of the form."
      },
      "complexType": {
        "original": "unknown",
        "resolved": "unknown",
        "references": {}
      }
    }, {
      "method": "vegaFormReset",
      "name": "vegaFormReset",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1.24.0"
          }],
        "text": "An event emitter notifying the reset of the form."
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }, {
      "method": "formReset",
      "name": "formReset",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [{
            "name": "eventSemantics",
            "text": "namespace:native"
          }, {
            "name": "vegaVersion",
            "text": "2.0.0"
          }],
        "text": "An event emitter notifying the reset of the form."
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }]; }
  static get methods() { return {
    "valid": {
      "complexType": {
        "signature": "(showError?: boolean | 'rule') => Promise<FormValidateResult>",
        "parameters": [{
            "tags": [{
                "name": "param",
                "text": "showError determine whether the validation result should be reflected in UI"
              }],
            "text": "determine whether the validation result should be reflected in UI"
          }],
        "references": {
          "Promise": {
            "location": "global"
          },
          "FormValidateResult": {
            "location": "import",
            "path": "./types"
          }
        },
        "return": "Promise<FormValidateResult>"
      },
      "docs": {
        "text": "Invoke this method asynchronously to retrieve the validation result for\n   all the vega field elements nested within the `vega-form`.\n\nIf an element is a `vega-form` itself, it will recursively\n   invoke its `valid()` method.",
        "tags": [{
            "name": "param",
            "text": "showError determine whether the validation result should be reflected in UI"
          }, {
            "name": "return",
            "text": "validation result"
          }, {
            "name": "vegaVersion",
            "text": "1.3.0"
          }]
      }
    },
    "getValue": {
      "complexType": {
        "signature": "<T>(option?: FormControlOption) => Promise<T>",
        "parameters": [{
            "tags": [{
                "name": "param",
                "text": "option form control option to skip disabled/invisible components"
              }],
            "text": "form control option to skip disabled/invisible components"
          }],
        "references": {
          "Promise": {
            "location": "global"
          },
          "T": {
            "location": "global"
          },
          "FormControlOption": {
            "location": "import",
            "path": "./types"
          }
        },
        "return": "Promise<T>"
      },
      "docs": {
        "text": "Invoke this method asynchronously to retrieve the values for\n   all the available vega field elements nested within the `vega-form`.",
        "tags": [{
            "name": "param",
            "text": "option form control option to skip disabled/invisible components"
          }, {
            "name": "return",
            "text": "object with key and field value as key-value pair"
          }, {
            "name": "vegaVersion",
            "text": "1.3.0"
          }]
      }
    },
    "setValue": {
      "complexType": {
        "signature": "<T>(value: T, option?: FormControlOption) => Promise<void>",
        "parameters": [{
            "tags": [{
                "name": "param",
                "text": "value to prefill into the vega-form"
              }],
            "text": "to prefill into the vega-form"
          }, {
            "tags": [{
                "name": "param",
                "text": "option form control option to skip disabled/invisible components"
              }],
            "text": "form control option to skip disabled/invisible components"
          }],
        "references": {
          "Promise": {
            "location": "global"
          },
          "T": {
            "location": "global"
          },
          "FormControlOption": {
            "location": "import",
            "path": "./types"
          }
        },
        "return": "Promise<void>"
      },
      "docs": {
        "text": "Invoke this method to prefill the `vega-form` with the given value.\n\nThis action will also set the `isTouched` property to `false`.",
        "tags": [{
            "name": "param",
            "text": "value to prefill into the vega-form"
          }, {
            "name": "param",
            "text": "option form control option to skip disabled/invisible components"
          }, {
            "name": "vegaVersion",
            "text": "1.3.0"
          }]
      }
    },
    "reset": {
      "complexType": {
        "signature": "(defaultValueMap?: unknown, option?: FormControlOption) => Promise<void>",
        "parameters": [{
            "tags": [{
                "name": "param",
                "text": "defaultValueMap map of the vega form field name to default value"
              }],
            "text": "map of the vega form field name to default value"
          }, {
            "tags": [{
                "name": "param",
                "text": "option form control option to skip disabled/invisible components"
              }],
            "text": "form control option to skip disabled/invisible components"
          }],
        "references": {
          "Promise": {
            "location": "global"
          },
          "FormControlOption": {
            "location": "import",
            "path": "./types"
          }
        },
        "return": "Promise<void>"
      },
      "docs": {
        "text": "Invoke this method to reset the vega field elements in the form to\n   the default value provided, or an empty string if\n   no default value is found.",
        "tags": [{
            "name": "param",
            "text": "defaultValueMap map of the vega form field name to default value"
          }, {
            "name": "param",
            "text": "option form control option to skip disabled/invisible components"
          }, {
            "name": "vegaVersion",
            "text": "1.3.0"
          }]
      }
    },
    "setValidationStates": {
      "complexType": {
        "signature": "(states: VegaFormFieldValidationState[]) => Promise<void>",
        "parameters": [{
            "tags": [{
                "name": "param",
                "text": "states - An array of validation error objects."
              }],
            "text": "- An array of validation error objects."
          }],
        "references": {
          "Promise": {
            "location": "global"
          },
          "VegaFormFieldValidationState": {
            "location": "import",
            "path": "./types"
          }
        },
        "return": "Promise<void>"
      },
      "docs": {
        "text": "Invoke this method to set validation states on specific form fields.",
        "tags": [{
            "name": "param",
            "text": "states - An array of validation error objects."
          }, {
            "name": "vegaVersion",
            "text": "2.59.0"
          }]
      }
    }
  }; }
  static get elementRef() { return "host"; }
  static get watchers() { return [{
      "propName": "isValid",
      "methodName": "watchIsValid"
    }, {
      "propName": "disableBlurValidation",
      "methodName": "watchDisableBlurValidation"
    }]; }
}
__decorate([
  InjectVegaGlobalSlimmer()
], VegaForm.prototype, "globalSlimmers", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaForm.prototype, "submitEventEmitter", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaForm.prototype, "resetEventEmitter", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaForm.prototype, "validateEventEmitter", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaForm.prototype, "disableBlurValidationNotifySlimmer", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaForm.prototype, "stateController", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaForm.prototype, "childEventPrevent", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaForm.prototype, "childFormValidateEventPrevent", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaForm.prototype, "vegaComponentUsageRuntimeMetricsSlimmer", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaForm.prototype, "vegaFormFieldValidationStateController", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaForm.prototype, "nativeFormRenderer", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaForm.prototype, "vegaFormSubmitController", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaForm.prototype, "vegaFormResetController", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaForm.prototype, "vegaFormFieldController", void 0);
