const TASKBOARD_PROJECT_QUERY_KEY = "tbProjectId";
const TASKBOARD_TASK_QUERY_KEY = "tbTaskId";
const TASKBOARD_VIEW_QUERY_KEY = "tbView";
const TASKBOARD_WIZARD_QUERY_KEY = "tbWizard";
const REVIEW_SIDEBAR_ID = "reviews";
function asNotificationRecord(value) {
    if (!value || typeof value !== "object" || Array.isArray(value))
        return null;
    return value;
}
function getNotificationStringField(record, key) {
    if (!record)
        return null;
    const value = record[key];
    if (typeof value !== "string")
        return null;
    const trimmed = value.trim();
    return trimmed.length > 0 ? trimmed : null;
}
function replaceNotificationUrlParams(params) {
    if (typeof window === "undefined")
        return;
    const currentParams = new URLSearchParams(window.location.search);
    for (const [key, value] of Object.entries(params)) {
        if (value)
            currentParams.set(key, value);
        else
            currentParams.delete(key);
    }
    const queryString = currentParams.toString();
    const newUrl = queryString
        ? `${window.location.pathname}?${queryString}`
        : window.location.pathname;
    window.history.replaceState(null, "", newUrl);
    window.dispatchEvent(new PopStateEvent("popstate"));
}
function createWorkspaceNotificationRoute(options) {
    const { workspaceId, parseContext, buildParams, openSidebars = [] } = options;
    return {
        parseContext,
        open: async (editContext, context) => {
            if (editContext.workspaceId !== workspaceId) {
                editContext.switchWorkspace(workspaceId, { skipNavigationHistory: true });
            }
            for (const sidebarId of openSidebars) {
                editContext.openSidebar(sidebarId);
            }
            replaceNotificationUrlParams({
                workspace: workspaceId,
                ...buildParams(context),
            });
            return true;
        },
    };
}
function parseItemNotificationContext(context) {
    const record = asNotificationRecord(context);
    const itemId = getNotificationStringField(record, "itemId");
    if (!itemId)
        return null;
    const language = getNotificationStringField(record, "language") ?? undefined;
    return { itemId, language };
}
function parseReviewNotificationContext(context) {
    const record = asNotificationRecord(context);
    const reviewId = getNotificationStringField(record, "reviewId");
    if (!reviewId)
        return null;
    return { reviewId };
}
function parseTaskNotificationContext(context) {
    const record = asNotificationRecord(context);
    const projectId = getNotificationStringField(record, "projectId");
    if (!projectId)
        return null;
    const taskId = getNotificationStringField(record, "taskId") ?? undefined;
    return { projectId, taskId };
}
function parseAgentNotificationContext(context) {
    const record = asNotificationRecord(context);
    const agentId = getNotificationStringField(record, "agentId");
    if (!agentId)
        return null;
    return { agentId };
}
export const coreNotificationRoutes = {
    "item-changed": {
        parseContext: parseItemNotificationContext,
        open: async (editContext, context) => {
            if (editContext.workspaceId !== "editor") {
                editContext.switchWorkspace("editor", { skipNavigationHistory: true });
            }
            const loaded = await editContext.loadItem(context.language
                ? { id: context.itemId, language: context.language, version: 0 }
                : context.itemId);
            if (!loaded)
                return false;
            const loadedDescriptor = (loaded.descriptor ?? loaded);
            replaceNotificationUrlParams({
                workspace: "editor",
                itemid: context.itemId,
                lang: context.language,
                language: undefined,
                version: loadedDescriptor?.version != null
                    ? String(loadedDescriptor.version)
                    : undefined,
                reviewId: undefined,
                [TASKBOARD_PROJECT_QUERY_KEY]: undefined,
                [TASKBOARD_TASK_QUERY_KEY]: undefined,
                [TASKBOARD_VIEW_QUERY_KEY]: undefined,
                [TASKBOARD_WIZARD_QUERY_KEY]: undefined,
            });
            return true;
        },
    },
    feedback: {
        parseContext: parseItemNotificationContext,
        open: async (editContext, context) => {
            if (editContext.workspaceId !== "editor") {
                editContext.switchWorkspace("editor", { skipNavigationHistory: true });
            }
            const loaded = await editContext.loadItem(context.language
                ? { id: context.itemId, language: context.language, version: 0 }
                : context.itemId);
            if (!loaded)
                return false;
            const loadedDescriptor = (loaded.descriptor ?? loaded);
            replaceNotificationUrlParams({
                workspace: "editor",
                itemid: context.itemId,
                lang: context.language,
                language: undefined,
                version: loadedDescriptor?.version != null
                    ? String(loadedDescriptor.version)
                    : undefined,
                reviewId: undefined,
                [TASKBOARD_PROJECT_QUERY_KEY]: undefined,
                [TASKBOARD_TASK_QUERY_KEY]: undefined,
                [TASKBOARD_VIEW_QUERY_KEY]: undefined,
                [TASKBOARD_WIZARD_QUERY_KEY]: undefined,
            });
            return true;
        },
    },
    "review-invitation": createWorkspaceNotificationRoute({
        workspaceId: "editor",
        parseContext: parseReviewNotificationContext,
        openSidebars: [REVIEW_SIDEBAR_ID],
        buildParams: ({ reviewId }) => ({
            reviewId,
            [TASKBOARD_PROJECT_QUERY_KEY]: undefined,
            [TASKBOARD_TASK_QUERY_KEY]: undefined,
        }),
    }),
    "review-completed": createWorkspaceNotificationRoute({
        workspaceId: "editor",
        parseContext: parseReviewNotificationContext,
        openSidebars: [REVIEW_SIDEBAR_ID],
        buildParams: ({ reviewId }) => ({
            reviewId,
            [TASKBOARD_PROJECT_QUERY_KEY]: undefined,
            [TASKBOARD_TASK_QUERY_KEY]: undefined,
        }),
    }),
    "review-rejected": createWorkspaceNotificationRoute({
        workspaceId: "editor",
        parseContext: parseReviewNotificationContext,
        openSidebars: [REVIEW_SIDEBAR_ID],
        buildParams: ({ reviewId }) => ({
            reviewId,
            [TASKBOARD_PROJECT_QUERY_KEY]: undefined,
            [TASKBOARD_TASK_QUERY_KEY]: undefined,
        }),
    }),
    "project-attention-required": createWorkspaceNotificationRoute({
        workspaceId: "taskboard",
        parseContext: parseTaskNotificationContext,
        buildParams: ({ projectId, taskId }) => ({
            reviewId: undefined,
            [TASKBOARD_PROJECT_QUERY_KEY]: projectId,
            [TASKBOARD_TASK_QUERY_KEY]: taskId,
            [TASKBOARD_VIEW_QUERY_KEY]: undefined,
        }),
    }),
    "project-task-completed": createWorkspaceNotificationRoute({
        workspaceId: "taskboard",
        parseContext: parseTaskNotificationContext,
        buildParams: ({ projectId, taskId }) => ({
            reviewId: undefined,
            [TASKBOARD_PROJECT_QUERY_KEY]: projectId,
            [TASKBOARD_TASK_QUERY_KEY]: taskId,
            [TASKBOARD_VIEW_QUERY_KEY]: undefined,
        }),
    }),
    "project-finished": createWorkspaceNotificationRoute({
        workspaceId: "taskboard",
        parseContext: parseTaskNotificationContext,
        buildParams: ({ projectId, taskId }) => ({
            reviewId: undefined,
            [TASKBOARD_PROJECT_QUERY_KEY]: projectId,
            [TASKBOARD_TASK_QUERY_KEY]: taskId,
            [TASKBOARD_VIEW_QUERY_KEY]: undefined,
        }),
    }),
    "task-assigned": createWorkspaceNotificationRoute({
        workspaceId: "taskboard",
        parseContext: parseTaskNotificationContext,
        buildParams: ({ projectId, taskId }) => ({
            reviewId: undefined,
            [TASKBOARD_PROJECT_QUERY_KEY]: projectId,
            [TASKBOARD_TASK_QUERY_KEY]: taskId,
            [TASKBOARD_VIEW_QUERY_KEY]: undefined,
        }),
    }),
    "task-status-changed": createWorkspaceNotificationRoute({
        workspaceId: "taskboard",
        parseContext: parseTaskNotificationContext,
        buildParams: ({ projectId, taskId }) => ({
            reviewId: undefined,
            [TASKBOARD_PROJECT_QUERY_KEY]: projectId,
            [TASKBOARD_TASK_QUERY_KEY]: taskId,
            [TASKBOARD_VIEW_QUERY_KEY]: undefined,
        }),
    }),
    "agent-notification": createWorkspaceNotificationRoute({
        workspaceId: "agents",
        parseContext: parseAgentNotificationContext,
        buildParams: ({ agentId }) => ({
            reviewId: undefined,
            [TASKBOARD_PROJECT_QUERY_KEY]: undefined,
            [TASKBOARD_TASK_QUERY_KEY]: undefined,
            [TASKBOARD_VIEW_QUERY_KEY]: undefined,
            [TASKBOARD_WIZARD_QUERY_KEY]: undefined,
            agentId,
        }),
    }),
};
//# sourceMappingURL=notificationRoutes.js.map