import{LitElement as t}from"lit";import{dispatchCustomEvent as e}from"./lib/events.js";import{isStringEmpty as r}from"./lib/utils.js";import{focusBySelector as s}from"./lib/focus-helper.js";import{combineValidators as i}from"./lib/form/validation.js";function a(t){const e=new FormData(t);return Object.fromEntries(Array.from(e.keys()).map((t=>{const r=e.getAll(t);return 0===r.length?null:1===r.length?[t,r[0]]:[t,r]})).filter((t=>null!=t)))}function n(t){return t instanceof g}function l(t){return!function(t){return t instanceof HTMLButtonElement||t instanceof HTMLInputElement&&o.includes(t.type)}(t)&&"function"==typeof t.checkValidity&&void 0!==t.willValidate&&void 0!==t.validationMessage}const o=["button","reset","submit"];function u(t){return null==t?"":"string"==typeof t?t:(t instanceof HTMLElement&&(t.innerHTML=t.innerHTML.replace("<br>"," ")),t instanceof Node?t.textContent:t)}function h(t){s(t,":invalid, [internals-invalid]")}class g extends t{static get formAssociated(){return!0}static get properties(){return{customErrorMessages:{type:Object,attribute:!1},customValidator:{type:Object,attribute:!1},errorMessage:{type:Object,attribute:"error-message"},name:{type:String,reflect:!0}}}constructor(){super(),this.customErrorMessages=null,this.customValidator=null,this.errorMessage=null,this.name=null,this._errorState={message:null,validity:null},this._internals=this.attachInternals()}_getValuePropertyName(){return"value"}_getResetPropertyName(){return"resetValue"}_getFormControlElement(){throw new Error("You must implement _getFormControlElement() abstract method")}_getErrorElement(){throw new Error("You must implement _getErrorElement() abstract method")}_getErrorMessages(){return null}_getValidator(){return null}_getFormControlData(){const t=this.$getValue();return null==t||"string"==typeof t||t instanceof File||t instanceof FormData?t:(console.warn("The value to set to form data should be of type `null|File|string|FormData`. You've got ",t),t.toString())}_getReactiveValidationProperties(){return[]}validate(){const t={...this._getErrorMessages(),...this.customErrorMessages??{}},e=i([this._getValidator(),this.customValidator]).validate(this.$getValue(),null!=this._internals.form?a(this._internals.form):{}),r=!0===e.valid?null:this.$resolveErrorMessage(e.code,t);return this._errorState={message:r,validity:e},e.valid?this.$setValidValidity():this.$setInvalidValidity(u(r)),e}reportInlineValidity(){return this.errorMessage!==this._errorState.message&&(this.errorMessage=this._errorState.message,e(this,"error-message-change",this.errorMessage)),this._errorState.validity.valid}get inlineValidity(){return this._errorState.validity}formResetCallback(){this.$resetValue(),this.validate(),null!=this._errorState.message&&(this.errorMessage=null,e(this,"error-message-change",null))}get willValidate(){return this._internals.willValidate}checkValidity(){return this._internals.checkValidity()}reportValidity(){return this._internals.reportValidity()}get validity(){return this._internals.validity}get validationMessage(){return this._internals.validationMessage}setCustomValidity(t){r(t)?this.$setValidValidity():this.$setInvalidValidity(t)}updated(t){let e=!1;const r=t.has("errorMessage"),s=null==this.errorMessage||"string"==typeof this.errorMessage&&0===this.errorMessage.length;t.has(this._getValuePropertyName())&&(this._internals.setFormValue(this._getFormControlData()),e=!0);if(this._getReactiveValidationProperties().some((e=>t.has(e)))&&(e=!0),r&&s&&(e=!0),(t.has("customValidator")||t.has("customErrorMessages"))&&(e=!0),r&&!s){let t;t="string"==typeof this.errorMessage?this.errorMessage:this._getErrorElement().innerText.replace("\n"," "),this.$setInvalidValidity(t),e=!1}e&&this.validate()}$getValue(){return this[this._getValuePropertyName()]}$resetValue(){const t=this[this._getResetPropertyName()];null!=t&&(this[this._getValuePropertyName()]=t)}$resolveErrorMessage(t,e){const r=e?.[t];return("function"==typeof r?r():r)??t}$setValidValidity(){this._internals.setValidity({})}$setInvalidValidity(t){this._internals.setValidity({customError:!0},t,this._getFormControlElement())}}export{g as C,l as a,u as c,h as f,a as g,n as i};
//# sourceMappingURL=cc-form-control-element.abstract-23bf2c49.js.map
