import{__decorate as e}from"tslib";import t from"@arcgis/core/core/Collection";import s from"@arcgis/core/rest/support/AttachmentQuery.js";import{InvalidOperationError as r}from"../InvalidOperationError.js";import{toAttachment as i}from"../data/Attachment.js";import{PopupTemplateFeatureSettings as a}from"../mapping/support/PopupTemplateFeatureSettings.js";import{DefaultArcGISRequestHelper as l}from"../support/ArcGISRequestHelper.js";import{serializable as o}from"../support/Serializable.js";import{ArcGISRelationshipCollection as n}from"../support/_ArcGISRelationshipCollection.js";import{applyEdits as u}from"../support/_featureEdit.js";import{watch as p}from"../support/observableUtils.js";import{ReadOnlyCollection as c}from"../utilities/ReadOnlyCollection.js";import{getGdbVersion as h}from"../utilities/_version.js";import{isArcGISServiceTableExtension as d}from"../utilities/extensions.js";import{parse as m}from"../utilities/format/number.js";import{isFeatureLayer as b,isMapImageSublayer as y,isSubtypeGroupLayer as _}from"../utilities/layers.js";import{isNumeric as f}from"../utilities/number.js";import{TableExtension as g,TableSchema as j}from"./_TableExtension.js";import{addAttachment as I,deleteAttachments as w,supportsAttachmentEditing as x}from"./support/attachments.js";let S=class extends g{attachmentTypes;get requestHelper(){return this._requestHelper||(this._requestHelper=new l),this._requestHelper}set requestHelper(e){this._requestHelper=e}get capabilities(){const e=x(this.table);return this.table?.capabilities?.operations?{...this.table.capabilities.operations,supportsAttachmentEditing:e,supportsStandardizedQueries:this.table.capabilities?.query?.supportsStandardizedQueriesOnly}:{...super.capabilities,supportsAttachmentEditing:e}}get canChangeDefinitionExpression(){return!0}_requestHelper;async _getAttachments(e){if(!this.table?.capabilities?.data?.supportsAttachment)return[];const t=+e.primaryKey,r=this.table?.gdbVersion??h(this.table?.customParameters),a=new s({objectIds:[t]}),l=await this.table.queryAttachments(a);return l?.[t]?.length?l[t].map((e=>i(e,{gdbVersion:r}))):[]}async _addAttachment(e,t){return I(e,this.table,t,{attachmentTypes:this.attachmentTypes})}async _deleteAttachments(e,t){return w(e,this.table,t)}get _layerUrl(){return f(this.table.layerId)?`${this.table.url}/${this.table.layerId}`:this.table.url}async*_getFeatures(e,t){const s=this.table.createQuery();s.objectIds=e.map((e=>m(e))),s.where=void 0,s.outFields=t.outFields,s.returnGeometry=t.returnGeometry,yield*(await import("../support/_query.js")).executeQuery(s,this,this.table,t.cancelToken)}async _addFeatures(e,t){await u({adds:e},this.table,t)}async _updateFeatures(e,t){await u({updates:e},this.table,t)}async _deleteFeatures(e,t){await u({deletes:e},this.table,t)}async _onInitialize(){await super._onInitialize(),await this.relationships.initialize()}_isSupportedTable(e){return!e.loaded||e.isTable}_createFeatureSettings(){return new z(this)}_createRelationships(){return new A(this)}_createSchema(){return new E(this)}_getSerializableProperties(){const e=super._getSerializableProperties();return{...e,attachmentTypes:["initial"],table:{...this._toPropertyDef(e.table),default:{}},requestHelper:{serialize:!1}}}};S=e([o],S);export{S as ArcGISServiceTableExtension};class T extends c{constructor(e){super(),e.table?this._onTableResolved(e.table):(this._collection=new t,this._handles.add(p(e,"table",(()=>{this._onTableResolved(e.table)}),{sync:!0,once:!0})))}_onTableResolved(e){this._collection=new t(e.types)}}let E=class extends j{_typeCollection;constructor(e){super(e),this._typeCollection=new T(e),this._bindSettingToTableProperty("displayField"),this._bindSettingToTableProperty("typeIdField")}destroy(){this._typeCollection.destroy(),super.destroy()}get types(){return this._typeCollection}_setSetting(e,t){if(this._tableBindings.has(e))throw new r("This schema property is read-only.");super._setSetting(e,t)}_createFieldCollection(){return new c(super._createFieldCollection(),{destroyBackingCollection:!0})}};E=e([o],E);let z=class extends a{};z=e([o],z);class A extends n{get _sourceUrl(){if(this._source.table?.url)return f(this._source.table.layerId)?`${this._source.table.url}/${this._source.table.layerId}`:this._source.table.url}get _sourceLayerId(){return this._source.table?.layerId}_findRelatedSource(e){if(!this._source.mapExtension||!this._source.table?.url)return;const t=this._source.mapExtension.tableExtensions.initializedItems.find((t=>d(t)&&t.table.url===this._source.table.url&&t.table.layerId===e));if(t)return t;for(const t of this._source.mapExtension.allLayerExtensions.initializedItems.toArray()){const s=t.allSublayerExtensions.initializedItems.find((t=>y(t.sublayer)&&t.sublayer.layer.url===this._source.table.url&&t.sublayer.id===e));if(s)return s}return this._source.mapExtension.allLayerExtensions.initializedItems.find((t=>(b(t.layer)||_(t.layer))&&t.layer.url===this._source.table.url&&t.layer.layerId===e))}}