"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("@/store/actions/builder/main"));
const toolbarEditorData = {};
const toolbarModalData = {};
const application = {};
const content = {}; // cur page content
const file = {}; // cur page file
const pageContent = {}; // page content
const pageNode = null; // page node
const mock = {}; // cur page mock
const relations = {}; // cur page relations
const extendContent = {};
const formattedData = {}; // formatted data(contains: formatted page content, maps)
const selectedNode = null;
const localVariables = [];
const contentState = {
    file,
    content,
    mock,
    relations,
    formattedData,
    pageContent,
    pageNode,
    extend: extendContent,
    selectedNode,
    curStep: 1,
    stepCount: 0,
    localVariables,
    editStatus: false,
};
const initialState = Object.assign(Object.assign({ 
    // toolbar
    toolbarEditorData, toolbarEditorType: '', toolbarEditorVisible: false, toolbarModalData, toolbarModalType: '', toolbarModalVisible: false, 
    // loading
    loading: false, saveLoading: false, publishLoading: false, 
    // main
    application }, contentState), { 
    // TODO
    templateOpenInPage: false });
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.clearByContentChange): {
            Object.assign(draft, Object.assign({}, contentState));
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.updateLoading): {
            draft.loading = action.payload.loading;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.updatePublishing): {
            draft.publishLoading = 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.pushApp): {
            draft.application = action.payload.data || null;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.pushFile): {
            const { data = [] } = action.payload;
            draft.file = data[0];
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.pushLiveContent): {
            draft.extend = action.payload.data || {};
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.pushContent): {
            const _pageContent = action.payload.data || {};
            const { content, mock, relations } = _pageContent;
            draft.content = lodash_1.default.cloneDeep(content);
            draft.mock = lodash_1.default.cloneDeep(mock);
            draft.relations = lodash_1.default.cloneDeep(relations);
            draft.pageContent = lodash_1.default.cloneDeep(_pageContent);
            // TODO: init page node
            // will be remove until the page node create by component
            if (draft.file.type === 'page') {
                const root = content.schemas[0];
                if (root && root.name === 'page') {
                    draft.pageNode = root;
                }
                else {
                    draft.pageNode = null;
                }
            }
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.pushPageNode): {
            draft.pageNode = action.payload.data;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.updateContent): {
            draft.pageContent = action.payload.data;
            draft.editStatus = true;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.updateEditState): {
            draft.editStatus = action.payload.state;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.pushFormatData): {
            draft.formattedData = lodash_1.default.cloneDeep(action.payload.data);
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.selectComponent): {
            draft.selectedNode = action.payload.params;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.setCurStep): {
            draft.curStep = action.payload.step;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.setSteps): {
            draft.stepCount = action.payload.step;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.pushLocalVariables): {
            draft.localVariables = action.payload.variables;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.updateToolbarEditorVisible): {
            draft.toolbarEditorData = action.payload.data || {};
            draft.toolbarEditorType = action.payload.type || '';
            draft.toolbarEditorVisible = action.payload.open;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.updateToolbarModalVisible): {
            draft.toolbarModalData = action.payload.data || {};
            draft.toolbarModalType = action.payload.type || '';
            draft.toolbarModalVisible = action.payload.open;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.templateOpenInPage): {
            draft.templateOpenInPage = action.payload.status;
            break;
        }
        default:
            break;
    }
});
exports.default = reducer;
