import{__decorate as e}from"tslib";import t from"@arcgis/core/core/Collection";import r from"@arcgis/core/layers/support/FeatureType.js";import s from"@arcgis/core/rest/support/AttachmentQuery.js";import{InvalidOperationError as a}from"../InvalidOperationError.js";import{toAttachment as i}from"../data/Attachment.js";import{addAttachment as l,deleteAttachments as o}from"../data/support/attachments.js";import{_isFeatureCollection as n}from"../json/FeatureLayerJson.js";import{applyJsonToLayer as u}from"../json/LayerJson.js";import{DefaultArcGISRequestHelper as p}from"../support/ArcGISRequestHelper.js";import{serializable as y}from"../support/Serializable.js";import{ArcGISRelationshipCollection as h}from"../support/_ArcGISRelationshipCollection.js";import{applyEdits as d}from"../support/_featureEdit.js";import{notifyChange as c,watch as m}from"../support/observableUtils.js";import{getGdbVersion as _}from"../utilities/_version.js";import{isFeatureLayerExtension as f,isSubtypeGroupLayerExtension as b}from"../utilities/extensions.js";import{isFeatureLayer as S}from"../utilities/layers.js";import{isNumeric as j}from"../utilities/number.js";import{isEmpty as w}from"../utilities/object.js";import{FeatureLayerExtensionBase as C,FeatureLayerReadOnlySchema as I}from"./FeatureLayerExtensionBase.js";import{addOutFields as g}from"./support/layers.js";const F="Cannot use feature layer that has a source collection and no url.";let D=class extends C{attachmentTypes;get enableClientSideData(){return void 0===this._enableClientSideData?"cluster"===this.layer?.featureReduction?.type:this._enableClientSideData}set enableClientSideData(e){this._enableClientSideData=e,this._updateClientSideFields()}get renderer(){return this.layer?.renderer}get canChangeLabels(){return!0}_enableClientSideData;async _addAttachment(e,t){return l(e,this.layer,t,{attachmentTypes:this.attachmentTypes})}async _deleteAttachments(e,t){return o(e,this.layer,t)}_getSerializableProperties(){const e=super._getSerializableProperties();return{...e,attachmentTypes:["initial"],requestHelper:{...this._toPropertyDef(e.requestHelper),default:new p({batchLayerRequests:!1})},enableClientSideData:{serializeModes:["initial"],isDefault:e=>e===!("cluster"!==this.layer?.featureReduction?.type)}}}_isSupportedLayer(e){return super._isSupportedLayer(e)&&!t.isCollection(e.source)}async _getAttachments(e){if(!this.layer?.capabilities?.data?.supportsAttachment)return[];const t=+e.primaryKey,r=this.layer?.gdbVersion??_(this.layer?.customParameters),a=new s({objectIds:[t]}),l=await this.layer.queryAttachments(a);return l?.[t]?.length?l[t].map((e=>i(e,{gdbVersion:r}))):[]}async _addFeatures(e,t){await d({adds:e},this.layer,t)}async _updateFeatures(e,t){await d({updates:e},this.layer,t)}async _deleteFeatures(e,t){await d({deletes:e},this.layer,t)}_createRelationships(){return new L(this)}_createSchema(){return new I(this)}async _onInitialize(){if(this.layer&&!this._isSupportedLayer(this.layer))throw new a(F);await super._onInitialize(),await this.relationships.initialize(),this._updateClientSideFields(),this._watchHandles.push(m(this,"layer",(()=>{this._updateClientSideFields()}),{sync:!0})),this._addSubtypeDomains(),c(this,"capabilities")}_applyLayerProperties(e,t){if(n(e))throw new a(F);return super._applyLayerProperties(e,t)}async _applyPortalItemProperties(e){if("Feature Service"!==e.type)throw new Error(`Cannot create Feature Layer from Portal item that has type: ${e.type}`);let{url:t}=e;t.match(".*MapServer/\\d{1,}/?$")||t.match(".*FeatureServer/\\d{1,}/?$")||(t=`${t}/0`),this.layer.url=t||this.layer.url;const r=await e.fetchData();if(r?.layers){const e=r.layers.find((e=>e.id===this.layer.layerId));if(e){const{id:t,...r}=e;await u(r,this.layer)}}}_addSubtypeDomains(){if(!this.layer?.subtypes)return;this.layer.typeIdField=this.layer.typeIdField||this.layer.subtypeField,this.layer.types=this.layer.types??[];const e=!this.layer.types.length;this.layer.subtypes.forEach((t=>{const s=this.layer.types.find((e=>e.id===t.code));s&&w(s.domains)?s.domains=t.domains:!s&&e&&this.layer.types.push(new r({id:t.code,domains:t.domains,name:t.name}))}))}_updateClientSideFields(){this.layer&&this._enableClientSideData&&g(this.layer,this.featureSettings.outFields)}};D=e([y],D);export{D as FeatureLayerExtension};class L extends h{get _sourceUrl(){if(this._source.layer?.url)return j(this._source.layer.layerId)?`${this._source.layer.url}/${this._source.layer.layerId}`:this._source.layer.url}get _sourceLayerId(){return this._source.layer?this._source.layer.layerId:void 0}_findRelatedSource(e){if(!this._source.mapExtension||!this._source.layer?.url)return;const t=this._source.mapExtension.allLayerExtensions.initializedItems.find((t=>(f(t)||b(t))&&t.layer.url===this._source.layer.url&&t.layer.layerId===e));return t||this._source.mapExtension.tableExtensions.initializedItems.find((t=>S(t.table)&&t.table.url===this._source.layer.url&&t.table.layerId===e))}}