import * as React from 'react';
import SimpleButton from '../../components/SimpleButton';
import { Tag } from '../../components/Tag';
import { useAdaptable } from '../AdaptableContext';
import { AdaptableObjectListItem } from '../Components/AdaptableObjectList/AdaptableObjectList';
import { Box, Flex } from '../../components/Flex';
export const SharedEntityTypeItemView = (props) => {
    const [isExpanded, setIsExpanded] = React.useState(false);
    const adaptable = 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(Flex, { className: "twa:my-2" },
            React.createElement(Flex, { className: "twa:mr-2", alignItems: "center" },
                React.createElement(Tag, { className: "twa:m-0" }, props.data.Module)),
            item ? (isExpanded ? (React.createElement(SimpleButton, { "data-name": "shared-entity-collapse-button", onClick: () => setIsExpanded(false), icon: "arrow-up" }, "Collapse")) : (React.createElement(SimpleButton, { "data-name": "shared-entity-expand-button", onClick: () => setIsExpanded(true), icon: "arrow-down" }, "Expand"))) : null),
        isExpanded && item && (React.createElement(Box, { as: "ul", className: "ab-Shared-Entity-Shared-Object twa:p-0 twa:flex twa:flex-col twa:gap-3" },
            React.createElement(AdaptableObjectListItem, { hideControls: true, data: item, module: module })))));
};
export const getSharedEntityActiveStatusObjectView = (isDependency) => (props) => {
    const adaptable = 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(Box, { className: "twa:ml-1 twa:text-success" },
                    React.createElement("b", null, "up-to-date")));
            }
            if (isStaleAndActive) {
                statusInfo = (React.createElement(Box, { className: "twa:ml-1 twa:text-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, null, sharedValue),
        activeInfo && React.createElement(Tag, null, activeInfo)));
};
export 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, null,
        React.createElement(Box, { className: "twa:ml-1 twa:text-warn" }, staleDepsString)));
};
