/*
 * 文件定位：ToolabilityPool / TAP 高级工具系统 / Office 工具集合 / 演示文稿工具。
 * 核心目的：提供 办公文档基础工具 / 演示文稿工具 中的“编码演示文稿”基础能力原语。
 * 能力要求1：需要定义该能力的输入、输出、错误、权限需求和可观测事件。
 * 能力要求2：这些 Office 能力由 TAP 高级工具系统承接，不再作为 baseTool 完成目标。
 * 能力要求3：TAP 负责 Office 能力的治理、审批、组合和专业能力库承接。
 * 边界：承接 Office 高级工具能力，不回写到 agentCore baseTools。
 * 对接：通过 TAP 复用转交与 runtime 官方模块桥接接通，并保留治理、审批和审计边界。
 * 实现提示：先补稳定类型契约、最小可测行为和清晰错误边界，再接入真实执行逻辑。
 */
export const officeSlidesEncodeDescriptor = {
    toolId: "office.slidesEncode",
    capability: "encode-presentation",
    route: "toolabilityPool.officeBase.presentations",
    defaultDryRun: true,
    tapOwnsApproval: true,
    unsafeSideEffects: true,
    permissionsRequired: ["filesystem:write", "office:slides:write"],
};
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.slidesEncode:dry-run";
}
function normalizeRoot(root) {
    const trimmed = root.trim();
    return trimmed === "/" ? trimmed : trimmed.replace(/\/+$/, "");
}
function hasPathTraversal(outputPath) {
    return outputPath.split(/[\\/]+/).some((segment) => segment === "..");
}
function auditEvent(type, context, outputPath, metadata) {
    return {
        type,
        toolId: officeSlidesEncodeDescriptor.toolId,
        invocationId: invocationId(context),
        dryRun: dryRunEnabled(context),
        outputPath,
        metadata: {
            ...(context?.auditMetadata ?? {}),
            ...(metadata ?? {}),
        },
    };
}
function failure(code, message, boundary, context, outputPath) {
    return {
        ok: false,
        toolId: officeSlidesEncodeDescriptor.toolId,
        error: { code, message, boundary, publicSafe: true },
        audit: [auditEvent("agentCore.basicTool.office.slidesEncode.rejected", context, outputPath, { code })],
        events: ["basicTool.office.slidesEncode.rejected"],
    };
}
function normalizeOutputPath(outputPath, context) {
    const normalized = outputPath?.trim() ?? "";
    if (normalized.length === 0) {
        return failure("MISSING_OUTPUT_PATH", "office.slidesEncode requires target.outputPath", "input", context, outputPath);
    }
    if (hasPathTraversal(normalized)) {
        return failure("SCOPE_REJECTED", "office.slidesEncode target.outputPath must not escape its presentation scope", "scope", context, normalized);
    }
    return normalized;
}
function normalizeFormat(format, context, outputPath) {
    if (format === undefined) {
        return "pptx";
    }
    if (format === "pptx" || format === "odp" || format === "pdf-preview") {
        return format;
    }
    return failure("INVALID_FORMAT", "office.slidesEncode target.format is not supported", "input", context, outputPath);
}
function normalizeSource(source, context, outputPath) {
    if (source === undefined) {
        return failure("MISSING_SOURCE", "office.slidesEncode requires target.source", "input", context, outputPath);
    }
    if (source.sourceKind !== "structured-outline" &&
        source.sourceKind !== "html-deck" &&
        source.sourceKind !== "markdown-deck") {
        return failure("INVALID_SOURCE_KIND", "office.slidesEncode target.source.sourceKind must be structured-outline, html-deck, or markdown-deck", "input", context, outputPath);
    }
    if (!Number.isInteger(source.slideCount) || source.slideCount === undefined || source.slideCount <= 0) {
        return failure("INVALID_SLIDE_COUNT", "office.slidesEncode target.source.slideCount must be a positive integer", "input", context, outputPath);
    }
    return {
        title: source.title?.trim() || undefined,
        slideCount: source.slideCount,
        sourceKind: source.sourceKind,
    };
}
function normalizeTarget(target, context) {
    const outputPath = normalizeOutputPath(target?.outputPath, context);
    if (typeof outputPath !== "string") {
        return outputPath;
    }
    const format = normalizeFormat(target?.format, context, outputPath);
    if (typeof format !== "string") {
        return format;
    }
    const source = normalizeSource(target?.source, context, outputPath);
    if ("ok" in source) {
        return source;
    }
    return {
        outputPath,
        format,
        overwrite: target?.overwrite === true,
        source,
    };
}
function ensureScope(target, context) {
    const allowedRoots = cleanList(context?.allowedPresentationRoots).map(normalizeRoot);
    if (allowedRoots.length === 0) {
        return undefined;
    }
    const allowed = allowedRoots.some((root) => target.outputPath === root || target.outputPath.startsWith(`${root}/`));
    if (allowed) {
        return undefined;
    }
    return failure("SCOPE_REJECTED", "office.slidesEncode target output is outside the allowed presentation roots", "scope", context, target.outputPath);
}
function ensurePermissions(target, context) {
    const granted = cleanList(context?.grantedPermissions);
    if (granted.length === 0) {
        return undefined;
    }
    const missing = officeSlidesEncodeDescriptor.permissionsRequired.filter((permission) => !granted.includes(permission));
    if (missing.length === 0) {
        return undefined;
    }
    return failure("PERMISSION_DENIED", `office.slidesEncode is missing permissions: ${missing.join(", ")}`, "permission", context, target.outputPath);
}
function blockRealExecution(target, context) {
    if (dryRunEnabled(context)) {
        return undefined;
    }
    return failure("REAL_EXECUTION_BLOCKED", "office.slidesEncode only returns a guarded dry-run plan in the first implementation", "contract", context, target.outputPath);
}
function actionPreview(target) {
    return [
        "office.slidesEncode",
        "--output",
        target.outputPath,
        "--format",
        target.format,
        "--source-kind",
        target.source.sourceKind,
        "--slides",
        String(target.source.slideCount),
        target.overwrite ? "--overwrite" : "--no-overwrite",
    ];
}
export function planOfficeSlidesEncode(request = {}) {
    const target = normalizeTarget(request.target, request.context);
    if ("ok" in target) {
        return target;
    }
    const scopeFailure = ensureScope(target, request.context);
    if (scopeFailure !== undefined) {
        return scopeFailure;
    }
    const permissionFailure = ensurePermissions(target, request.context);
    if (permissionFailure !== undefined) {
        return permissionFailure;
    }
    const realExecutionFailure = blockRealExecution(target, request.context);
    if (realExecutionFailure !== undefined) {
        return realExecutionFailure;
    }
    return {
        ok: true,
        toolId: officeSlidesEncodeDescriptor.toolId,
        output: {
            kind: "agentCore.basicTool.office.slidesEncode",
            target,
            actionPreview: actionPreview(target),
            dryRun: true,
            executionBlocked: true,
            permissionsRequired: officeSlidesEncodeDescriptor.permissionsRequired,
            unsafeSideEffects: true,
            resultEnvelope: {
                metadata: {
                    encoding: "not-executed",
                    formatFamily: "presentation",
                },
            },
        },
        audit: [
            auditEvent("agentCore.basicTool.office.slidesEncode.dryRun", request.context, target.outputPath, {
                format: target.format,
                overwrite: target.overwrite,
                sourceKind: target.source.sourceKind,
                slideCount: target.source.slideCount,
            }),
        ],
        events: ["basicTool.office.slidesEncode.dryRun"],
    };
}
