"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SolidCreateDashboardWithWidgetsMcpHandler = void 0;
const common_1 = require("@nestjs/common");
const class_transformer_1 = require("class-transformer");
const create_dashboard_dto_1 = require("../../../dtos/create-dashboard.dto");
const action_metadata_service_1 = require("../../action-metadata.service");
const dashboard_service_1 = require("../../dashboard.service");
const menu_item_metadata_service_1 = require("../../menu-item-metadata.service");
const model_metadata_service_1 = require("../../model-metadata.service");
const module_metadata_service_1 = require("../../module-metadata.service");
const role_metadata_service_1 = require("../../role-metadata.service");
let SolidCreateDashboardWithWidgetsMcpHandler = class SolidCreateDashboardWithWidgetsMcpHandler {
    constructor(dashboardService, actionMetadataService, menuItemMetadataService, moduleMetadataService, modelMetadataService, roleService) {
        this.dashboardService = dashboardService;
        this.actionMetadataService = actionMetadataService;
        this.menuItemMetadataService = menuItemMetadataService;
        this.moduleMetadataService = moduleMetadataService;
        this.modelMetadataService = modelMetadataService;
        this.roleService = roleService;
    }
    async apply(aiInteraction) {
        const escapedMessage = aiInteraction.message.replace(/\\'/g, "'");
        const aiResponseMessage = JSON.parse(escapedMessage);
        const { data } = aiResponseMessage;
        const { schema } = data;
        const { dashboardDto, dashboard } = await this.createDashboard(schema);
        const { moduleUserKey, actionMetadataEntity } = await this.createActionMetadataEntry(dashboardDto, dashboard);
        await this.createMenuItemEntry(dashboard, moduleUserKey, actionMetadataEntity);
        return {
            seedingRequired: false,
            serverRebooting: false,
        };
    }
    async createMenuItemEntry(dashboard, moduleUserKey, actionMetadataEntity) {
        const menuData = {
            displayName: dashboard.displayName || dashboard.name,
            name: `${moduleUserKey}-${dashboard.name}-dashboard-menu-item`,
            sequenceNumber: 1,
            actionUserKey: actionMetadataEntity.name,
            moduleUserKey: moduleUserKey,
            parentMenuItemUserKey: '',
        };
        const adminRole = await this.roleService.findRoleByName('Admin');
        const specifiedRoles = menuData['roles'];
        const specifiedRoleObjects = [adminRole];
        if (specifiedRoles) {
            for (let i = 0; i < specifiedRoles.length; i++) {
                const specifiedRole = specifiedRoles[i];
                const specifiedRoleObject = await this.roleService.findRoleByName(specifiedRole);
                if (!specifiedRoleObject) {
                    throw new Error(`Invalid role: (${specifiedRole}) specified against menu with display name ${menuData.displayName}.`);
                }
                specifiedRoleObjects.push(specifiedRoleObject);
            }
        }
        menuData['roles'] = specifiedRoleObjects;
        menuData['action'] = await this.actionMetadataService.findOneByUserKey(menuData.actionUserKey);
        menuData['module'] = await this.moduleMetadataService.findOneByUserKey(menuData.moduleUserKey);
        if (menuData.parentMenuItemUserKey) {
            menuData['parentMenuItem'] = await this.menuItemMetadataService.findOneByUserKey(menuData.parentMenuItemUserKey);
        }
        else {
            menuData['parentMenuItem'] = null;
        }
        await this.menuItemMetadataService.upsert(menuData);
    }
    async createActionMetadataEntry(dashboardDto, dashboard) {
        const moduleUserKey = dashboardDto.moduleUserKey;
        const actionMetadata = {
            name: `${dashboard.name}-view`,
            displayName: dashboard.displayName || dashboard.name,
            type: 'custom',
            domain: "{}",
            context: "{}",
            customComponent: `/admin/core/${moduleUserKey}/dashboards?dashboardName=${dashboard.name}`,
            customIsModal: true,
            serverEndpoint: '',
            moduleUserKey: moduleUserKey,
            modelUserKey: '',
            viewUserKey: `${dashboard.name}-view`,
        };
        actionMetadata['module'] = await this.moduleMetadataService.findOneByUserKey(actionMetadata.moduleUserKey);
        if (actionMetadata.modelUserKey) {
            actionMetadata['model'] = await this.modelMetadataService.findOneByUserKey(actionMetadata.modelUserKey);
        }
        const actionMetadataEntity = await this.actionMetadataService.upsert(actionMetadata);
        return { moduleUserKey, actionMetadataEntity };
    }
    async createDashboard(aiResponseMessage) {
        const dashboardDto = (0, class_transformer_1.plainToInstance)(create_dashboard_dto_1.CreateDashboardDto, aiResponseMessage);
        dashboardDto['layoutJson'] = JSON.stringify(dashboardDto['layoutJson']);
        const dashboard = await this.dashboardService.create(dashboardDto, []);
        return { dashboardDto, dashboard };
    }
};
exports.SolidCreateDashboardWithWidgetsMcpHandler = SolidCreateDashboardWithWidgetsMcpHandler;
exports.SolidCreateDashboardWithWidgetsMcpHandler = SolidCreateDashboardWithWidgetsMcpHandler = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [dashboard_service_1.DashboardService,
        action_metadata_service_1.ActionMetadataService,
        menu_item_metadata_service_1.MenuItemMetadataService,
        module_metadata_service_1.ModuleMetadataService,
        model_metadata_service_1.ModelMetadataService,
        role_metadata_service_1.RoleMetadataService])
], SolidCreateDashboardWithWidgetsMcpHandler);
//# sourceMappingURL=solid-create-dashboard-mcp-handler.service.js.map