"use strict";
/**
 * Account context policy helpers.
 *
 * Controls whether account instances keep project context isolated, or share
 * project workspace context with other accounts in the same context group.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatAccountContextPolicy = exports.policyToAccountContextMetadata = exports.resolveAccountContextPolicy = exports.resolveCreateAccountContext = exports.isAccountContextMetadata = exports.isValidAccountProfileName = exports.isValidContextGroupName = exports.normalizeContextGroupName = exports.ACCOUNT_PROFILE_NAME_PATTERN = exports.MAX_CONTEXT_GROUP_LENGTH = exports.DEFAULT_ACCOUNT_CONTINUITY_MODE = exports.DEFAULT_ACCOUNT_CONTEXT_GROUP = exports.DEFAULT_ACCOUNT_CONTEXT_MODE = void 0;
exports.DEFAULT_ACCOUNT_CONTEXT_MODE = 'isolated';
exports.DEFAULT_ACCOUNT_CONTEXT_GROUP = 'default';
exports.DEFAULT_ACCOUNT_CONTINUITY_MODE = 'standard';
exports.MAX_CONTEXT_GROUP_LENGTH = 64;
exports.ACCOUNT_PROFILE_NAME_PATTERN = /^[a-zA-Z][a-zA-Z0-9_-]*$/;
const CONTEXT_GROUP_PATTERN = /^[a-zA-Z][a-zA-Z0-9_-]*$/;
/**
 * Normalize context group names so paths and config stay consistent.
 */
function normalizeContextGroupName(value) {
    return value.trim().toLowerCase().replace(/\s+/g, '-');
}
exports.normalizeContextGroupName = normalizeContextGroupName;
/**
 * Validate context group naming constraints.
 */
function isValidContextGroupName(value) {
    return value.length <= exports.MAX_CONTEXT_GROUP_LENGTH && CONTEXT_GROUP_PATTERN.test(value);
}
exports.isValidContextGroupName = isValidContextGroupName;
/**
 * Validate account profile naming constraints.
 */
function isValidAccountProfileName(value) {
    return exports.ACCOUNT_PROFILE_NAME_PATTERN.test(value);
}
exports.isValidAccountProfileName = isValidAccountProfileName;
/**
 * Runtime type guard for account context metadata payloads.
 */
function isAccountContextMetadata(value) {
    if (typeof value !== 'object' || value === null) {
        return false;
    }
    const candidate = value;
    const mode = candidate['context_mode'];
    const group = candidate['context_group'];
    const continuity = candidate['continuity_mode'];
    const modeValid = mode === undefined || mode === 'isolated' || mode === 'shared';
    const groupValid = group === undefined || typeof group === 'string';
    const continuityValid = continuity === undefined || continuity === 'standard' || continuity === 'deeper';
    if (!modeValid || !groupValid || !continuityValid) {
        return false;
    }
    if (mode !== 'shared' && continuity !== undefined) {
        return false;
    }
    return true;
}
exports.isAccountContextMetadata = isAccountContextMetadata;
/**
 * Resolve create-command flags into a valid context policy.
 */
function resolveCreateAccountContext(input) {
    const hasGroupFlag = input.contextGroup !== undefined;
    const continuityMode = input.deeperContinuity ? 'deeper' : 'standard';
    if (input.deeperContinuity && !input.shareContext && !hasGroupFlag) {
        return {
            policy: { mode: 'isolated' },
            error: 'Advanced deeper continuity requires shared context (--share-context or --context-group).',
        };
    }
    if (hasGroupFlag) {
        if (!input.contextGroup || input.contextGroup.trim().length === 0) {
            return {
                policy: { mode: 'isolated' },
                error: 'Context group name is required after --context-group',
            };
        }
        const normalizedGroup = normalizeContextGroupName(input.contextGroup);
        if (!isValidContextGroupName(normalizedGroup)) {
            return {
                policy: { mode: 'isolated' },
                error: `Invalid context group. Use letters/numbers/dash/underscore, start with a letter, max ${exports.MAX_CONTEXT_GROUP_LENGTH} chars.`,
            };
        }
        return {
            policy: {
                mode: 'shared',
                group: normalizedGroup,
                continuityMode,
            },
        };
    }
    if (input.shareContext) {
        return {
            policy: {
                mode: 'shared',
                group: exports.DEFAULT_ACCOUNT_CONTEXT_GROUP,
                continuityMode,
            },
        };
    }
    return {
        policy: { mode: exports.DEFAULT_ACCOUNT_CONTEXT_MODE },
    };
}
exports.resolveCreateAccountContext = resolveCreateAccountContext;
/**
 * Resolve persisted metadata into runtime policy with safe defaults.
 */
function resolveAccountContextPolicy(metadata) {
    const mode = metadata?.context_mode === 'shared' ? 'shared' : 'isolated';
    if (mode === 'shared') {
        const continuityMode = metadata?.continuity_mode === 'deeper' ? 'deeper' : 'standard';
        const rawGroup = metadata?.context_group;
        if (rawGroup && rawGroup.trim().length > 0) {
            const normalized = normalizeContextGroupName(rawGroup);
            if (isValidContextGroupName(normalized)) {
                return { mode: 'shared', group: normalized, continuityMode };
            }
        }
        return {
            mode: 'shared',
            group: exports.DEFAULT_ACCOUNT_CONTEXT_GROUP,
            continuityMode,
        };
    }
    return { mode: 'isolated' };
}
exports.resolveAccountContextPolicy = resolveAccountContextPolicy;
/**
 * Convert runtime policy back to persisted metadata.
 */
function policyToAccountContextMetadata(policy) {
    if (policy.mode === 'shared') {
        return {
            context_mode: 'shared',
            context_group: policy.group || exports.DEFAULT_ACCOUNT_CONTEXT_GROUP,
            continuity_mode: policy.continuityMode === 'deeper' ? 'deeper' : exports.DEFAULT_ACCOUNT_CONTINUITY_MODE,
        };
    }
    return {
        context_mode: 'isolated',
    };
}
exports.policyToAccountContextMetadata = policyToAccountContextMetadata;
/**
 * User-facing summary for display/help output.
 */
function formatAccountContextPolicy(policy) {
    if (policy.mode === 'shared') {
        const continuity = policy.continuityMode === 'deeper' ? 'deeper continuity' : 'standard';
        return `shared (${policy.group || exports.DEFAULT_ACCOUNT_CONTEXT_GROUP}, ${continuity})`;
    }
    return 'isolated';
}
exports.formatAccountContextPolicy = formatAccountContextPolicy;
//# sourceMappingURL=account-context.js.map