"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TeamSharingApiImpl = void 0;
const tslib_1 = require("tslib");
const ModuleConstants = tslib_1.__importStar(require("../../Utilities/Constants/ModuleConstants"));
const ApiBase_1 = require("./ApiBase");
const TeamSharingRedux = tslib_1.__importStar(require("../../Redux/ActionsReducers/TeamSharingRedux"));
const TeamSharingRedux_1 = require("../../Redux/ActionsReducers/TeamSharingRedux");
const TeamSharingInternalApi_1 = require("../Internal/TeamSharingInternalApi");
const TeamSharingState_1 = require("../../AdaptableState/TeamSharingState");
class TeamSharingApiImpl extends ApiBase_1.ApiBase {
    constructor(_adaptable) {
        super(_adaptable);
        this.internalApi = new TeamSharingInternalApi_1.TeamSharingInternalApi(_adaptable);
    }
    async loadSharedEntities() {
        if (!this.isTeamSharingAvailable()) {
            this.logWarn('Team Sharing is not Activated so Entities cannot be loaded');
            return [];
        }
        const teamSharingOptions = this.getTeamSharingOptions();
        const sharedEntitiesContext = this.getAdaptableInternalApi().buildBaseContext();
        let loadedSharedEntities = await teamSharingOptions.loadSharedEntities(sharedEntitiesContext);
        // with v15 we introduced 2 types of SharedEntity: AdaptableSharedEntity (basically the old one) and the new CustomSharedEntity
        // for backwards compatibility, we add default the 'EntityType' property to 'adaptableEntity'
        loadedSharedEntities = loadedSharedEntities.map((sharedEntity) => {
            if (sharedEntity.EntityType !== 'adaptableEntity' &&
                sharedEntity.EntityType !== 'customEntity') {
                // @ts-ignore
                sharedEntity.EntityType = 'adaptableEntity';
            }
            return sharedEntity;
        });
        const sharedEntities = teamSharingOptions.applySharedEntities(loadedSharedEntities, sharedEntitiesContext);
        return sharedEntities;
    }
    async persistSharedEntities(sharedEntities) {
        if (!this.isTeamSharingAvailable()) {
            this.logWarn('Team Sharing is not Activated so Entities cannot be shared');
            return false;
        }
        const teamSharingOptions = this.getTeamSharingOptions();
        const sharedEntitiesContext = this.getAdaptableInternalApi().buildBaseContext();
        const sharedEntitiesAfterCustomisation = teamSharingOptions.saveSharedEntities(sharedEntities, sharedEntitiesContext);
        await teamSharingOptions.persistSharedEntities(sharedEntitiesAfterCustomisation, sharedEntitiesContext);
        return true;
    }
    isTeamSharingAvailable() {
        return this._adaptable.ModuleService.isAdapTableModulePresent('TeamSharing');
    }
    hasTeamSharingFullRights() {
        return this.getEntitlementApi().isModuleFullEntitlement(ModuleConstants.TeamSharingModuleId);
    }
    checkForUpdates() {
        this.dispatchAction((0, TeamSharingRedux_1.TeamSharingCheckForUpdates)());
    }
    openTeamSharingSettingsPanel() {
        this.showModulePopup(ModuleConstants.TeamSharingModuleId);
    }
    shareAdaptableEntity(adaptableObject, module, sharedEntityConfig) {
        if (!this.isTeamSharingAvailable()) {
            this.logWarn('Team Sharing is not Activated so Entities cannot be shared');
            return;
        }
        this.dispatchAction(TeamSharingRedux.TeamSharingShare(adaptableObject, module, sharedEntityConfig));
    }
    shareCustomEntity(customObject, sharedEntityConfig) {
        if (!this.isTeamSharingAvailable()) {
            this.logWarn('Team Sharing is not Activated so Entities cannot be shared');
            return;
        }
        this.dispatchAction(TeamSharingRedux.TeamSharingShareCustom(customObject, sharedEntityConfig));
    }
    unshareEntity(entityId) {
        if (!this.isTeamSharingAvailable()) {
            this.logWarn('Team Sharing is not Activated so Entities cannot be unshared');
            return;
        }
        this.dispatchAction(TeamSharingRedux.TeamSharingRemoveItem(entityId));
    }
    importSharedEntry(sharedEntity) {
        if ((0, TeamSharingState_1.isAdaptableSharedEntity)(sharedEntity)) {
            this.dispatchAction(TeamSharingRedux.TeamSharingImportItem(sharedEntity));
        }
        else {
            const customSharedEntityImportHandler = this.getTeamSharingOptions().handleCustomSharedEntityImport;
            if (!customSharedEntityImportHandler) {
                this.logError(`Could NOT import custom shared entity because 'TeamSharingOptions.handleCustomSharedEntityImport(...)' is NOT defined!`, sharedEntity);
                return;
            }
            customSharedEntityImportHandler(sharedEntity, this.getAdaptableInternalApi().buildBaseContext());
        }
    }
    getTeamSharingState() {
        return this.getAdaptableState().TeamSharing;
    }
    getLoadedSharedEntities() {
        return this.getTeamSharingState().SharedEntities;
    }
    getLoadedAdaptableSharedEntities() {
        return this.getLoadedSharedEntities().filter(TeamSharingState_1.isAdaptableSharedEntity);
    }
    getLoadedCustomSharedEntities() {
        return this.getLoadedSharedEntities().filter(TeamSharingState_1.isCustomSharedEntity);
    }
    refreshTeamSharing() {
        this.dispatchAction(TeamSharingRedux.TeamSharingGet());
    }
}
exports.TeamSharingApiImpl = TeamSharingApiImpl;
