#!/usr/bin/env node
"use strict";
/**
 * Parses Claude Code settings for tool restrictions
 */
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.SettingsParser = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
/**
 * Settings Parser Class
 */
class SettingsParser {
    /**
     * Parse default permission mode from project settings
     */
    static parseDefaultPermissionMode(projectDir) {
        const settings = this.loadSettings(projectDir);
        const permissions = settings.permissions || {};
        // Priority: local > shared > fallback to 'acceptEdits'
        const defaultMode = permissions.defaultMode || 'acceptEdits';
        if (process.env.CCS_DEBUG) {
            console.error(`[i] Permission mode from settings: ${defaultMode}`);
        }
        return defaultMode;
    }
    /**
     * Parse project settings for tool restrictions
     */
    static parseToolRestrictions(projectDir) {
        const settings = this.loadSettings(projectDir);
        const permissions = settings.permissions || {};
        const allowed = permissions.allow || [];
        const denied = permissions.deny || [];
        if (process.env.CCS_DEBUG) {
            console.error(`[i] Tool restrictions: ${allowed.length} allowed, ${denied.length} denied`);
        }
        return {
            allowedTools: allowed,
            disallowedTools: denied,
        };
    }
    /**
     * Load and merge settings files (local overrides shared)
     */
    static loadSettings(projectDir) {
        const claudeDir = path.join(projectDir, '.claude');
        const sharedPath = path.join(claudeDir, 'settings.json');
        const localPath = path.join(claudeDir, 'settings.local.json');
        // Load shared settings
        const shared = this.readJsonSafe(sharedPath) || {};
        // Load local settings (overrides shared)
        const local = this.readJsonSafe(localPath) || {};
        // Merge permissions (local overrides shared)
        return {
            permissions: {
                allow: [...(shared.permissions?.allow || []), ...(local.permissions?.allow || [])],
                deny: [...(shared.permissions?.deny || []), ...(local.permissions?.deny || [])],
                // Local defaultMode takes priority over shared
                defaultMode: local.permissions?.defaultMode || shared.permissions?.defaultMode || undefined,
            },
        };
    }
    /**
     * Read JSON file safely (no throw)
     */
    static readJsonSafe(filePath) {
        try {
            if (!fs.existsSync(filePath)) {
                return null;
            }
            const content = fs.readFileSync(filePath, 'utf8');
            return JSON.parse(content);
        }
        catch (error) {
            if (process.env.CCS_DEBUG) {
                console.warn(`[!] Failed to read settings: ${filePath}: ${error.message}`);
            }
            return null;
        }
    }
}
exports.SettingsParser = SettingsParser;
//# sourceMappingURL=settings-parser.js.map