"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parsePackageJsonName = parsePackageJsonName;
exports.calculateNewPackageJsonName = calculateNewPackageJsonName;
exports.extractScopeFromSubdirectory = extractScopeFromSubdirectory;
exports.scopeToSubdirectory = scopeToSubdirectory;
/**
 * Parses a full package.json name to extract scope and package name.
 * Handles the unisphere- prefix rules and tolerates incorrect prefixes.
 *
 * @param packageJsonName - Full package name (e.g., "@kaltura-corp/unisphere-rtc-avatar")
 * @param experienceName - Experience name from .unisphere config (e.g., "rtc")
 * @returns Parsed scope and package name
 */
function parsePackageJsonName(packageJsonName, experienceName) {
    if (!packageJsonName?.trim()) {
        throw new Error('packageJsonName cannot be empty');
    }
    if (!experienceName?.trim()) {
        throw new Error('experienceName cannot be empty');
    }
    // Validate format: must start with @scope/
    const scopeMatch = packageJsonName.match(/^(@[^/]+)\//);
    if (!scopeMatch) {
        throw new Error(`Invalid package.json name format: "${packageJsonName}"\n` +
            'Expected format: @scope/package-name');
    }
    const scope = scopeMatch[1];
    let nameAfterScope = packageJsonName.substring(scope.length + 1); // +1 for the '/'
    // Remove unisphere- prefix if present (handle incorrect usage)
    if (nameAfterScope.startsWith('unisphere-')) {
        nameAfterScope = nameAfterScope.substring('unisphere-'.length);
    }
    // Remove experience prefix
    if (nameAfterScope === experienceName) {
        // Special case: package name is same as experience (e.g., "@unisphere/rtc")
        return { scope, packageName: experienceName };
    }
    const expectedPrefix = `${experienceName}-`;
    if (!nameAfterScope.startsWith(expectedPrefix)) {
        throw new Error(`Package name "${packageJsonName}" does not follow expected format.\n` +
            `Expected: ${scope}/${expectedPrefix}package-name or ${scope}/${experienceName}\n` +
            `Found: ${packageJsonName}`);
    }
    const packageName = nameAfterScope.substring(expectedPrefix.length);
    if (!packageName) {
        throw new Error(`Package name "${packageJsonName}" results in empty package name.\n` +
            `Expected: ${scope}/${expectedPrefix}<package-name>`);
    }
    return { scope, packageName };
}
/**
 * Calculates the new package.json name based on target scope and package name.
 * Automatically handles unisphere- prefix rules.
 *
 * @param packageName - Package identifier (e.g., "avatar", "models")
 * @param experienceName - Experience name from .unisphere config (e.g., "rtc")
 * @param newScope - Target scope (e.g., "@kaltura-corp", "@unisphere")
 * @returns New package.json name
 */
function calculateNewPackageJsonName(packageName, experienceName, newScope) {
    if (!packageName?.trim()) {
        throw new Error('packageName cannot be empty');
    }
    if (!experienceName?.trim()) {
        throw new Error('experienceName cannot be empty');
    }
    if (!newScope?.trim()) {
        throw new Error('newScope cannot be empty');
    }
    if (!newScope.startsWith('@')) {
        throw new Error(`Invalid scope format: "${newScope}"\n` +
            'Expected format: @scope (e.g., @unisphere, @kaltura-corp)');
    }
    // Special case: package name equals experience name
    if (packageName === experienceName) {
        if (newScope === '@kaltura-corp') {
            return `@kaltura-corp/unisphere-${experienceName}`;
        }
        return `${newScope}/${experienceName}`;
    }
    // Regular case: experience-package
    const baseName = `${experienceName}-${packageName}`;
    if (newScope === '@kaltura-corp') {
        return `@kaltura-corp/unisphere-${baseName}`;
    }
    return `${newScope}/${baseName}`;
}
/**
 * Extracts the scope from a subdirectory name.
 *
 * @param subdirectory - Subdirectory name (e.g., "kaltura-corp") or null for flat structure
 * @returns Scope (e.g., "@kaltura-corp") or null
 */
function extractScopeFromSubdirectory(subdirectory) {
    if (!subdirectory?.trim()) {
        return null;
    }
    return `@${subdirectory}`;
}
/**
 * Converts a scope to a subdirectory name.
 *
 * @param scope - Scope (e.g., "@kaltura-corp")
 * @returns Subdirectory name (e.g., "kaltura-corp")
 */
function scopeToSubdirectory(scope) {
    if (!scope?.trim()) {
        throw new Error('scope cannot be empty');
    }
    if (!scope.startsWith('@')) {
        throw new Error(`Invalid scope format: "${scope}"\n` +
            'Expected format: @scope (e.g., @unisphere, @kaltura-corp)');
    }
    return scope.substring(1);
}
