"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resourceToContent = exports.contentToResourceViewItem = exports.spaceToResourceViewItem = exports.wrapResourceView = exports.wrapResource = exports.isResourceViewDataAppItem = exports.isResourceViewSpaceItem = exports.isResourceViewItemDashboard = exports.isResourceViewItemChart = exports.ResourceItemCategory = exports.ResourceViewItemType = void 0;
const tslib_1 = require("tslib");
const assertUnreachable_1 = tslib_1.__importDefault(require("../utils/assertUnreachable"));
const content_1 = require("./content");
Object.defineProperty(exports, "ResourceViewItemType", { enumerable: true, get: function () { return content_1.ContentType; } });
var ResourceItemCategory;
(function (ResourceItemCategory) {
    ResourceItemCategory["MOST_POPULAR"] = "mostPopular";
    ResourceItemCategory["RECENTLY_UPDATED"] = "recentlyUpdated";
    ResourceItemCategory["PINNED"] = "pinned";
    ResourceItemCategory["FAVORITES"] = "favorites";
    ResourceItemCategory["VERIFIED"] = "verified";
})(ResourceItemCategory || (exports.ResourceItemCategory = ResourceItemCategory = {}));
const isResourceViewItemChart = (item) => item.type === content_1.ContentType.CHART;
exports.isResourceViewItemChart = isResourceViewItemChart;
const isResourceViewItemDashboard = (item) => item.type === content_1.ContentType.DASHBOARD;
exports.isResourceViewItemDashboard = isResourceViewItemDashboard;
const isResourceViewSpaceItem = (item) => item.type === content_1.ContentType.SPACE;
exports.isResourceViewSpaceItem = isResourceViewSpaceItem;
const isResourceViewDataAppItem = (item) => item.type === content_1.ContentType.DATA_APP;
exports.isResourceViewDataAppItem = isResourceViewDataAppItem;
const wrapResource = (resource, type) => {
    switch (type) {
        case content_1.ContentType.CHART:
            return { type, data: resource };
        case content_1.ContentType.DASHBOARD:
            return { type, data: resource };
        case content_1.ContentType.SPACE:
            return { type, data: resource };
        case content_1.ContentType.DATA_APP:
            return {
                type,
                data: resource,
            };
        default:
            return (0, assertUnreachable_1.default)(type, `Unknown resource type: ${type}`);
    }
};
exports.wrapResource = wrapResource;
const wrapResourceView = (resources, type) => resources.map((resource) => (0, exports.wrapResource)(resource, type));
exports.wrapResourceView = wrapResourceView;
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,
});
exports.spaceToResourceViewItem = spaceToResourceViewItem;
const contentToResourceViewItem = (content) => {
    const updatedByUser = content.lastUpdatedBy || content.createdBy || undefined;
    switch (content.contentType) {
        case content_1.ContentType.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 (0, exports.wrapResource)(chartViewItem, content_1.ContentType.CHART);
        case content_1.ContentType.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 (0, exports.wrapResource)(dashboardViewItem, content_1.ContentType.DASHBOARD);
        case content_1.ContentType.SPACE:
            return (0, exports.wrapResource)((0, exports.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,
            }), content_1.ContentType.SPACE);
        case content_1.ContentType.DATA_APP:
            const dataAppViewItem = {
                uuid: content.uuid,
                name: content.name,
                description: content.description || undefined,
                spaceUuid: content.space.uuid,
                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 (0, exports.wrapResource)(dataAppViewItem, content_1.ContentType.DATA_APP);
        default:
            return (0, assertUnreachable_1.default)(content, `Unsupported content type`);
    }
};
exports.contentToResourceViewItem = contentToResourceViewItem;
const resourceToContent = (resource) => {
    switch (resource.type) {
        case content_1.ContentType.CHART:
            return resource.data;
        case content_1.ContentType.DASHBOARD:
            return resource.data;
        case content_1.ContentType.SPACE:
            return resource.data;
        case content_1.ContentType.DATA_APP:
            return resource.data;
        default:
            return (0, assertUnreachable_1.default)(resource, `Unsupported resource type`);
    }
};
exports.resourceToContent = resourceToContent;
//# sourceMappingURL=resourceViewItem.js.map