"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getContinuityArtifactNames = exports.createTimestampStamp = exports.getAuthBackupRoot = exports.parseResumeFlagIntent = exports.resolveRuntimePlainCcsResumeLane = exports.resolveConfiguredPlainCcsResumeLane = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const claude_config_path_1 = require("../utils/claude-config-path");
const instance_manager_1 = __importDefault(require("../management/instance-manager"));
const profile_detector_1 = __importDefault(require("./profile-detector"));
const profile_continuity_inheritance_1 = require("./profile-continuity-inheritance");
const config_loader_facade_1 = require("../config/config-loader-facade");
function countTopLevelProjectDirs(projectsDir) {
    try {
        return fs
            .readdirSync(projectsDir, { withFileTypes: true })
            .filter((entry) => entry.isDirectory()).length;
    }
    catch {
        return 0;
    }
}
function resolveNativeLaneSummary(kind = 'native', profileName) {
    const configDir = (0, claude_config_path_1.getDefaultClaudeConfigDir)();
    const label = kind === 'profile-default' && profileName
        ? `profile "${profileName}" via native Claude lane`
        : 'native Claude lane';
    return {
        kind,
        label,
        configDir,
        profileName,
        projectCount: countTopLevelProjectDirs(path.join(configDir, 'projects')),
    };
}
function resolveAccountLaneSummary(kind, accountName) {
    const instanceMgr = new instance_manager_1.default();
    const configDir = instanceMgr.getInstancePath(accountName);
    return {
        kind,
        label: kind === 'account-inherited'
            ? `plain ccs inherits from account "${accountName}"`
            : `plain ccs defaults to account "${accountName}"`,
        configDir,
        accountName,
        projectCount: countTopLevelProjectDirs(path.join(configDir, 'projects')),
    };
}
async function resolveConfiguredPlainCcsResumeLane() {
    const detector = new profile_detector_1.default();
    const defaultProfile = detector.resolveDefaultProfileResult();
    if (defaultProfile.type === 'account') {
        return resolveAccountLaneSummary('account-default', defaultProfile.name);
    }
    const inheritedAccount = (0, profile_continuity_inheritance_1.resolveConfiguredContinuitySourceAccount)(defaultProfile.name, defaultProfile.type);
    if (inheritedAccount) {
        return resolveAccountLaneSummary('account-inherited', inheritedAccount);
    }
    if (defaultProfile.type !== 'default') {
        return resolveNativeLaneSummary('profile-default', defaultProfile.name);
    }
    return resolveNativeLaneSummary();
}
exports.resolveConfiguredPlainCcsResumeLane = resolveConfiguredPlainCcsResumeLane;
async function resolveRuntimePlainCcsResumeLane() {
    if (process.env.CLAUDE_CONFIG_DIR) {
        const configDir = path.resolve(process.env.CLAUDE_CONFIG_DIR);
        return {
            kind: 'ambient',
            label: 'current shell CLAUDE_CONFIG_DIR',
            configDir,
            projectCount: countTopLevelProjectDirs(path.join(configDir, 'projects')),
        };
    }
    return resolveConfiguredPlainCcsResumeLane();
}
exports.resolveRuntimePlainCcsResumeLane = resolveRuntimePlainCcsResumeLane;
function parseResumeFlagIntent(args) {
    for (let index = 0; index < args.length; index += 1) {
        const arg = args[index];
        if (arg === '-r') {
            return { implicit: true };
        }
        if (arg === '--resume') {
            const next = args[index + 1];
            if (next && !next.startsWith('-')) {
                return { implicit: false, explicitSessionId: next };
            }
            return { implicit: true };
        }
        if (arg.startsWith('--resume=')) {
            const sessionId = arg.slice('--resume='.length).trim();
            return sessionId ? { implicit: false, explicitSessionId: sessionId } : { implicit: true };
        }
    }
    return null;
}
exports.parseResumeFlagIntent = parseResumeFlagIntent;
function getAuthBackupRoot() {
    return path.join((0, config_loader_facade_1.getCcsDir)(), 'backups', 'auth-continuity');
}
exports.getAuthBackupRoot = getAuthBackupRoot;
function createTimestampStamp(date = new Date()) {
    const yyyy = date.getFullYear();
    const mm = String(date.getMonth() + 1).padStart(2, '0');
    const dd = String(date.getDate()).padStart(2, '0');
    const hh = String(date.getHours()).padStart(2, '0');
    const min = String(date.getMinutes()).padStart(2, '0');
    const ss = String(date.getSeconds()).padStart(2, '0');
    return `${yyyy}${mm}${dd}_${hh}${min}${ss}`;
}
exports.createTimestampStamp = createTimestampStamp;
function getContinuityArtifactNames(profileType) {
    const sharedItems = ['projects'];
    const deeperItems = ['session-env', 'file-history', 'shell-snapshots', 'todos'];
    if (profileType === 'default' || profileType === 'account') {
        return [...sharedItems, ...deeperItems];
    }
    return sharedItems;
}
exports.getContinuityArtifactNames = getContinuityArtifactNames;
//# sourceMappingURL=resume-lane-diagnostics.js.map