import { getAppDisplayName } from '../ee/apps/types';
import assertUnreachable from '../utils/assertUnreachable';
import { ContentType as ResourceViewItemType, } from './content';
export { ResourceViewItemType };
export var ResourceItemCategory;
(function (ResourceItemCategory) {
    ResourceItemCategory["MOST_POPULAR"] = "mostPopular";
    ResourceItemCategory["RECENTLY_UPDATED"] = "recentlyUpdated";
    ResourceItemCategory["PINNED"] = "pinned";
    ResourceItemCategory["FAVORITES"] = "favorites";
    ResourceItemCategory["VERIFIED"] = "verified";
})(ResourceItemCategory || (ResourceItemCategory = {}));
export const isResourceViewItemChart = (item) => item.type === ResourceViewItemType.CHART;
export const isResourceViewItemDashboard = (item) => item.type === ResourceViewItemType.DASHBOARD;
export const isResourceViewSpaceItem = (item) => item.type === ResourceViewItemType.SPACE;
export const isResourceViewDataAppItem = (item) => item.type === ResourceViewItemType.DATA_APP;
/**
 * Display name for any resource view item. Identical to `item.data.name` for
 * every type except data apps, which fall back to a placeholder when unnamed
 * (see `getAppDisplayName`). Use this anywhere a resource item's name is
 * rendered so unnamed apps never show up blank.
 */
export const getResourceViewItemName = (item) => isResourceViewDataAppItem(item)
    ? getAppDisplayName(item.data.name, item.data.uuid)
    : item.data.name;
export const wrapResource = (resource, type) => {
    switch (type) {
        case ResourceViewItemType.CHART:
            return { type, data: resource };
        case ResourceViewItemType.DASHBOARD:
            return { type, data: resource };
        case ResourceViewItemType.SPACE:
            return { type, data: resource };
        case ResourceViewItemType.DATA_APP:
            return {
                type,
                data: resource,
            };
        default:
            return assertUnreachable(type, `Unknown resource type: ${type}`);
    }
};
export const wrapResourceView = (resources, type) => resources.map((resource) => wrapResource(resource, type));
export const spaceToResourceViewItem = (space) => ({
    organizationUuid: space.organizationUuid,
    projectUuid: space.projectUuid,
    uuid: space.uuid,
    name: space.name,
    inheritParentPermissions: space.inheritParentPermissions,
    pinnedListUuid: space.pinnedListUuid,
    pinnedListOrder: space.pinnedListOrder,
    accessListLength: space.access.length,
    dashboardCount: space.dashboardCount,
    chartCount: space.chartCount,
    childSpaceCount: space.childSpaceCount,
    appCount: space.appCount,
    access: space.access,
    parentSpaceUuid: space.parentSpaceUuid,
    path: space.path,
});
export const contentToResourceViewItem = (content) => {
    const updatedByUser = content.lastUpdatedBy || content.createdBy || undefined;
    switch (content.contentType) {
        case ResourceViewItemType.CHART:
            const chartViewItem = {
                ...content,
                description: content.description || undefined,
                spaceUuid: content.space.uuid,
                pinnedListUuid: content.pinnedList?.uuid || null,
                pinnedListOrder: null,
                updatedAt: content.lastUpdatedAt || content.createdAt,
                updatedByUser: updatedByUser && {
                    ...updatedByUser,
                    userUuid: updatedByUser.uuid,
                },
                verification: content.verification,
                projectUuid: content.project.uuid, // Required for permission checks in ResourceActionMenu
                organizationUuid: content.organization.uuid,
            };
            return wrapResource(chartViewItem, ResourceViewItemType.CHART);
        case ResourceViewItemType.DASHBOARD:
            const dashboardViewItem = {
                ...content,
                description: content.description || undefined,
                spaceUuid: content.space.uuid,
                pinnedListUuid: content.pinnedList?.uuid || null,
                pinnedListOrder: null,
                updatedAt: content.lastUpdatedAt || content.createdAt,
                updatedByUser: updatedByUser && {
                    ...updatedByUser,
                    userUuid: updatedByUser.uuid,
                },
                verification: content.verification,
                projectUuid: content.project.uuid,
                organizationUuid: content.organization.uuid,
            };
            return wrapResource(dashboardViewItem, ResourceViewItemType.DASHBOARD);
        case ResourceViewItemType.SPACE:
            return wrapResource(spaceToResourceViewItem({
                ...content,
                organizationUuid: content.organization.uuid,
                projectUuid: content.project.uuid,
                pinnedListUuid: content.pinnedList?.uuid || null,
                pinnedListOrder: content.pinnedList?.order || null,
                userAccess: undefined, // This property is not needed for the resource view item
                projectMemberAccessRole: null,
                parentSpaceUuid: content.parentSpaceUuid,
                path: content.path,
            }), ResourceViewItemType.SPACE);
        case ResourceViewItemType.DATA_APP:
            const dataAppViewItem = {
                uuid: content.uuid,
                name: content.name,
                description: content.description || undefined,
                spaceUuid: content.space?.uuid ?? null,
                createdByUserUuid: content.createdBy?.uuid ?? null,
                updatedAt: content.lastUpdatedAt || content.createdAt,
                updatedByUser: updatedByUser
                    ? {
                        userUuid: updatedByUser.uuid,
                        firstName: updatedByUser.firstName,
                        lastName: updatedByUser.lastName,
                    }
                    : null,
                views: content.views,
                firstViewedAt: content.firstViewedAt,
                latestVersionNumber: content.latestVersionNumber,
                latestVersionStatus: content.latestVersionStatus,
                pinnedListUuid: content.pinnedList?.uuid || null,
                pinnedListOrder: content.pinnedList?.order || null,
            };
            return wrapResource(dataAppViewItem, ResourceViewItemType.DATA_APP);
        default:
            return assertUnreachable(content, `Unsupported content type`);
    }
};
export const resourceToContent = (resource) => {
    switch (resource.type) {
        case ResourceViewItemType.CHART:
            return resource.data;
        case ResourceViewItemType.DASHBOARD:
            return resource.data;
        case ResourceViewItemType.SPACE:
            return resource.data;
        case ResourceViewItemType.DATA_APP:
            return resource.data;
        default:
            return assertUnreachable(resource, `Unsupported resource type`);
    }
};
//# sourceMappingURL=resourceViewItem.js.map