"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/builder/header"));
const backState = {};
const dsl = {};
const fileDetail = {};
const pageList = [];
const storeModalPageInfo = { page: 1, size: 8, total: 0 };
const storeModalTemplates = [];
const storeModalTemplatesBackup = [];
const initialState = {
    fileDetail,
    pageList,
    folderId: '',
    fileId: '',
    applicationId: '',
    contentId: '',
    fileType: '',
    loading: false,
    locale: '',
    project: '',
    // store related
    storeModalTemplates,
    storeModalTemplatesBackup,
    storeModalPageInfo,
    storeLoading: false,
    storeModalType: '',
    storeModalVisible: false,
    // dsl related
    dslLoading: false,
    dsl,
    dslModalVisible: false,
    // mock related
    mockLoading: false,
    mockModalVisible: false,
    mockId: '',
    // go back
    backState,
};
const reducer = (state = initialState, action) => (0, immer_1.default)(state, (draft) => {
    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.updateLocale): {
            draft.locale = action.payload.locale;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.pushCatalog): {
            const { data } = action.payload;
            const { files, name } = data || {};
            draft.pageList =
                (files &&
                    files.map((item) => (Object.assign(Object.assign({}, item), { fold: true })))) ||
                    [];
            draft.project = name || '';
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.updateFileFold): {
            const { id, fold } = action.payload;
            const pageList = state.pageList;
            const newPageList = lodash_1.default.cloneDeep(pageList);
            newPageList.forEach((item) => {
                if (item.id === id) {
                    item.fold = fold;
                }
                if (item.contents && item.contents.length > 0) {
                    item.contents.forEach((subItem) => {
                        if (subItem.id === id) {
                            subItem.fold = fold;
                        }
                    });
                }
            });
            draft.pageList = newPageList;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.updateContentInfo): {
            const { applicationId, folderId, fileId, contentId, fileType } = action.payload;
            const _applicationId = state.applicationId;
            const _contentId = state.contentId;
            const _fileId = state.fileId;
            const _fileType = state.fileType;
            const _folderId = state.folderId;
            draft.applicationId = applicationId || _applicationId;
            draft.contentId = contentId || _contentId;
            draft.fileId = fileId || _fileId;
            draft.fileType = fileType || _fileType;
            draft.folderId = folderId || _folderId;
            break;
        }
        // store related
        case (0, typesafe_actions_1.getType)(ACTIONS.updateStoreLoading): {
            draft.storeLoading = action.payload.loading;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.updateStoreModalVisible): {
            draft.storeModalType = action.payload.type || 'page';
            draft.storeModalVisible = action.payload.open;
            draft.storeModalTemplatesBackup = [];
            draft.storeModalTemplates = [];
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.pushPageTemplate): {
            const { list, pageInfo } = action.payload;
            draft.storeModalTemplates = list;
            draft.storeModalPageInfo = pageInfo;
            break;
        }
        // dsl related
        case (0, typesafe_actions_1.getType)(ACTIONS.updateDSLLoading): {
            draft.dslLoading = action.payload.loading;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.updateDSLModalVisible): {
            draft.dslModalVisible = action.payload.open;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.pushDsl): {
            draft.dsl = action.payload.dsl;
            break;
        }
        // mock related
        case (0, typesafe_actions_1.getType)(ACTIONS.updateMockLoading): {
            draft.mockLoading = action.payload.loading;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.updateMockModalVisible): {
            draft.mockModalVisible = action.payload.open;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.updateMockId): {
            draft.mockId = action.payload.id;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.updateBackState): {
            draft.backState = action.payload.state || undefined;
            break;
        }
        default:
            break;
    }
});
exports.default = reducer;
