import{LitElement as e,html as t,css as s}from"../../../lit/index.js";import{SimpleFieldsLite as o}from"./simple-fields-lite.js";import{SimpleFields as i}from"../simple-fields.js";class SimpleFieldsFormLite extends e{static get styles(){return[s`
        :host {
          display: block;
        }
      `]}static get tag(){return"simple-fields-form-lite"}render(){return t`
      <form part="form">
        <slot name="before"></slot>
        <simple-fields-lite
          id="sf"
          .autofocus="${!this.disableAutofocus}"
          .language="${this.language}"
          .resources="${this.resources}"
          .schema="${this.schema}"
          .elementizer="${this.elementizer}"
          .value="${this.value}"
          @value-changed="${this._valueChanged}"
          part="fields"
        >
        </simple-fields-lite>
        <slot name="after"></slot>
        <slot></slot>
      </form>
    `}getFormElementById(e){return(this.__formElementsArray||[]).filter((t=>t.id===e))[0]}_valueChanged(e){this.value=e.detail.value}setValue(e){this.value=e}rebuildForm(){this.shadowRoot.querySelector("#sf")&&this.shadowRoot.querySelector("#sf").rebuidForm()}firstUpdated(e){e.forEach(((e,t)=>{"loadEndpoint"===t&&this.autoload&&this.loadData()}))}updated(e){e.forEach(((e,t)=>{!this.autoload||this.loadResponse||this.loading||"loadEndpoint"!==t&&"autoload"!==t||this.loadData(),"loadResponse"===t&&this.loadResponse.data&&(this._applyLoadedData(),this.dispatchEvent(new CustomEvent("response",{bubbles:!0,composed:!0,cancelable:!1,detail:this.loadResponse})))}))}_applyLoadedData(){this.loadResponse.data.schema&&(this.schema=this.loadResponse.data.schema),this.loadResponse.data.value&&(this.value=this.loadResponse.data.value)}loadData(){this.loading=!0,this.fetchData(this.loadEndpoint,this.method,this.headers,this.body).then((e=>{this.loading=!1,this.loadResponse=e,this.dispatchEvent(new CustomEvent("simple-fields-form-data-loaded",{detail:{value:e}}))}))}async fetchData(e,t,s,o){let i={};return"GET"==t?(o&&(e+="?"+Object.entries(o).map((([e,t])=>`${encodeURIComponent(e)}=${encodeURIComponent(t)}`)).join("&")),i=await fetch(e,{method:t,headers:s})):i=await fetch(e,{method:t,headers:s,body:JSON.stringify(o)}),await i.json()}constructor(){super(),this._setFieldProperties(),this._setFormProperties()}_setFieldProperties(){this.disableAutofocus=!1,this.language="en",this.resources={},this.schema={},this.value={}}_setFormProperties(){this.method="POST",this.loading=!1,this.autoload=!1,this.headers={Accept:"application/json","Content-Type":"application/json"},this.body={}}submit(){let e=this.shadowRoot.querySelector("#sf");return this.saveEndpoint&&fetch(this.saveEndpoint,{method:this.method,headers:this.headers,body:JSON.stringify(e.value)}),e.value}static get fieldProperties(){return{disableAutofocus:{type:Boolean},error:{type:Object},language:{type:String,attribute:"lang",reflect:!0},resources:{type:Object},schema:{type:Object},schemaConversion:{type:Object,attribute:"schema-conversion"},value:{type:Object}}}static get formProperties(){return{autoload:{type:Boolean,reflect:!0},loading:{type:Boolean,reflect:!0},loadEndpoint:{type:String,attribute:"load-endpoint"},saveEndpoint:{type:String,attribute:"save-endpoint"},method:{type:String},headers:{type:Object},body:{type:Object},loadResponse:{type:Object}}}get formFields(){return this.shadowRoot&&this.shadowRoot.querySelector&&this.shadowRoot.querySelector("#sf")?this.shadowRoot.querySelector("#sf"):void 0}get formElements(){return this.formFields?this.formFields.formElements:{}}get formElementsArray(){return this.formFields?this.formFields.formElementsArray:[]}static get properties(){return{...this.fieldProperties,...this.formProperties}}}customElements.define(SimpleFieldsFormLite.tag,SimpleFieldsFormLite);export{SimpleFieldsFormLite};