"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 typesafe_actions_1 = require("typesafe-actions");
const ACTIONS = __importStar(require("@/store/actions/applications/detail/packages/detail"));
const fileDetail = {};
const componentInfo = {};
const versionDrawer = { open: false, type: 'add', data: {} };
const cloudSyncDrawer = {
    open: false,
    loading: false,
    componentRemote: {},
    lastVersion: {},
};
const versionList = {
    applicationId: '',
    fileId: '',
    loading: false,
    versions: [],
    pageInfo: { total: 0, page: 1, size: 1000 },
};
const initialState = {
    fileDetail,
    cloudSyncDrawer,
    compInfoLoading: false,
    componentInfo,
    versionList,
    versionDrawer,
};
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.updateDetailState): {
            const { params = {} } = action.payload;
            Object.assign(draft, params);
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.resetComponentInfoState): {
            draft.componentInfo = initialState.componentInfo;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.updateComponentInfoState): {
            const { params = {} } = action.payload;
            Object.assign(draft.componentInfo, params);
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.resetVersionListState): {
            draft.versionList = initialState.versionList;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.updateVersionListState): {
            const { params = {} } = action.payload;
            Object.assign(draft.versionList, params);
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.resetVersionDrawerState): {
            draft.versionDrawer = initialState.versionDrawer;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.updateVersionDrawerState): {
            const { params = {} } = action.payload;
            Object.assign(draft.versionDrawer, params);
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.updateCloudSyncDrawerOpenStatus): {
            const { open } = action.payload;
            if (!open) {
                draft.cloudSyncDrawer = initialState.cloudSyncDrawer;
            }
            else {
                draft.cloudSyncDrawer.open = open;
            }
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.updateCloudSyncDrawerLoadingStatus): {
            const { open } = action.payload;
            draft.cloudSyncDrawer.loading = open;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.updateComponentOnlineStatus): {
            const { online } = action.payload;
            const componentInfo = Object.assign({}, draft.componentInfo);
            componentInfo.online = online;
            draft.componentInfo = componentInfo;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.fetchComponentRemotes): {
            draft.cloudSyncDrawer.loading = true;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.pushComponentRemotes): {
            const { remote, lastVersion } = action.payload;
            draft.cloudSyncDrawer.componentRemote = remote;
            draft.cloudSyncDrawer.lastVersion = lastVersion;
            draft.cloudSyncDrawer.loading = false;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.pushFileDetail): {
            const { data } = action.payload;
            draft.fileDetail = data;
            break;
        }
        default:
            break;
    }
});
exports.default = reducer;
