import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
/**
 * Copyright (c) 2020-present, Goldman Sachs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { observer } from 'mobx-react-lite';
import { FolderIcon, HistoryIcon, TimesIcon, UserIcon, UsersIcon, } from '@finos/legend-art';
import { WorkspaceType } from '@finos/legend-server-sdlc';
import { generateEditorRoute } from '../../__lib__/LegendStudioNavigation.js';
const MAX_TILES = 6;
const formatRelativeTime = (timestamp) => {
    const diffMs = Date.now() - timestamp;
    if (diffMs < 0) {
        return 'just now';
    }
    const minutes = Math.floor(diffMs / 60_000);
    if (minutes < 1) {
        return 'just now';
    }
    if (minutes < 60) {
        return `${minutes}m ago`;
    }
    const hours = Math.floor(minutes / 60);
    if (hours < 24) {
        return `${hours}h ago`;
    }
    const days = Math.floor(hours / 24);
    if (days < 7) {
        return `${days}d ago`;
    }
    const weeks = Math.floor(days / 7);
    if (weeks < 4) {
        return `${weeks}w ago`;
    }
    const months = Math.floor(days / 30);
    if (months < 12) {
        return `${months}mo ago`;
    }
    return `${Math.floor(days / 365)}y ago`;
};
export const RecentWorkspacesPanel = observer((props) => {
    const { setupStore } = props;
    const applicationStore = setupStore.applicationStore;
    // Recents are stored in LRU order (most-recent first). Show the top N
    // workspaces; map each to its project name (or fall back to projectId
    // if the matching project entry was evicted independently).
    const tiles = setupStore.recentWorkspaces.slice(0, MAX_TILES);
    if (tiles.length === 0) {
        return null;
    }
    const projectNameById = new Map(setupStore.recentProjects.map((p) => [p.projectId, p.name]));
    const openWorkspace = (projectId, workspaceId, workspaceType) => {
        applicationStore.navigationService.navigator.goToLocation(generateEditorRoute(projectId, undefined, workspaceId, workspaceType));
    };
    return (_jsxs("div", { className: "workspace-setup__recents", children: [_jsx("div", { className: "workspace-setup__recents__header", children: _jsxs("div", { className: "workspace-setup__recents__header__title", children: [_jsx(HistoryIcon, {}), _jsx("span", { children: "Recent workspaces" })] }) }), _jsx("div", { className: "workspace-setup__recents__grid", children: tiles.map((entry) => {
                    const projectName = projectNameById.get(entry.projectId) ?? entry.projectId;
                    const key = `${entry.projectId}::${entry.workspaceType}::${entry.workspaceId}`;
                    const handleRemove = (event) => {
                        event.stopPropagation();
                        setupStore.removeRecentWorkspace({
                            projectId: entry.projectId,
                            workspaceId: entry.workspaceId,
                            workspaceType: entry.workspaceType,
                        });
                    };
                    return (_jsxs("button", { type: "button", className: "workspace-setup__recents__tile", title: `Open ${projectName} / ${entry.workspaceId}`, onClick: () => openWorkspace(entry.projectId, entry.workspaceId, entry.workspaceType), children: [_jsx("button", { type: "button", tabIndex: -1, className: "workspace-setup__recents__tile__remove", title: "Remove from recents", onClick: handleRemove, children: _jsx(TimesIcon, {}) }), _jsxs("div", { className: "workspace-setup__recents__tile__project", children: [_jsx(FolderIcon, {}), _jsx("span", { className: "workspace-setup__recents__tile__project__name", children: projectName })] }), _jsxs("div", { className: "workspace-setup__recents__tile__workspace", children: [entry.workspaceType === WorkspaceType.GROUP ? (_jsx(UsersIcon, {})) : (_jsx(UserIcon, {})), _jsx("span", { className: "workspace-setup__recents__tile__workspace__name", children: entry.workspaceId })] }), _jsx("div", { className: "workspace-setup__recents__tile__time", children: formatRelativeTime(entry.lastOpenedAt) })] }, key));
                }) })] }));
});
//# sourceMappingURL=RecentWorkspacesPanel.js.map