/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { useState, useCallback, useMemo } from 'react';
import * as path from 'node:path';
import { loadTrustedFolders, TrustLevel, } from '../../config/trustedFolders.js';
import { useSettings } from '../contexts/SettingsContext.js';
import { getIdeTrust } from '@vybestack/llxprt-code-core';
/**
 * Hook that manages folder trust settings for the permissions dialog.
 * Handles current trust level state, pending changes, inherited trust detection,
 * and restart requirement detection.
 */
export function usePermissionsModifyTrust() {
    const settings = useSettings();
    const cwd = process.cwd();
    const parentFolderName = path.basename(path.dirname(cwd));
    const trustedFolders = useMemo(() => loadTrustedFolders(), []);
    // Determine current trust level for the working directory
    const currentTrustLevel = useMemo(() => {
        // Find exact match for current directory
        const rule = trustedFolders.rules.find((r) => path.normalize(r.path) === path.normalize(cwd));
        return rule?.trustLevel;
    }, [trustedFolders, cwd]);
    // Check if trusted through IDE
    const isIdeTrusted = useMemo(() => {
        const ideTrust = getIdeTrust();
        return ideTrust === true;
    }, []);
    // Check if trusted through parent folder
    const isParentTrusted = useMemo(() => {
        if (isIdeTrusted)
            return false;
        // Check if any parent rule would trust this directory
        for (const rule of trustedFolders.rules) {
            if (rule.trustLevel === TrustLevel.TRUST_PARENT) {
                const parentPath = path.dirname(rule.path);
                if (path.normalize(cwd).startsWith(path.normalize(parentPath))) {
                    return true;
                }
            }
            if (rule.trustLevel === TrustLevel.TRUST_FOLDER) {
                const normalizedRulePath = path.normalize(rule.path);
                const normalizedCwd = path.normalize(cwd);
                if (normalizedCwd.startsWith(normalizedRulePath) &&
                    normalizedCwd !== normalizedRulePath) {
                    return true;
                }
            }
        }
        return false;
    }, [trustedFolders, cwd, isIdeTrusted]);
    const [pendingTrustLevel, setPendingTrustLevel] = useState(currentTrustLevel);
    const [hasCommitted, setHasCommitted] = useState(false);
    const commitTrustLevel = useCallback((level) => {
        const nextLevel = level ?? pendingTrustLevel;
        if (!nextLevel) {
            return;
        }
        setPendingTrustLevel(nextLevel);
        trustedFolders.setValue(cwd, nextLevel);
        setHasCommitted(true);
    }, [pendingTrustLevel, trustedFolders, cwd]);
    // Determine if restart is required after committing
    const requiresRestart = useMemo(() => {
        if (!hasCommitted)
            return false;
        // Check if folder trust feature is enabled
        const folderTrustEnabled = settings.merged.folderTrust ?? false;
        if (!folderTrustEnabled)
            return false;
        // A restart is required if we changed the trust level
        return pendingTrustLevel !== currentTrustLevel;
    }, [hasCommitted, pendingTrustLevel, currentTrustLevel, settings]);
    return {
        currentTrustLevel,
        pendingTrustLevel,
        setPendingTrustLevel,
        commitTrustLevel,
        isIdeTrusted,
        isParentTrusted,
        requiresRestart,
        workingDirectory: cwd,
        parentFolderName,
        trustedFolders,
    };
}
//# sourceMappingURL=usePermissionsModifyTrust.js.map