"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);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
var VisualEditorController_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.VisualEditorController = void 0;
const common_1 = require("@nestjs/common");
const swagger_1 = require("@nestjs/swagger");
const sessions_service_1 = require("../sessions/sessions.service");
const session_inputs_service_1 = require("../session-inputs/session-inputs.service");
const application_state_service_1 = require("../application-state/application-state.service");
const visual_editor_dto_1 = require("./visual-editor.dto");
let VisualEditorController = VisualEditorController_1 = class VisualEditorController {
    constructor(sessionsService, sessionInputsService, applicationStateService) {
        this.sessionsService = sessionsService;
        this.sessionInputsService = sessionInputsService;
        this.applicationStateService = applicationStateService;
        this.logger = new common_1.Logger(VisualEditorController_1.name);
    }
    async submitVisualPrompt(submitDto) {
        this.logger.log('Received prompt from visual editor');
        let sessionId = await this.applicationStateService.getActiveSessionId();
        if (!sessionId) {
            this.logger.log('No active session found. Creating a new one.');
            const newSession = await this.sessionsService.create({
                session_title: 'New Visual Editor Session',
            });
            sessionId = newSession.id;
            this.logger.log(`Created new session with ID: ${sessionId}`);
        }
        else {
            this.logger.log(`Using active session ID: ${sessionId}`);
        }
        const adHocFiles = [];
        if (submitDto.componentPath) {
            adHocFiles.push(submitDto.componentPath);
        }
        if (submitDto.parentComponentPath) {
            adHocFiles.push(submitDto.parentComponentPath);
        }
        const ad_hoc_context_definition = JSON.stringify({
            files: adHocFiles,
            folders: [],
            command_outputs: [],
        });
        const sessionDetails = await this.sessionsService.findOne(sessionId);
        const existingInputsCount = sessionDetails.sessionInputs?.length || 0;
        const isFirstPrompt = existingInputsCount === 0;
        const templateId = isFirstPrompt
            ? sessionDetails.default_initial_context_template_id
            : sessionDetails.default_followup_context_template_id;
        await this.sessionInputsService.create(sessionId, {
            user_prompt: submitDto.prompt,
            execution_strategy: 'apply_revert',
            context_template_id: templateId || null,
            ad_hoc_context_definition: ad_hoc_context_definition,
        });
        this.logger.log(`Successfully created new session input in session ${sessionId}`);
        return { message: 'Prompt submitted successfully' };
    }
};
exports.VisualEditorController = VisualEditorController;
__decorate([
    (0, common_1.Post)('submit'),
    (0, common_1.HttpCode)(common_1.HttpStatus.OK),
    (0, swagger_1.ApiOperation)({
        summary: 'Submit visual editor prompt',
        description: 'Submit a prompt from the visual editor with selected component context. Creates a session input with the component files included as context.',
    }),
    (0, swagger_1.ApiResponse)({
        status: 200,
        description: 'Prompt submitted successfully',
        schema: {
            properties: {
                message: { type: 'string', example: 'Prompt submitted successfully' },
            },
        },
    }),
    __param(0, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [visual_editor_dto_1.SubmitVisualPromptDto]),
    __metadata("design:returntype", Promise)
], VisualEditorController.prototype, "submitVisualPrompt", null);
exports.VisualEditorController = VisualEditorController = VisualEditorController_1 = __decorate([
    (0, swagger_1.ApiTags)('visual-editor'),
    (0, common_1.Controller)('visual-editor'),
    __metadata("design:paramtypes", [sessions_service_1.SessionsService,
        session_inputs_service_1.SessionInputsService,
        application_state_service_1.ApplicationStateService])
], VisualEditorController);
//# sourceMappingURL=visual-editor.controller.js.map