/**
 * Long-running edit ops (e.g. copy-items) return from HTTP before CreatedItemIds exist.
 * EditorShell sends operation:completed over the socket with the final payload; we bridge
 * that to a window event so operations.duplicateItem / copyItems can await the result.
 */
export const EDIT_OPERATION_TERMINAL_EVENT = "parhelia:edit-operation-terminal";
export function editOperationNeedsTerminalCompletion(result) {
    if (result.errorMessage)
        return false;
    const ids = result.createdItemIds;
    if (Array.isArray(ids) && ids.length > 0)
        return false;
    return (result.executionStatus === "executing" || result.isLongRunning === true);
}
export const copyItemsNeedsTerminalCompletion = editOperationNeedsTerminalCompletion;
function normalizeOpId(id) {
    return String(id).replace(/[{}]/g, "").toLowerCase();
}
export function waitForEditOperationTerminal(operationId, timeoutMs) {
    const want = normalizeOpId(operationId);
    return new Promise((resolve, reject) => {
        const timeout = setTimeout(() => {
            window.removeEventListener(EDIT_OPERATION_TERMINAL_EVENT, handler);
            reject(new Error("Operation timed out"));
        }, timeoutMs);
        const handler = (ev) => {
            const d = ev.detail;
            const oid = d.operationId ?? d.id;
            if (!oid || normalizeOpId(oid) !== want)
                return;
            clearTimeout(timeout);
            window.removeEventListener(EDIT_OPERATION_TERMINAL_EVENT, handler);
            resolve({
                createdItemIds: d.createdItemIds,
                errorMessage: d.errorMessage,
                successCount: d.successCount,
                failureCount: d.failureCount,
                summary: d.summary,
            });
        };
        window.addEventListener(EDIT_OPERATION_TERMINAL_EVENT, handler);
    });
}
//# sourceMappingURL=waitForEditOperationTerminal.js.map