/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{__decorate as t}from"tslib";import r from"../core/Accessor.js";import e from"../core/Error.js";import o from"../core/Logger.js";import{isAbsolute as s,makeRelative as l}from"../core/urlUtils.js";import{property as a,cast as p,subclass as i}from"../core/accessorSupport/decorators.js";let n=class extends r{constructor(t){super(t),this.portalItem=null}normalizeCtorArgs(t){return t?.portalItem&&t.path?{...t,path:u(t.path,t.portalItem)}:t}set path(t){null!=t&&s(t)?o.getLogger(this).error("portalitemresource:invalid-path","A portal item resource path must be relative"):this._set("path",t)}_castPath(t){return u(t,this.portalItem)}get url(){return this.portalItem&&this.path?`${this.portalItem.itemUrl}/resources/${this.path}`:null}get cdnUrl(){return this.portalItem&&this.path?`${this.portalItem.itemCdnUrl}/resources/${this.path}`:null}get itemRelativeUrl(){return this.portalItem&&this.path?`./resources/${this.path}`:null}fetch(t="json",r){const o=this.cdnUrl;if(null==o)throw new e("portal-item-resource:fetch","Portal item resource does not refer to a valid item or path");return this.portalItem.portal.request(o,{responseType:t,query:{token:this.portalItem.apiKey,_ts:r?.cacheBust?Date.now():null},signal:r?.signal})}async update(t,r){const{addOrUpdateResources:e}=await import("./support/resourceUtils.js");return await e(this.portalItem,[{resource:this,content:t,compress:r?.compress,access:r?.access}],"update",r),this}hasPath(){return null!=this.path}};function u(t,r){return null==t?t:(t=t.replace(/^\/+/,""),null!=r&&s(t)&&(t=l(t,r.itemUrl)),t?.replace(/^\/+/,"").replace(/^(\.\/)?resources\//,""))}t([a()],n.prototype,"portalItem",void 0),t([a({type:String,value:null})],n.prototype,"path",null),t([p("path")],n.prototype,"_castPath",null),t([a({type:String,readOnly:!0})],n.prototype,"url",null),t([a({type:String,readOnly:!0})],n.prototype,"cdnUrl",null),t([a({type:String,readOnly:!0})],n.prototype,"itemRelativeUrl",null),n=t([i("esri.portal.PortalItemResource")],n);export{n as default};
