import t from"@arcgis/core/core/Collection";import{watch as r}from"../../support/observableUtils.js";import{CollectionProxy as e}from"../../utilities/CollectionProxy.js";export class ArrayWrappedCollection extends e{_parent;_targetProperty;_arrayProperty;get _target(){return this._targetProperty?this._getProp(this._parent,this._targetProperty):this._parent}constructor(e,o){if(super(),o)this._parent=e,this._targetProperty=e.hasOwnProperty("sublayer")?"sublayer":"layer",this._arrayProperty=o;else{const{parent:t,targetProperty:r,arrayProperty:o}=e;this._parent=t,this._targetProperty=r,this._arrayProperty=o}if(this._target)this._onTargetResolved(this._target);else{this._collection=new t;const e=r(this._parent,this._targetProperty,(()=>{const t=this._target;t&&(this.length&&this._setProp(t,this._arrayProperty,this.toArray()),this._onTargetResolved(t),e.remove())}),{sync:!0})}}add(t,r){const e=this._target;return e&&!this._getProp(e,this._arrayProperty)&&(this._setProp(e,this._arrayProperty,[]),this._collection=o(this._getProp(e,this._arrayProperty)??[])),super.add(t,r)}_onTargetResolved(t){this._collection=o(this._getProp(t,this._arrayProperty)??[]),r(this._parent,this._targetProperty?`${this._targetProperty}.${this._arrayProperty}`:this._arrayProperty,(()=>{this._target&&(this._collection=o(this._getProp(this._target,this._arrayProperty)??[]))}),{sync:!0})}_setProp(t,r,e){if(!r)return t;const o=r.split(".");let s=t;for(;s&&"object"==typeof s&&o.length>1;)s=s[o.shift()];return s&&"object"==typeof s&&(s[o[0]]=e),t}_getProp(t,r){if(!r)return;const e=r.split(".");let o=t;for(;o&&"object"==typeof o&&e.length;)o=o[e.shift()];return e.length?void 0:o}}function o(r){const e=Array.isArray(r)?r:[],o=new t;return Object.defineProperty(o,"_items",{get:()=>e}),Object.defineProperty(o,"length",{get:()=>e.length,set:t=>e.length=t}),o}