"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSharedEntityStaleDepsItemView = exports.getSharedEntityActiveStatusObjectView = exports.SharedEntityTypeItemView = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const rebass_1 = require("rebass");
const SimpleButton_1 = tslib_1.__importDefault(require("../../components/SimpleButton"));
const Tag_1 = require("../../components/Tag");
const AdaptableContext_1 = require("../AdaptableContext");
const AdaptableObjectList_1 = require("../Components/AdaptableObjectList/AdaptableObjectList");
const SharedEntityTypeItemView = (props) => {
    const [isExpanded, setIsExpanded] = React.useState(false);
    const adaptable = (0, AdaptableContext_1.useAdaptable)();
    const moduleName = props.data.Module;
    const module = adaptable.ModuleService.getModuleById(moduleName);
    const sharedObject = props.data.Entity;
    const item = module?.toView(sharedObject);
    return (React.createElement(React.Fragment, null,
        React.createElement(rebass_1.Flex, { mb: 2, mt: 2 },
            React.createElement(rebass_1.Flex, { mr: 2, alignItems: "center" },
                React.createElement(Tag_1.Tag, { style: { margin: 0 } }, props.data.Module)),
            item ? (isExpanded ? (React.createElement(SimpleButton_1.default, { "data-name": "shared-entity-collapse-button", onClick: () => setIsExpanded(false), icon: "arrow-up" }, "Collapse")) : (React.createElement(SimpleButton_1.default, { "data-name": "shared-entity-expand-button", onClick: () => setIsExpanded(true), icon: "arrow-down" }, "Expand"))) : null),
        isExpanded && item && (React.createElement(rebass_1.Box, { as: "ul", padding: 0, className: "ab-Shared-Entity-Shared-Object" },
            React.createElement(AdaptableObjectList_1.AdaptableObjectListItem, { hideControls: true, data: item, module: module })))));
};
exports.SharedEntityTypeItemView = SharedEntityTypeItemView;
const getSharedEntityActiveStatusObjectView = (isDependency) => (props) => {
    const adaptable = (0, AdaptableContext_1.useAdaptable)();
    const activeEntries = adaptable.api.internalApi.getState().TeamSharing.ActiveSharedEntityMap;
    const sharedEntity = props.data;
    const isAdaptableObjectPresentInLocalState = !!adaptable.api.internalApi
        .getModuleService()
        .getModuleById('TeamSharing')
        .isAdaptableObjectPresentInLocalState(sharedEntity);
    const isUpToDateAndActive = isAdaptableObjectPresentInLocalState &&
        sharedEntity.Type === 'Active' &&
        activeEntries[sharedEntity.Entity.Uuid]?.Revision === sharedEntity.Revision;
    const staleActiveEntities = adaptable.api.internalApi
        .getTeamSharingService()
        .getStaleActiveSharedEntities();
    const isStaleAndActive = isAdaptableObjectPresentInLocalState && !!staleActiveEntities[sharedEntity.Uuid];
    let activeInfo = null;
    if (isAdaptableObjectPresentInLocalState) {
        const activeRevision = activeEntries?.[sharedEntity.Entity.Uuid]?.Revision;
        if (sharedEntity.Type === 'Active') {
            const newRevision = sharedEntity.Revision;
            let statusInfo = null;
            if (isUpToDateAndActive) {
                statusInfo = (React.createElement(rebass_1.Box, { ml: 1, style: { color: 'var(--ab-color-success)' } },
                    React.createElement("b", null, "up-to-date")));
            }
            if (isStaleAndActive) {
                statusInfo = (React.createElement(rebass_1.Box, { ml: 1, style: { color: 'var(--ab-color-warn)' } }, ` (Rev. ${newRevision} available)`));
            }
            activeInfo = activeRevision && (React.createElement(React.Fragment, null,
                activeRevision &&
                    `Imported Rev. ${activeRevision} ${isStaleAndActive ? 'is stale' : ''} `,
                ' ',
                statusInfo));
        }
        else {
            // snapshot
            activeInfo = activeRevision && React.createElement(React.Fragment, null, `Imported Rev. ${activeRevision}`);
        }
    }
    const sharedValue = `by ${sharedEntity.UserName} at ${new Date(sharedEntity.Timestamp).toLocaleString()}`;
    return (React.createElement(React.Fragment, null,
        !isDependency && React.createElement(Tag_1.Tag, null, sharedValue),
        activeInfo && React.createElement(Tag_1.Tag, null, activeInfo)));
};
exports.getSharedEntityActiveStatusObjectView = getSharedEntityActiveStatusObjectView;
const getSharedEntityStaleDepsItemView = (staleDependencies) => () => {
    let staleDepsString = 'Shared entity has';
    if (staleDependencies?.length === 1) {
        staleDepsString += ` one (${staleDependencies[0].Module}) stale dependency`;
    }
    else {
        staleDepsString += ` ${staleDependencies.length} stale dependencies`;
    }
    return (React.createElement(Tag_1.Tag, null,
        React.createElement(rebass_1.Box, { ml: 1, style: { color: 'var(--ab-color-warn)' } }, staleDepsString)));
};
exports.getSharedEntityStaleDepsItemView = getSharedEntityStaleDepsItemView;
