/* COPYRIGHT Esri - https://js.arcgis.com/5.1/LICENSE.txt */
import{a as _}from"./XF5N3NI5.js";import"./UBPOT5V3.js";import"./ISFTCFSJ.js";import{b as y}from"./2I5LGZNX.js";import{E as g,F as b,G as f,I as w,h as m,i as l,q as o,t as p}from"./FFF5HUPM.js";import"./EXBEKVP5.js";export default $arcgis.t(([c])=>{var v=m`@layer{:host{display:block}.hide-editor-toolbar .esri-editor__panel-toolbar,.display-none{display:none!important}.esri-editor__panel-content{padding-block:10px!important}.notice-msg{padding:10px;width:calc(100% - 20px)}}`,h=class extends f{constructor(){super(...arguments),this.customizeSubmit=!1,this.enableSnapping=!1,this.showGuidingMsg=!0,this._editorLoading=!1,this._translations=_({name:"create-related-feature",blocking:!0}),this._addingAttachment=!1,this._editor=g(),this._isSubmitBtnClicked=!1,this.success=o(),this.fail=o(),this.isActionPending=o(),this.formReady=o(),this._onEditorPropertyChange=()=>{let t=this._editor.value;t&&(c.watch(()=>t.state,e=>{e==="adding-attachment"||e==="editing-attachment"?(this._addingAttachment=!0,this.isActionPending.emit(!0)):this._addingAttachment&&(this.isActionPending.emit(!1),this._addingAttachment=!1)}),c.watch(()=>t.activeWorkflow?.formViewModel?.state,e=>{e==="ready"&&this.formReady.emit()}))}}static{this.properties={customizeSubmit:5,enableSnapping:5,mapView:0,selectedFeature:0,showGuidingMsg:5,table:0,_editorLoading:16,_translations:16}}static{this.styles=v}static{this.shadowRootOptions=p}async submit(){this._editor.value?.activeWorkflow&&(this._isSubmitBtnClicked=!0,this._editor.value.activeWorkflow?.formViewModel?.submit())}render(){let t=this._editorLoading?"":"display-none";return l`${this.showGuidingMsg&&l`<calcite-notice class="notice-msg" icon=lightbulb kind=success open><div slot=message>${this._translations.provideDetailsMsg}</div></calcite-notice>`||""}<calcite-loader class=${w(t)} .label=${this._translations.loading} scale=s></calcite-loader>${this.mapView&&this._getEditor()||""}`}disconnectedCallback(){super.disconnectedCallback(),this._editor.value&&this._editor.value.destroy()}_getEditor(){return l`<arcgis-editor .view=${this.mapView} hide-edit-features-section .hideSettingsMenu=${!this.enableSnapping} hide-sketch .hideSnappingControlsElementsEnabledToggle=${!this.enableSnapping} @arcgisReady=${this._startCreate} @arcgisPropertyChange=${this._onEditorPropertyChange} ${b(this._editor)}></arcgis-editor>`}async _startCreate(){let t=this.selectedFeature?.layer,e=this.table;if(t.relationships&&e?.relationships&&this.selectedFeature&&e.templates){let s=t.relationships[0],i=e.relationships.find(r=>t.layerId===r.relatedTableId),a=Number(this.selectedFeature.getObjectId());if(!Number.isNaN(a)&&i){let r=(await t.queryFeatures({objectIds:[a],outFields:[t.objectIdField,s.keyField]})).features[0],n=e.templates[0],d={attributeOverrides:this.makeAttributesForRelatedFeature(r,s,i),layer:e,template:n};await this._editor.value?.startCreateFeaturesWorkflowAtFeatureCreation(d),await this.hideEditorsElements();let u=this._editor.value?.activeWorkflow?.formViewModel;u&&this._editor.value?.activeWorkflow?.formViewModel?.addHandles(u.on("submit",this.submitted.bind(this)))}}}async hideEditorsElements(){this.customizeSubmit&&(await this.timeout(700),this.el.querySelector("arcgis-editor")?.shadowRoot?.querySelectorAll("calcite-flow-item")?.forEach(t=>{let e=t.shadowRoot?.querySelector("calcite-panel")?.shadowRoot?.querySelector("article");e?.querySelector("header")?.setAttribute("style","display: none"),e?.querySelector("footer")?.setAttribute("style","display: none")}))}makeAttributesForRelatedFeature(t,e,s){let i=e.keyField,a;t.attributes.hasOwnProperty(i)?a=t.getAttribute(i):t.attributes.hasOwnProperty(i.toLowerCase())?a=t.getAttribute(i.toLowerCase()):t.attributes.hasOwnProperty(i.toUpperCase())&&(a=t.getAttribute(i.toUpperCase()));let r=s.keyField,n=this.table?.fields.find(d=>d.name.toLocaleLowerCase()===r.toLocaleLowerCase());if(n)return r=n.name,{[r]:a}}async submitted(t){if(t.invalid.length){this._isSubmitBtnClicked=!1;return}if(t.valid.length&&this._isSubmitBtnClicked){this._isSubmitBtnClicked=!1;try{if(this._editor.value?.activeWorkflow){await this._editor.value.activeWorkflow.commit();let e=this._editor.value.activeWorkflow.failures;e?.length&&e.some(s=>{if(s.error)throw s.error})}}catch(e){this.fail.emit(e);return}this.success.emit()}}async timeout(t){return await new Promise(e=>setTimeout(e,t))}};y("create-related-feature",h);return h},"core/reactiveUtils")