"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const PerFolderSortOrderService_1 = require("./PerFolderSortOrderService");
const notesSortOrderUtils_1 = require("./notesSortOrderUtils");
const Setting_1 = require("../../models/Setting");
const reducer_1 = require("../../reducer");
const eventManager_1 = require("../../eventManager");
const { shimInit } = require('../../shim-init-node.js');
const { ALL_NOTES_FILTER_ID } = require('../../reserved-ids');
const folderId1 = 'aa012345678901234567890123456789';
const folderId2 = 'bb012345678901234567890123456789';
let appState = null;
const updateAppState = (update) => {
    appState = Object.assign(Object.assign({}, appState), update);
    eventManager_1.default.appStateEmit(appState);
};
const switchToFolder = (id) => {
    updateAppState({
        notesParentType: 'Folder',
        selectedFolderId: id,
    });
};
const switchToAllNotes = () => {
    updateAppState({
        notesParentType: 'SmartFilter',
        selectedSmartFilterId: ALL_NOTES_FILTER_ID,
    });
};
describe('PerFolderSortOrderService', () => {
    beforeAll(async () => {
        shimInit();
        Setting_1.default.autoSaveEnabled = false;
    });
    beforeEach(() => {
        PerFolderSortOrderService_1.default.initialize();
        Setting_1.default.setValue('notes.perFolderSortOrderEnabled', true);
        updateAppState(reducer_1.defaultState);
        switchToFolder(folderId1);
    });
    afterEach(() => {
        Setting_1.default.setValue('notes.perFolderSortOrders', {});
    });
    test('get(), isSet() and set()', async () => {
        // Clear all per-folder sort order
        expect(PerFolderSortOrderService_1.default.isSet(folderId1)).toBe(false);
        expect(PerFolderSortOrderService_1.default.isSet(folderId2)).toBe(false);
        // Set shared sort order
        (0, notesSortOrderUtils_1.setNotesSortOrder)('user_created_time', false);
        expect(Setting_1.default.value('notes.sortOrder.field')).toBe('user_created_time');
        expect(Setting_1.default.value('notes.sortOrder.reverse')).toBe(false);
        // Manipulate per-folder sort order
        PerFolderSortOrderService_1.default.set(folderId1, true);
        expect(PerFolderSortOrderService_1.default.isSet(folderId1)).toBe(true);
        PerFolderSortOrderService_1.default.set(folderId1, false);
        expect(PerFolderSortOrderService_1.default.isSet(folderId1)).toBe(false);
        PerFolderSortOrderService_1.default.set(folderId1);
        expect(PerFolderSortOrderService_1.default.isSet(folderId1)).toBe(true);
        // Get per-folder sort order from a folder with per-folder sort order
        expect(PerFolderSortOrderService_1.default.get(folderId1)).toBeDefined();
        // Folder without per-folder sort order has no per-folder sort order
        expect(PerFolderSortOrderService_1.default.get(folderId2)).toBeUndefined();
    });
    test('should allow specifying a sort order specific to a folder', () => {
        switchToFolder(folderId1);
        expect(PerFolderSortOrderService_1.default.isSet(folderId1)).toBe(false);
        expect(PerFolderSortOrderService_1.default.isSet(folderId2)).toBe(false);
        (0, notesSortOrderUtils_1.setNotesSortOrder)('user_created_time', false);
        expect(Setting_1.default.value('notes.sortOrder.field')).toBe('user_created_time');
        expect(Setting_1.default.value('notes.sortOrder.reverse')).toBe(false);
        // Folder 2 should use the shared sort order.
        switchToFolder(folderId2);
        expect(Setting_1.default.value('notes.sortOrder.field')).toBe('user_created_time');
        expect(Setting_1.default.value('notes.sortOrder.reverse')).toBe(false);
        // If changing the per-folder sort order for folder 1, folder 2 should continue
        // to use the shared sort order.
        switchToFolder(folderId1);
        PerFolderSortOrderService_1.default.set(folderId1, true);
        (0, notesSortOrderUtils_1.setNotesSortOrder)('title', true);
        expect(Setting_1.default.value('notes.sortOrder.field')).toBe('title');
        expect(Setting_1.default.value('notes.sortOrder.reverse')).toBe(true);
        switchToFolder(folderId2);
        expect(Setting_1.default.value('notes.sortOrder.field')).toBe('user_created_time');
        expect(Setting_1.default.value('notes.sortOrder.reverse')).toBe(false);
    });
    test('should allow setting a sort order specific to All Notes', () => {
        switchToFolder(folderId1);
        expect(PerFolderSortOrderService_1.default.isSet(ALL_NOTES_FILTER_ID)).toBe(false);
        expect(PerFolderSortOrderService_1.default.isSet(folderId1)).toBe(false);
        // Set default shared sort order
        (0, notesSortOrderUtils_1.setNotesSortOrder)('user_created_time', false);
        expect(Setting_1.default.value('notes.sortOrder.field')).toBe('user_created_time');
        expect(Setting_1.default.value('notes.sortOrder.reverse')).toBe(false);
        // Switching to all notes should not change the default sort order.
        switchToAllNotes();
        expect(Setting_1.default.value('notes.sortOrder.field')).toBe('user_created_time');
        expect(Setting_1.default.value('notes.sortOrder.reverse')).toBe(false);
        // It should be possible to enable per-folder sorting for all notes.
        PerFolderSortOrderService_1.default.set(ALL_NOTES_FILTER_ID, true);
        expect(PerFolderSortOrderService_1.default.isSet(ALL_NOTES_FILTER_ID)).toBe(true);
        (0, notesSortOrderUtils_1.setNotesSortOrder)('user_updated_time', true);
        // Per-folder sorting should be respected for all notes
        switchToFolder(folderId1);
        expect(Setting_1.default.value('notes.sortOrder.field')).toBe('user_created_time');
        expect(Setting_1.default.value('notes.sortOrder.reverse')).toBe(false);
        // Shared sort order should be overriden by per-folder sorting
        (0, notesSortOrderUtils_1.setNotesSortOrder)('title', false);
        switchToAllNotes();
        expect(Setting_1.default.value('notes.sortOrder.field')).toBe('user_updated_time');
        expect(Setting_1.default.value('notes.sortOrder.reverse')).toBe(true);
    });
    test('should not let All Notes sort bleed into shared sort order on relaunch', () => {
        switchToFolder(folderId1);
        (0, notesSortOrderUtils_1.setNotesSortOrder)('title', false);
        switchToAllNotes();
        PerFolderSortOrderService_1.default.set(ALL_NOTES_FILTER_ID, true);
        (0, notesSortOrderUtils_1.setNotesSortOrder)('user_updated_time', true);
        expect(PerFolderSortOrderService_1.default.isSet(ALL_NOTES_FILTER_ID)).toBe(true);
        expect(Setting_1.default.value('notes.sortOrder.field')).toBe('user_updated_time');
        // Simulate relaunch: activeFolderId holds the last notebook, notesParent holds All Notes
        Setting_1.default.setValue('activeFolderId', folderId1);
        Setting_1.default.setValue('notesParent', (0, reducer_1.serializeNotesParent)({ type: 'SmartFilter', selectedItemId: ALL_NOTES_FILTER_ID }));
        PerFolderSortOrderService_1.default.initialize();
        Setting_1.default.setValue('notes.perFolderSortOrderEnabled', true);
        updateAppState(reducer_1.defaultState);
        switchToAllNotes();
        // Navigating to the notebook must restore shared sort (title), not All Notes' sort
        switchToFolder(folderId1);
        expect(Setting_1.default.value('notes.sortOrder.field')).toBe('title');
        expect(Setting_1.default.value('notes.sortOrder.reverse')).toBe(false);
    });
});
//# sourceMappingURL=PerFolderSortOrderService.test.js.map