"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.FridayAppReplyView = void 0;
const typeorm_1 = require("typeorm");
let FridayAppReplyView = class FridayAppReplyView extends typeorm_1.BaseEntity {
};
exports.FridayAppReplyView = FridayAppReplyView;
__decorate([
    (0, typeorm_1.ViewColumn)(),
    __metadata("design:type", String)
], FridayAppReplyView.prototype, "id", void 0);
__decorate([
    (0, typeorm_1.ViewColumn)(),
    __metadata("design:type", String)
], FridayAppReplyView.prototype, "name", void 0);
__decorate([
    (0, typeorm_1.ViewColumn)(),
    __metadata("design:type", String)
], FridayAppReplyView.prototype, "role", void 0);
__decorate([
    (0, typeorm_1.ViewColumn)({
        transformer: {
            from: (value) => {
                // Convert JSON string to ContentBlocks
                return JSON.parse(value).reduce((acc, block) => {
                    acc.push(...block);
                    return acc;
                }, []);
            },
            to: (value) => {
                // Convert ContentBlocks to JSON string
                return JSON.stringify(value);
            },
        },
    }),
    __metadata("design:type", Array)
], FridayAppReplyView.prototype, "content", void 0);
__decorate([
    (0, typeorm_1.Column)(),
    __metadata("design:type", String)
], FridayAppReplyView.prototype, "startTimeStamp", void 0);
__decorate([
    (0, typeorm_1.Column)({ nullable: true }),
    __metadata("design:type", String)
], FridayAppReplyView.prototype, "endTimeStamp", void 0);
__decorate([
    (0, typeorm_1.Column)(),
    __metadata("design:type", Boolean)
], FridayAppReplyView.prototype, "finished", void 0);
exports.FridayAppReplyView = FridayAppReplyView = __decorate([
    (0, typeorm_1.ViewEntity)({
        expression: (dataSource) => {
            const dialect = dataSource.options.type;
            const jsonArrayAgg = (() => {
                switch (dialect) {
                    case 'sqlite':
                        // SQLite
                        return `(
                SELECT json_group_array(json(content))
                FROM (
                    SELECT content 
                    FROM friday_app_message_table
                    WHERE reply_id = r.id
                    ORDER BY timestamp ASC
                )
            )`;
                    default:
                        throw new Error(`Unsupported database type: ${dialect}`);
                }
            })();
            return dataSource
                .createQueryBuilder()
                .select('r.id', 'id')
                .addSelect('m.name', 'name')
                .addSelect('m.role', 'role')
                .addSelect(`${jsonArrayAgg}`, 'content')
                .addSelect('r.startTimeStamp', 'startTimeStamp')
                .addSelect('r.endTimeStamp', 'endTimeStamp')
                .addSelect('r.finished', 'finished')
                .from('friday_app_reply_table', 'r')
                .leftJoin('friday_app_message_table', 'm', 'm.reply_id = r.id')
                .groupBy('r.id');
        },
    })
], FridayAppReplyView);
