/* COPYRIGHT Esri - https://js.arcgis.com/1.2/LICENSE.txt */
import{a as b}from"./XN2LWKJE.js";import"./4BPRM7CL.js";import"./NIL6NWG5.js";import{b as w}from"./ODCCVHGI.js";import{H as p,J as f,h as u,i as c,r,u as m}from"./42WFQ3AX.js";import"./3QPJ2JCA.js";export default $arcgis.t(([g,l])=>{var _=u`@layer{
/**
 * @license
 * Copyright 2025 Esri
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

:host {
  display: block;
}

/* Override to hide Editor settings panel collapsible */
.hide-editor-toolbar .esri-editor__panel-toolbar {
  display: none !important;
}

.display-none {
  display: none !important;
}

/* override the padding of the feature form */
.esri-editor__panel-content {
  padding-block: 10px !important;
}

.notice-msg {
  padding: 10px;
  width: calc(100% - 20px);
}
}`,h=class extends p{constructor(){super(...arguments),this.customizeSubmit=!1,this.enableSnapping=!1,this.showGuidingMsg=!0,this._editorLoading=!1,this._translations=b({name:"create-related-feature",blocking:!0}),this._addingAttachment=!1,this._isSubmitBtnClicked=!1,this.success=r(),this.fail=r(),this.isActionPending=r(),this.formReady=r()}static{this.properties={customizeSubmit:[5,{},{type:Boolean}],enableSnapping:[5,{},{type:Boolean}],mapView:[0,{},{attribute:!1}],selectedFeature:[0,{},{attribute:!1}],showGuidingMsg:[5,{},{type:Boolean}],table:[0,{},{attribute:!1}],_editorLoading:[16,{},{state:!0}],_translations:[16,{},{state:!0}]}}static{this.styles=_}static{this.shadowRootOptions=m}willUpdate(e){e.has("mapView")&&this.mapViewWatchHandler(),e.has("_editorLoading")&&(this.hasUpdated||this._editorLoading!==!1)&&this._editorLoadingWatchHandler(this._editorLoading)}async mapViewWatchHandler(){await this.mapView?.when(async()=>{await this.init()})}async _editorLoadingWatchHandler(e){e&&(this._container?.classList.add("display-none"),await this.startCreate(),this._container?.classList.remove("display-none"),this._editorLoading=!1)}async submit(){this._editor?.viewModel?.featureFormViewModel&&(this._isSubmitBtnClicked=!0,this._editor.viewModel.featureFormViewModel.submit())}async init(){this.mapView&&await this.createEditorWidget()}render(){let e=this._editorLoading?"":"display-none";return c`${this.showGuidingMsg&&c`<calcite-notice class="notice-msg" icon=lightbulb kind=success open><div slot=message>${this._translations.provideDetailsMsg}</div></calcite-notice>`||""}<calcite-loader class=${f(e)} label scale=s></calcite-loader>`}loaded(){this.init()}disconnectedCallback(){super.disconnectedCallback(),this._editor&&this._editor.destroy()}async createEditorWidget(){this._editor&&this._editor.destroy(),this._container=document.createElement("div"),this._container?.classList.add("display-none"),this.enableSnapping||this._container.classList.add("hide-editor-toolbar"),this._editor=new g({view:this.mapView,visibleElements:{flow:!0,settingsMenu:this.enableSnapping,sketch:!1,snappingControls:this.enableSnapping},container:this._container}),this.el.appendChild(this._container);let e=l.watch(()=>this._editor?.viewModel.featureTemplatesViewModel.state,i=>{i==="ready"&&(this._editorLoading=!0)});this._editor?.viewModel.addHandles(e);let t=l.watch(()=>this._editor?.viewModel.state,i=>{i==="adding-attachment"||i==="editing-attachment"?(this._addingAttachment=!0,this.isActionPending.emit(!0)):this._addingAttachment&&(this.isActionPending.emit(!1),this._addingAttachment=!1)});this._editor?.viewModel.addHandles(t);let a=l.watch(()=>this._editor?.viewModel.featureFormViewModel?.state,i=>{i==="ready"&&this.formReady.emit()});this._editor?.viewModel.addHandles(a)}async startCreate(){let e=this.selectedFeature?.layer,t=this.table;if(e?.relationships&&t?.relationships&&this.selectedFeature&&t.templates){let a=e.relationships[0],i=t.relationships.find(o=>e.layerId===o.relatedTableId),s=Number(this.selectedFeature.getObjectId());if(!Number.isNaN(s)&&i){let n=(await e.queryFeatures({objectIds:[s],outFields:[e.objectIdField,a.keyField]})).features[0],d=t.templates[0],y={attributeOverrides:this.makeAttributesForRelatedFeature(n,a,i),layer:t,template:d};await this._editor?.startCreateFeaturesWorkflowAtFeatureCreation(y),await this.hideEditorsElements(),this._editor?.viewModel?.featureFormViewModel?.addHandles(this._editor.viewModel.featureFormViewModel.on("submit",this.submitted.bind(this)))}}}async hideEditorsElements(){this.customizeSubmit&&(await this.timeout(700),this.el.querySelector(".esri-editor")?.querySelectorAll("calcite-flow-item")?.forEach(e=>{let t=e.shadowRoot?.querySelector("calcite-panel")?.shadowRoot?.querySelector("article");t?.querySelector("header")?.setAttribute("style","display: none"),t?.querySelector("footer")?.setAttribute("style","display: none")}))}makeAttributesForRelatedFeature(e,t,a){let i=t.keyField,s;e.attributes.hasOwnProperty(i)?s=e.getAttribute(i):e.attributes.hasOwnProperty(i.toLowerCase())?s=e.getAttribute(i.toLowerCase()):e.attributes.hasOwnProperty(i.toUpperCase())&&(s=e.getAttribute(i.toUpperCase()));let o=a.keyField,n=this.table?.fields.find(d=>d.name.toLocaleLowerCase()===o.toLocaleLowerCase());if(n)return o=n.name,{[o]:s}}async submitted(e){if(e.invalid.length){this._isSubmitBtnClicked=!1;return}if(e.valid.length&&this._isSubmitBtnClicked){this._isSubmitBtnClicked=!1;try{this._editor?.activeWorkflow&&(await this._editor.activeWorkflow.commit(),this._editor.viewModel.failures?.length&&this._editor.viewModel.failures.some(t=>{if(t.error)throw t.error}))}catch(t){this.fail.emit(t);return}this.success.emit()}}async timeout(e){return new Promise(t=>setTimeout(t,e))}};w("create-related-feature",h);return h},"widgets/Editor","core/reactiveUtils")