"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const immer_1 = __importDefault(require("immer"));
const lodash_1 = __importDefault(require("lodash"));
const typesafe_actions_1 = require("typesafe-actions");
const ACTIONS = __importStar(require("@/actions/workspace/projects/personal/content"));
const index_1 = require("@/constants/index");
const fileDetail = {};
const contents = [];
const editContent = {};
const baseContents = [];
const extendRecord = {};
const locales = [];
const folder = {};
const authList = [];
const userList = [];
const initialState = {
    loading: false,
    saveLoading: false,
    editorDrawerOpen: false,
    fileDetail,
    contents,
    editContent,
    extendRecord,
    locales,
    baseContents,
    authListDrawerVisible: false,
    authListLoading: false,
    folder,
    authList,
    userList,
};
const reducer = (state = initialState, action) => (0, immer_1.default)(state, (draft) => {
    var _a;
    switch (action.type) {
        case (0, typesafe_actions_1.getType)(ACTIONS.clearAll): {
            Object.assign(draft, Object.assign({}, initialState));
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.updateLoading): {
            draft.loading = action.payload.loading;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.updateSaveLoading): {
            draft.saveLoading = action.payload.loading;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.pushContentList): {
            const { data } = action.payload;
            draft.baseContents = data
                .filter((item) => item.isBase && item.version)
                .map((item) => ({ id: item.id, name: item.title }));
            // sort for group
            if (((_a = draft.fileDetail) === null || _a === void 0 ? void 0 : _a.type) === index_1.FileType.page) {
                const contentRecord = {};
                const extendRecord = {};
                const list = [];
                data.forEach((item) => {
                    if (item.isBase || !item.extendId) {
                        list.push(item);
                    }
                    else {
                        if (!contentRecord[item.extendId]) {
                            contentRecord[item.extendId] = [];
                        }
                        contentRecord[item.extendId].push(item);
                        if (!extendRecord[item.extendId]) {
                            extendRecord[item.extendId] = [];
                        }
                        extendRecord[item.extendId].push(item.id);
                    }
                });
                Object.keys(contentRecord).forEach((key) => {
                    const children = contentRecord[key];
                    if (children && children.length > 0) {
                        const idx = list.findIndex((item) => item.id === key);
                        list.splice(idx + 1, 0, ...children);
                    }
                });
                draft.extendRecord = extendRecord;
                draft.contents = list;
            }
            else {
                draft.contents = data;
            }
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.openEditDrawer): {
            const { open, editContent = {} } = action.payload;
            draft.editorDrawerOpen = open;
            draft.editContent = editContent;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.updateEditContentValue): {
            const { key, value } = action.payload;
            const editContent = draft.editContent;
            draft.editContent = Object.assign({}, editContent, { [key]: value });
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.updateEditContentTags): {
            const { key, value } = action.payload;
            const editContent = draft.editContent;
            const newEditContent = (lodash_1.default.cloneDeep(editContent) || {});
            const { tags = [] } = newEditContent;
            const tagIndex = tags.findIndex((item) => key in item);
            if (tagIndex > -1) {
                if (value) {
                    tags[tagIndex][key] = value;
                }
                else {
                    tags.splice(tagIndex, 1);
                }
            }
            else if (value) {
                tags.push({ [key]: value });
            }
            draft.editContent = newEditContent;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.pushLocales): {
            const { locales } = action.payload;
            draft.locales = locales;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.pushFileDetail): {
            const { data } = action.payload;
            draft.fileDetail = data;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.pushParentFiles): {
            draft.folder = action.payload.folder;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.updateFileOnlineStatus): {
            const { online } = action.payload;
            draft.fileDetail = Object.assign({}, draft.fileDetail, { online });
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.updateAuthDrawerVisible): {
            const { visible = false, editContent = {} } = action.payload;
            draft.authListDrawerVisible = visible;
            draft.editContent = editContent;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.updateAuthListLoading): {
            draft.authListLoading = action.payload.status;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.pushAuthList): {
            draft.authList = action.payload.list;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.pushUserList): {
            draft.userList = action.payload.list;
            break;
        }
        default:
            break;
    }
});
exports.default = reducer;
