"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/store/list"));
const index_1 = require("@/constants/index");
const selectedItem = {};
const projectResourceList = [];
const packageResourceList = [];
const variableResourceList = [];
const pageInfo = { page: 1, size: 16, total: 0 };
const buyIds = [];
const applicationList = [];
const allApplicationList = [];
const initialState = {
    loading: false,
    previewModalVisible: false,
    projectResourceList,
    packageResourceList,
    variableResourceList,
    selectedItem,
    buyModalVisible: false,
    buyIds,
    pageInfo,
    searchText: '',
    selectedAppIds: buyIds,
    type: index_1.FileType.page,
    applicationList,
    allApplicationList,
};
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): {
            const { loading } = action.payload;
            draft.loading = loading;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.pushProjectResources): {
            const { list, pageInfo } = action.payload;
            draft.projectResourceList = list;
            draft.packageResourceList = [];
            draft.variableResourceList = [];
            draft.pageInfo = pageInfo;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.pushPackageResources): {
            const { list, pageInfo } = action.payload;
            draft.packageResourceList = list;
            draft.projectResourceList = [];
            draft.variableResourceList = [];
            draft.pageInfo = pageInfo;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.pushVariableResources): {
            const { list, pageInfo } = action.payload;
            draft.variableResourceList = list;
            draft.packageResourceList = [];
            draft.projectResourceList = [];
            draft.pageInfo = pageInfo;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.updateBuyModalVisible): {
            const { visible, ids } = action.payload;
            draft.buyIds = ids || [];
            draft.buyModalVisible = visible;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.updatePreviewModalVisible): {
            const { visible, resourceItem } = action.payload;
            draft.selectedItem = resourceItem;
            draft.previewModalVisible = visible;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.updateSearchText): {
            const { text } = action.payload;
            draft.searchText = text;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.updateSelectedAppIds): {
            const { ids } = action.payload;
            draft.selectedAppIds = ids;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.updateType): {
            const { type } = action.payload;
            draft.type = type;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.updateProjectResourceItemChecked): {
            const { id } = action.payload;
            const newResourceList = lodash_1.default.cloneDeep(draft.projectResourceList);
            newResourceList.forEach((item) => {
                if (item.id === id) {
                    item.checked = !item.checked;
                }
            });
            draft.projectResourceList = newResourceList;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.updatePackageResourceItemChecked): {
            const { id } = action.payload;
            const newResourceList = lodash_1.default.cloneDeep(draft.packageResourceList);
            newResourceList.forEach((item) => {
                if (item.id === id) {
                    item.checked = !item.checked;
                }
            });
            draft.packageResourceList = newResourceList;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.updateVariableResourceItemChecked): {
            const { id } = action.payload;
            const newResourceList = lodash_1.default.cloneDeep(draft.variableResourceList);
            newResourceList.forEach((item) => {
                if (item.id === id) {
                    item.checked = !item.checked;
                }
            });
            draft.variableResourceList = newResourceList;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.pushApplicationList): {
            draft.applicationList = action.payload.list || [];
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.pushAllApplicationList): {
            draft.allApplicationList = action.payload.list;
            break;
        }
        default:
            break;
    }
});
exports.default = reducer;
