"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseContentRef = parseContentRef;
exports.parseScopedName = parseScopedName;
exports.parseCreatorOption = parseCreatorOption;
exports.resolveCreatorOption = resolveCreatorOption;
exports.buildScopedRef = buildScopedRef;
exports.contentKindToSavedItemKind = contentKindToSavedItemKind;
exports.contentKindToSearchKind = contentKindToSearchKind;
exports.contentKindLabel = contentKindLabel;
exports.contentKindPluralLabel = contentKindPluralLabel;
function sanitizeSegment(value, label) {
    const normalized = value.trim();
    if (!normalized) {
        throw new Error(`missing_${label}`);
    }
    if (normalized.includes('/') || normalized.includes('\\') || normalized.includes('..')) {
        throw new Error(`invalid_${label}`);
    }
    return normalized;
}
function parseContentRef(raw) {
    const normalized = raw.trim();
    if (!normalized) {
        throw new Error('missing_ref');
    }
    const segments = normalized.split('/').map((segment) => segment.trim()).filter(Boolean);
    if (segments.length !== 3) {
        throw new Error('invalid_ref');
    }
    const creator = sanitizeSegment(segments[0], 'creator');
    const kind = sanitizeSegment(segments[1], 'kind');
    const name = sanitizeSegment(segments[2], 'name');
    if (kind !== 'app' && kind !== 'asset' && kind !== 'asset-pack' && kind !== 'asset-spec') {
        throw new Error('invalid_kind');
    }
    return {
        creator,
        kind,
        name,
        ref: `${creator}/${kind}/${name}`,
    };
}
function parseScopedName(raw, label) {
    return sanitizeSegment(raw, label);
}
function parseCreatorOption(raw) {
    if (typeof raw !== 'string' || raw.trim().length === 0) {
        return 'me';
    }
    const normalized = raw.trim();
    if (normalized.toLowerCase() === 'me') {
        return 'me';
    }
    return sanitizeSegment(normalized, 'creator');
}
async function resolveCreatorOption(client, raw) {
    const parsed = parseCreatorOption(raw);
    if (parsed !== 'me') {
        return parsed;
    }
    const response = await client.me();
    const username = response.user?.username?.trim();
    if (!username) {
        throw new Error('missing_current_user');
    }
    return username;
}
function buildScopedRef(creator, kind, name) {
    const normalizedCreator = sanitizeSegment(creator, 'creator');
    const normalizedName = sanitizeSegment(name, 'name');
    return {
        creator: normalizedCreator,
        kind,
        name: normalizedName,
        ref: `${normalizedCreator}/${kind}/${normalizedName}`,
    };
}
function contentKindToSavedItemKind(kind) {
    if (kind === 'app') {
        return 'APP';
    }
    if (kind === 'asset') {
        return 'ASSET';
    }
    if (kind === 'asset-spec') {
        throw new Error('asset_specs_not_saveable');
    }
    return 'PACK';
}
function contentKindToSearchKind(kind) {
    if (kind === 'asset-spec') {
        return 'asset-spec';
    }
    if (kind === 'asset-pack') {
        return 'pack';
    }
    return kind;
}
function contentKindLabel(kind) {
    if (kind === 'asset-pack') {
        return 'asset pack';
    }
    if (kind === 'asset-spec') {
        return 'asset spec';
    }
    return kind;
}
function contentKindPluralLabel(kind) {
    if (kind === 'asset-pack') {
        return 'asset packs';
    }
    if (kind === 'asset-spec') {
        return 'asset specs';
    }
    return `${kind}s`;
}
