/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{__decorate as e}from"tslib";import{id as r}from"../../kernel.js";import o from"../../core/Collection.js";import{Loadable as t}from"../../core/Loadable.js";import{normalize as n}from"../../core/urlUtils.js";import{property as i,subclass as s}from"../../core/accessorSupport/decorators.js";import a from"../../portal/Portal.js";import{hasUserTypeExtension as l}from"../../portal/support/utils.js";let c=class extends t{constructor(e){super(e),this.canCreateVersion=!0,this.canEditVersionedData=!0,this.layers=new o,this.lockType="none",this.tables=new o,this.versionInfo=null,this.versionService=null}load(e){return this.addResolvingPromise(this._load(e)),Promise.resolve(this)}get layersAndTables(){return new o([...this.layers.toArray(),...this.tables.toArray()])}updateLockType(){this.lockType=this.versionInfo?this.versionService.getLockType(this.versionInfo.versionIdentifier)??"none":"none"}get featureServiceVersion(){return this.featureService.sourceJSON?.currentVersion??0}async syncVersionInfo(){this.loaded&&await this._syncVersionInfo()}async _load(e){const{featureService:r}=this;await r.load(e);const o=await d(r.url),t=o?.user?.username;if(this.loggedInServiceUser=t??"",o&&t&&(this.hasAdvancedEditingUserTypeExtension=await l(o,t,"advediting")),r.versionManagementServiceUrl){const e=new(0,(await import("../../versionManagement/VersionManagementService.js")).default)({url:r.versionManagementServiceUrl});this.versionService=e,await e.load(),await this._syncVersionInfo()}}async _syncVersionInfo(){const{layersAndTables:e,versionService:r}=this;if(!r||0===e.length)return;const o=e.getItemAt(0)?.gdbVersion,t=o?await r.getVersionIdentifierFromName(o):r.defaultVersionIdentifier;this.versionInfo=await r.getVersionInfoExtended(t)}};async function d(e){const o=r?.findServerInfo(e??"");if(!o?.owningSystemUrl)return null;const t=`${o.owningSystemUrl}/sharing/rest`,i=a.getDefault();if(i?.loaded&&n(i.restUrl)===n(t))return i;return new a({authMode:"immediate",url:o.owningSystemUrl}).load()}e([i({constructOnly:!0})],c.prototype,"canCreateVersion",void 0),e([i({constructOnly:!0})],c.prototype,"canEditVersionedData",void 0),e([i({constructOnly:!0})],c.prototype,"featureService",void 0),e([i({constructOnly:!0})],c.prototype,"layers",void 0),e([i()],c.prototype,"layersAndTables",null),e([i()],c.prototype,"lockType",void 0),e([i({constructOnly:!0})],c.prototype,"tables",void 0),e([i()],c.prototype,"versionInfo",void 0),e([i()],c.prototype,"versionService",void 0),e([i()],c.prototype,"hasAdvancedEditingUserTypeExtension",void 0),e([i()],c.prototype,"loggedInServiceUser",void 0),e([i()],c.prototype,"featureServiceVersion",null),c=e([s("esri.undoredo.support.ServiceVersionInfo")],c);export{c as ServiceVersionInfo};
