"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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.summarizeAccountHistory = exports.describeSettingsSync = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const account_context_1 = require("./account-context");
const config_loader_facade_1 = require("../config/config-loader-facade");
const claude_config_path_1 = require("../utils/claude-config-path");
function safeRealpath(targetPath) {
    try {
        return fs.realpathSync(targetPath);
    }
    catch {
        return null;
    }
}
function countTopLevelDirectories(targetPath) {
    try {
        return fs
            .readdirSync(targetPath, { withFileTypes: true })
            .filter((entry) => entry.isDirectory()).length;
    }
    catch {
        return 0;
    }
}
function countJsonFiles(targetPath) {
    try {
        return fs.readdirSync(targetPath).filter((entry) => entry.endsWith('.json')).length;
    }
    catch {
        return 0;
    }
}
function resolvesTo(targetPath, expectedPath) {
    const realTarget = safeRealpath(targetPath);
    const realExpected = safeRealpath(expectedPath);
    return !!realTarget && !!realExpected && realTarget === realExpected;
}
function describeSettingsSync(instancePath, options = {}) {
    const rootSettingsPath = path.join((0, claude_config_path_1.getDefaultClaudeConfigDir)(), 'settings.json');
    const sharedSettingsPath = path.join((0, config_loader_facade_1.getCcsDir)(), 'shared', 'settings.json');
    const profileSettingsPath = path.join(instancePath, 'settings.json');
    if (options.bare) {
        if (!fs.existsSync(profileSettingsPath)) {
            return {
                state: 'missing',
                profile_settings_path: profileSettingsPath,
                shared_settings_path: sharedSettingsPath,
                root_settings_path: rootSettingsPath,
                description: 'missing (bare profile; no local settings.json yet)',
            };
        }
        return {
            state: 'profile-local',
            profile_settings_path: profileSettingsPath,
            shared_settings_path: sharedSettingsPath,
            root_settings_path: rootSettingsPath,
            description: 'profile-local (bare profile; not linked to ~/.claude/settings.json)',
        };
    }
    if (!fs.existsSync(profileSettingsPath)) {
        return {
            state: 'missing',
            profile_settings_path: profileSettingsPath,
            shared_settings_path: sharedSettingsPath,
            root_settings_path: rootSettingsPath,
            description: 'missing (run or repair this profile to recreate settings link)',
        };
    }
    if (resolvesTo(profileSettingsPath, rootSettingsPath)) {
        return {
            state: 'shared',
            profile_settings_path: profileSettingsPath,
            shared_settings_path: sharedSettingsPath,
            root_settings_path: rootSettingsPath,
            description: 'shared with ~/.claude/settings.json',
        };
    }
    return {
        state: 'unknown',
        profile_settings_path: profileSettingsPath,
        shared_settings_path: sharedSettingsPath,
        root_settings_path: rootSettingsPath,
        description: 'not linked to ~/.claude/settings.json',
    };
}
exports.describeSettingsSync = describeSettingsSync;
function summarizeAccountHistory(instancePath, policy) {
    const projectsPath = path.join(instancePath, 'projects');
    const sessionEnvPath = path.join(instancePath, 'session-env');
    const group = policy.group || account_context_1.DEFAULT_ACCOUNT_CONTEXT_GROUP;
    const sharedGroupRoot = path.join((0, config_loader_facade_1.getCcsDir)(), 'shared', 'context-groups', group);
    const sharedProjectsPath = path.join(sharedGroupRoot, 'projects');
    const deeperArtifacts = ['session-env', 'file-history', 'shell-snapshots', 'todos'];
    return {
        project_count: countTopLevelDirectories(projectsPath),
        session_count: countJsonFiles(sessionEnvPath),
        projects_path: projectsPath,
        projects_shared: policy.mode === 'shared' && resolvesTo(projectsPath, sharedProjectsPath),
        deeper_artifacts_shared: policy.mode === 'shared' &&
            policy.continuityMode === 'deeper' &&
            deeperArtifacts.every((artifact) => resolvesTo(path.join(instancePath, artifact), path.join(sharedGroupRoot, 'continuity', artifact))),
    };
}
exports.summarizeAccountHistory = summarizeAccountHistory;
//# sourceMappingURL=account-profile-diagnostics.js.map