const defaultState = {
    projectTitle: "Tasks",
    projectSubtitle: "Select a project to get started",
    permissionLabel: "No project selected",
    activeTab: 0,
    isAdministrator: false,
    showAllProjects: false,
    isProjectsLoading: false,
    selectedProjectId: null,
    selectedTaskId: null,
    projectOptions: [],
    hasSelectedProject: false,
    canCreateTask: false,
    canRunOrchestrator: false,
    runningOrchestrator: false,
    canManageProject: false,
    myTasks: [],
    myTasksLoading: false,
    onSelectProject: undefined,
    onRefreshProjects: undefined,
    onSelectMyTask: undefined,
    onSetActiveTab: undefined,
    onCreateProject: undefined,
    onOpenProjectTemplates: undefined,
    onOpenProjectSettings: undefined,
    onDeleteProject: undefined,
    onRestartProject: undefined,
    onToggleShowAllProjects: undefined,
    onCreateTask: undefined,
    onRunOrchestrator: undefined,
};
const defaultReturnState = {
    selectedProjectId: null,
    selectedTaskId: null,
    activeTab: 1,
};
let state = defaultState;
let returnState = defaultReturnState;
const listeners = new Set();
function emit() {
    listeners.forEach((listener) => listener());
}
function hasOwn(object, key) {
    return Object.prototype.hasOwnProperty.call(object, key);
}
function persistTaskBoardReturnState(partial) {
    const hasProject = hasOwn(partial, "selectedProjectId");
    const hasTask = hasOwn(partial, "selectedTaskId");
    const hasTab = hasOwn(partial, "activeTab");
    if (!hasProject && !hasTask && !hasTab)
        return;
    const previousProjectId = returnState.selectedProjectId;
    const selectedProjectId = hasProject
        ? (partial.selectedProjectId ?? null)
        : returnState.selectedProjectId;
    let selectedTaskId = hasTask
        ? (partial.selectedTaskId ?? null)
        : returnState.selectedTaskId;
    if (!selectedProjectId ||
        (hasProject && selectedProjectId !== previousProjectId && !hasTask)) {
        selectedTaskId = null;
    }
    const activeTab = hasTab &&
        typeof partial.activeTab === "number" &&
        Number.isFinite(partial.activeTab)
        ? partial.activeTab
        : returnState.activeTab;
    returnState = {
        selectedProjectId,
        selectedTaskId,
        activeTab,
    };
}
export function subscribeTaskBoardNav(listener) {
    listeners.add(listener);
    return () => listeners.delete(listener);
}
export function getTaskBoardNavSnapshot() {
    return state;
}
export function setTaskBoardNavState(partial) {
    persistTaskBoardReturnState(partial);
    state = { ...state, ...partial };
    emit();
}
export function resetTaskBoardNavState() {
    state = defaultState;
    emit();
}
export function getTaskBoardReturnState() {
    return returnState;
}
export function resetTaskBoardReturnState() {
    returnState = defaultReturnState;
}
//# sourceMappingURL=taskBoardNavStore.js.map