"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TeamSharingReducer = exports.TeamSharingCheckForUpdates = exports.TeamSharingUpdateItem = exports.TeamSharingLinkItem = exports.TeamSharingGet = exports.TeamSharingRemoveItem = exports.TeamSharingCommitImport = exports.TeamSharingProcessImport = exports.TeamSharingImportItem = exports.TeamSharingSet = exports.TeamSharingShareCustom = exports.TeamSharingShare = exports.TEAMSHARING_CHECK_FOR_UPDATES = exports.TEAMSHARING_UPDATE_ITEM = exports.TEAMSHARING_LINK_ITEM = exports.TEAMSHARING_REMOVE_ITEM = exports.TEAMSHARING_COMMIT_IMPORT = exports.TEAMSHARING_PROCESS_IMPORT = exports.TEAMSHARING_IMPORT_ITEM = exports.TEAMSHARING_SHARE_CUSTOM = exports.TEAMSHARING_SHARE = exports.TEAMSHARING_SET = exports.TEAMSHARING_GET = void 0;
const GeneralConstants_1 = require("../../Utilities/Constants/GeneralConstants");
exports.TEAMSHARING_GET = 'TEAMSHARING_GET';
exports.TEAMSHARING_SET = 'TEAMSHARING_SET';
exports.TEAMSHARING_SHARE = 'TEAMSHARING_SHARE';
exports.TEAMSHARING_SHARE_CUSTOM = 'TEAMSHARING_SHARE_CUSTOM';
exports.TEAMSHARING_IMPORT_ITEM = 'TEAMSHARING_IMPORT_ITEM';
exports.TEAMSHARING_PROCESS_IMPORT = 'TEAMSHARING_PROCESS_IMPORT';
exports.TEAMSHARING_COMMIT_IMPORT = 'TEAMSHARING_COMMIT_IMPORT';
exports.TEAMSHARING_REMOVE_ITEM = 'TEAMSHARING_REMOVE_ITEM';
exports.TEAMSHARING_LINK_ITEM = 'TEAMSHARING_LINK_ITEM';
exports.TEAMSHARING_UPDATE_ITEM = 'TEAMSHARING_UPDATE_ITEM';
exports.TEAMSHARING_CHECK_FOR_UPDATES = 'TEAMSHARING_CHECK_FOR_UPDATES';
const TeamSharingShare = (entity, module, configuration) => ({
    type: exports.TEAMSHARING_SHARE,
    Entity: entity,
    Module: module,
    Configuration: configuration,
});
exports.TeamSharingShare = TeamSharingShare;
const TeamSharingShareCustom = (entity, configuration) => ({
    type: exports.TEAMSHARING_SHARE_CUSTOM,
    Entity: entity,
    Configuration: configuration,
});
exports.TeamSharingShareCustom = TeamSharingShareCustom;
const TeamSharingSet = (Entities) => ({
    type: exports.TEAMSHARING_SET,
    Entities,
});
exports.TeamSharingSet = TeamSharingSet;
const TeamSharingImportItem = (SharedEntity) => ({
    type: exports.TEAMSHARING_IMPORT_ITEM,
    SharedEntity,
});
exports.TeamSharingImportItem = TeamSharingImportItem;
const TeamSharingProcessImport = (ImportSteps) => ({
    type: exports.TEAMSHARING_PROCESS_IMPORT,
    ImportSteps,
});
exports.TeamSharingProcessImport = TeamSharingProcessImport;
const TeamSharingCommitImport = () => ({
    type: exports.TEAMSHARING_COMMIT_IMPORT,
});
exports.TeamSharingCommitImport = TeamSharingCommitImport;
const TeamSharingRemoveItem = (Uuid) => ({
    type: exports.TEAMSHARING_REMOVE_ITEM,
    Uuid,
});
exports.TeamSharingRemoveItem = TeamSharingRemoveItem;
const TeamSharingGet = () => ({
    type: exports.TEAMSHARING_GET,
});
exports.TeamSharingGet = TeamSharingGet;
const TeamSharingLinkItem = (...SharedEntities) => ({
    type: exports.TEAMSHARING_LINK_ITEM,
    SharedEntities,
});
exports.TeamSharingLinkItem = TeamSharingLinkItem;
const TeamSharingUpdateItem = (ChangedAdaptableObject, UserName) => ({
    type: exports.TEAMSHARING_UPDATE_ITEM,
    ChangedAdaptableObject,
    UserName,
});
exports.TeamSharingUpdateItem = TeamSharingUpdateItem;
const TeamSharingCheckForUpdates = () => ({
    type: exports.TEAMSHARING_CHECK_FOR_UPDATES,
});
exports.TeamSharingCheckForUpdates = TeamSharingCheckForUpdates;
const initialTeamSharingState = {
    SharedEntities: GeneralConstants_1.EMPTY_ARRAY,
    ActiveSharedEntityMap: {},
    importProcessInProgress: false,
};
const TeamSharingReducer = (state = initialTeamSharingState, action) => {
    switch (action.type) {
        case exports.TEAMSHARING_SET: {
            const actionTyped = action;
            const sharedEntities = actionTyped.Entities;
            // keep only the active entities which are still shared (some may have been removed by other users)
            const sharedEntityItemIds = sharedEntities.map((sharedEntity) => sharedEntity.Entity.Uuid);
            const activeSharedEntityMap = {};
            Object.keys(state.ActiveSharedEntityMap).forEach((activeObjectId) => {
                if (sharedEntityItemIds.includes(activeObjectId)) {
                    activeSharedEntityMap[activeObjectId] = state.ActiveSharedEntityMap[activeObjectId];
                }
            });
            return Object.assign({}, state, {
                SharedEntities: sharedEntities,
                ActiveSharedEntityMap: activeSharedEntityMap,
            });
        }
        case exports.TEAMSHARING_LINK_ITEM: {
            const actionTyped = action;
            const newActiveEntities = actionTyped.SharedEntities;
            const activeSharedEntityMap = { ...state.ActiveSharedEntityMap };
            newActiveEntities.forEach((newActiveEntity) => (activeSharedEntityMap[newActiveEntity.Entity.Uuid] = newActiveEntity));
            return Object.assign({}, state, {
                ActiveSharedEntityMap: activeSharedEntityMap,
            });
        }
        case exports.TEAMSHARING_PROCESS_IMPORT: {
            return Object.assign({}, state, {
                importProcessInProgress: true,
            });
        }
        case exports.TEAMSHARING_COMMIT_IMPORT: {
            return Object.assign({}, state, {
                importProcessInProgress: false,
            });
        }
        default:
            return state;
    }
};
exports.TeamSharingReducer = TeamSharingReducer;
