"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/teams/list"));
const list = [];
const defaultEditTeam = {};
const pageInfo = { page: 1, size: 10, total: 0 };
const organizationUsers = [];
const initialState = {
    fetching: false,
    pageInfo,
    list,
    editTeam: defaultEditTeam,
    editDrawerOpen: false,
    userManagementDrawerOpen: false,
    managementTeam: defaultEditTeam,
    managementTeamLoading: false,
    organizationUsers,
};
const conditionReducer = (state = initialState, action) => (0, immer_1.default)(state, (draft) => {
    var _a, _b;
    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.fetching = action.payload.status;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.pushTeamList): {
            draft.list = action.payload.list;
            draft.pageInfo = action.payload.pageInfo;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.openDrawer): {
            draft.editDrawerOpen = action.payload.status;
            draft.editTeam = ((_a = action.payload) === null || _a === void 0 ? void 0 : _a.team) || defaultEditTeam;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.openUserManagementDrawer): {
            draft.managementTeam = ((_b = action.payload) === null || _b === void 0 ? void 0 : _b.team) || defaultEditTeam;
            draft.userManagementDrawerOpen = action.payload.open;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.updateEditTeamValue): {
            const { key, value } = action.payload;
            const newEditTeam = Object.assign({}, draft.editTeam);
            newEditTeam[key] = value;
            draft.editTeam = newEditTeam;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.updateTeamUsersAfterAdd): {
            const { users } = action.payload;
            const managementTeam = lodash_1.default.cloneDeep(draft.managementTeam);
            managementTeam.members = managementTeam.members.concat(users);
            draft.managementTeam = managementTeam;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.updateTeamUsersAfterDelete): {
            const { users } = action.payload;
            const managementTeam = lodash_1.default.cloneDeep(draft.managementTeam);
            managementTeam.members = managementTeam.members.filter((item) => !users.find((user) => item.userId === user.userId));
            draft.managementTeam = managementTeam;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.pushTeamUsers): {
            const { users } = action.payload;
            const managementTeam = lodash_1.default.cloneDeep(draft.managementTeam);
            managementTeam.members = users;
            draft.managementTeam = managementTeam;
            draft.managementTeamLoading = false;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.updateTeamManagementLoading): {
            draft.managementTeamLoading = action.payload.loading;
            break;
        }
        case (0, typesafe_actions_1.getType)(ACTIONS.pushOrganizationUsers): {
            draft.organizationUsers = action.payload.users;
            break;
        }
        default:
            break;
    }
});
exports.default = conditionReducer;
