import t from"@arcgis/core/portal/PortalItem";import{config as r}from"./config.js";import{checkArg as i}from"./utilities/checkArg.js";import{load as o}from"./utilities/esri.js";import{Uri as s}from"./utilities/uri.js";const a=/^portal:\/\/(?:([-\w][-.\w]*)\/)?([a-fA-F0-9]{32})$/;export class PortalUri{itemId;portalId;constructor(t){t&&this._parseUri(t)}toPortalItem(){const i=new t({id:this.itemId});if(this.portalId){if(!r.portals.has(this.portalId))throw new Error(`Portal with ID ${this.portalId} must be configured through @vertigis/arcgis-extensions/config.`);i.portal=r.portals.get(this.portalId)}return i}toString(){return this.portalId?`portal://${this.portalId}/${this.itemId}`:`portal://${this.itemId}`}toFullyQualifiedUri(t){const r=new PortalUri(this.toString());return r.portalId||(r.portalId=t),r}async fetchData(t){const r=this.toPortalItem();return await o(r),r.fetchData(t)}_parseUri(t){const r=t.match(a);if(!r)throw new Error("Invalid portal item URI.");this.itemId=r[2].toLowerCase(),this.portalId=r[1]||void 0}}export function isPortalUri(t){return"string"==typeof t&&!!t.match(a)}export function isFullyQualifiedPortalUri(t){const r=new s(t);return isPortalUri(t)&&""!==r.path&&""!==r.host}export function toFullyQualifiedPortalUri(t,r){return i("uri",t).isNotMissing().isNotEmpty(),new PortalUri(t).toFullyQualifiedUri(r).toString()}