/*
 * 文件定位：ToolabilityPool / TAP 高级工具系统 / Office 工具集合 / PDF 工具。
 * 核心目的：提供 办公文档基础工具 / PDF 工具 中的“检索 PDF”基础能力原语。
 * 能力要求1：需要定义该能力的输入、输出、错误、权限需求和可观测事件。
 * 能力要求2：这些 Office 能力由 TAP 高级工具系统承接，不再作为 baseTool 完成目标。
 * 能力要求3：TAP 负责 Office 能力的治理、审批、组合和专业能力库承接。
 * 边界：承接 Office 高级工具能力，不回写到 agentCore baseTools。
 * 对接：通过 TAP 复用转交与 runtime 官方模块桥接接通，并保留治理、审批和审计边界。
 * 实现提示：先补稳定类型契约、最小可测行为和清晰错误边界，再接入真实执行逻辑。
 */
import path from "node:path";
export const pdfRipgrepDescriptor = {
    toolId: "office.pdfRipgrep",
    capability: "ripgrep-pdf-text",
    route: "toolabilityPool.officeBase.Portable_Document_Format",
    defaultDryRun: true,
    tapOwnsApproval: true,
    permissionsRequired: ["filesystem:read", "office:pdf:read"],
};
function cleanList(values) {
    return [...new Set((values ?? []).map((value) => value.trim()).filter(Boolean))];
}
function dryRunEnabled(context) {
    return context?.dryRun !== false;
}
function invocationId(context) {
    return context?.invocationId?.trim() || "office.pdfRipgrep:dry-run";
}
function auditEvent(type, context, pdfPath, metadata) {
    return {
        type,
        toolId: pdfRipgrepDescriptor.toolId,
        invocationId: invocationId(context),
        dryRun: dryRunEnabled(context),
        pdfPath,
        metadata: {
            ...(context?.auditMetadata ?? {}),
            ...(metadata ?? {}),
        },
    };
}
function failure(code, message, boundary, context, pdfPath) {
    return {
        ok: false,
        toolId: pdfRipgrepDescriptor.toolId,
        error: { code, message, boundary, publicSafe: true },
        audit: [auditEvent("agentCore.basicTool.office.pdfRipgrep.rejected", context, pdfPath, { code })],
        events: ["basicTool.office.pdfRipgrep.rejected"],
    };
}
function normalizeAllowedRoot(root) {
    const trimmed = path.posix.normalize(root.trim());
    return trimmed === "/" ? trimmed : trimmed.replace(/\/+$/, "");
}
function normalizeDocumentPath(pdfPath) {
    return path.posix.normalize(pdfPath);
}
function ensurePdfPath(pdfPath, context) {
    const trimmed = pdfPath?.trim() ?? "";
    if (trimmed.length === 0) {
        return failure("MISSING_PDF_PATH", "office.pdfRipgrep requires target.pdfPath", "input", context, pdfPath);
    }
    const normalized = normalizeDocumentPath(trimmed);
    if (!normalized.toLowerCase().endsWith(".pdf")) {
        return failure("INVALID_PDF_PATH", "office.pdfRipgrep target.pdfPath must point to a PDF file", "input", context, normalized);
    }
    return normalized;
}
function ensureQuery(query, context, pdfPath) {
    const normalized = query?.trim() ?? "";
    if (normalized.length === 0) {
        return failure("MISSING_QUERY", "office.pdfRipgrep requires target.query", "input", context, pdfPath);
    }
    return normalized;
}
function normalizePageRange(pageRange, context, pdfPath) {
    if (pageRange === undefined) {
        return undefined;
    }
    const startPage = pageRange.startPage;
    const endPage = pageRange.endPage ?? startPage;
    if (!Number.isInteger(startPage) || !Number.isInteger(endPage) || startPage < 1 || endPage < startPage) {
        return failure("INVALID_PAGE_RANGE", "office.pdfRipgrep target.pageRange must use positive pages with endPage >= startPage", "input", context, pdfPath);
    }
    return { startPage, endPage };
}
function normalizeMaxMatches(maxMatches, context, pdfPath) {
    if (maxMatches === undefined) {
        return 50;
    }
    if (!Number.isInteger(maxMatches) || maxMatches < 1 || maxMatches > 1000) {
        return failure("INVALID_MAX_MATCHES", "office.pdfRipgrep target.maxMatches must be an integer from 1 to 1000", "input", context, pdfPath);
    }
    return maxMatches;
}
function ensureScope(pdfPath, context) {
    const allowedRoots = cleanList(context?.allowedDocumentRoots).map(normalizeAllowedRoot);
    if (allowedRoots.length === 0) {
        return undefined;
    }
    const allowed = allowedRoots.some((root) => pdfPath === root || pdfPath.startsWith(`${root}/`));
    if (allowed) {
        return undefined;
    }
    return failure("SCOPE_REJECTED", "office.pdfRipgrep target PDF is outside the allowed document roots", "scope", context, pdfPath);
}
function ensurePermissions(pdfPath, context) {
    const granted = cleanList(context?.grantedPermissions);
    if (granted.length === 0) {
        return undefined;
    }
    const missing = pdfRipgrepDescriptor.permissionsRequired.filter((permission) => !granted.includes(permission));
    if (missing.length === 0) {
        return undefined;
    }
    return failure("PERMISSION_DENIED", `office.pdfRipgrep is missing permissions: ${missing.join(", ")}`, "permission", context, pdfPath);
}
function ensureDryRunOnly(pdfPath, context) {
    if (dryRunEnabled(context)) {
        return undefined;
    }
    return failure("REAL_EXECUTION_BLOCKED", "office.pdfRipgrep only returns a guarded dry-run plan in the first implementation", "contract", context, pdfPath);
}
function normalizeTarget(target, context) {
    const pdfPath = ensurePdfPath(target?.pdfPath, context);
    if (typeof pdfPath !== "string") {
        return pdfPath;
    }
    const query = ensureQuery(target?.query, context, pdfPath);
    if (typeof query !== "string") {
        return query;
    }
    const pageRange = normalizePageRange(target?.pageRange, context, pdfPath);
    if (pageRange !== undefined && "ok" in pageRange) {
        return pageRange;
    }
    const maxMatches = normalizeMaxMatches(target?.maxMatches, context, pdfPath);
    if (typeof maxMatches !== "number") {
        return maxMatches;
    }
    return {
        pdfPath,
        query,
        pageRange,
        caseSensitive: target?.caseSensitive === true,
        maxMatches,
        includeTextContext: target?.includeTextContext === true,
    };
}
function commandPreview(target) {
    return [
        "pdftotext",
        "-layout",
        ...(target.pageRange === undefined ? [] : ["-f", String(target.pageRange.startPage), "-l", String(target.pageRange.endPage ?? target.pageRange.startPage)]),
        target.pdfPath,
        "-",
        "|",
        "rg",
        ...(target.caseSensitive === true ? [] : ["--ignore-case"]),
        "--max-count",
        String(target.maxMatches ?? 50),
        ...(target.includeTextContext === true ? ["--context", "1"] : []),
        target.query,
    ];
}
export function planPdfRipgrep(request = {}) {
    const target = normalizeTarget(request.target, request.context);
    if ("ok" in target) {
        return target;
    }
    const scopeFailure = ensureScope(target.pdfPath, request.context);
    if (scopeFailure !== undefined) {
        return scopeFailure;
    }
    const permissionFailure = ensurePermissions(target.pdfPath, request.context);
    if (permissionFailure !== undefined) {
        return permissionFailure;
    }
    const realExecutionFailure = ensureDryRunOnly(target.pdfPath, request.context);
    if (realExecutionFailure !== undefined) {
        return realExecutionFailure;
    }
    return {
        ok: true,
        toolId: pdfRipgrepDescriptor.toolId,
        output: {
            kind: "agentCore.basicTool.office.pdfRipgrep",
            target,
            commandPreview: commandPreview(target),
            dryRun: true,
            executionBlocked: true,
            permissionsRequired: pdfRipgrepDescriptor.permissionsRequired,
            unsafeSideEffects: false,
            resultEnvelope: {
                matches: [],
                truncated: false,
            },
        },
        audit: [
            auditEvent("agentCore.basicTool.office.pdfRipgrep.dryRun", request.context, target.pdfPath, {
                pageRange: target.pageRange,
                caseSensitive: target.caseSensitive,
                maxMatches: target.maxMatches,
                includeTextContext: target.includeTextContext,
            }),
        ],
        events: ["basicTool.office.pdfRipgrep.dryRun"],
    };
}
