/*!
 * Copyright (c) 2026 Vertex Software LLC. All rights reserved.
 */
import { Uri } from "@vertexvis/utils";
import { InvalidResourceUrnError } from "../errors";
export function fromUrn(urn) {
    var _a;
    const uri = Uri.parse(urn);
    if (uri.scheme !== 'urn' || uri.path == null) {
        throw new Error('Invalid URN. Expected URN scheme.');
    }
    const [nid, resourceType, resourceId, ...subResourcePath] = uri.path.split(/[:/]/);
    if (nid !== 'vertexvis' && nid !== 'vertex') {
        throw new Error('Invalid URN. Expected URN to be vertex namespace');
    }
    if (nid === 'vertexvis') {
        console.warn("vertexvis namespace is deprecated. Use 'vertex' for the namespace urn instead");
    }
    switch (resourceType) {
        case 'stream-key':
            const queries = fromQuery(uri.query);
            const subResource = (_a = fromSubResourcePath(subResourcePath.join('/'))) !== null && _a !== void 0 ? _a : queries.find((q) => q.type === 'scene-view-state');
            return {
                resource: { type: 'stream-key', id: resourceId },
                subResource,
                queries: queries.filter((q) => q.type !== 'scene-view-state'),
            };
        default:
            throw new InvalidResourceUrnError(`Invalid URN. Unknown resource type ${resourceType}`);
    }
}
function fromSubResourcePath(path) {
    if (path != null) {
        const [subResourceType, subResourceId] = path.split('/');
        switch (subResourceType) {
            case 'scene-view-states':
                return {
                    type: 'scene-view-state',
                    id: subResourceId,
                };
            default:
                return undefined;
        }
    }
}
function fromQuery(query) {
    if (query != null) {
        return query.split('&').map((queryFragment) => {
            const [resourceType, resourceId] = queryFragment.split('=');
            switch (resourceType) {
                case 'supplied-id':
                    return { type: 'supplied-id', id: resourceId };
                case 'scene-view-state':
                    return { type: 'scene-view-state', id: resourceId };
                default:
                    throw new Error('Invalid URN. Unknown query value type');
            }
        });
    }
    else {
        return [];
    }
}
//# sourceMappingURL=loadableResource.js.map
