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); }
};
import { injectable, inject } from 'tsyringe';
let GlobalSearchUseCase = class GlobalSearchUseCase {
    db;
    constructor(db) {
        this.db = db;
    }
    async execute(query, limit = 20) {
        const trimmed = query.trim();
        if (!trimmed)
            return [];
        // Escape FTS5 special characters and add prefix matching
        const ftsQuery = `${trimmed.replace(/['"*()]/g, '')}*`;
        const results = [];
        // Search projects
        const projectRows = this.db
            .prepare(`SELECT p.id, p.name, p.slug, p.description, p.identifier_prefix
         FROM pm_projects_fts fts
         JOIN pm_projects p ON p.id = fts.project_id
         WHERE pm_projects_fts MATCH ?
         AND p.deleted_at IS NULL
         LIMIT ?`)
            .all(ftsQuery, limit);
        for (const row of projectRows) {
            results.push({
                type: 'project',
                id: row.id,
                title: row.name,
                subtitle: row.identifier_prefix,
                url: `/projects/${row.slug}`,
            });
        }
        // Search work items
        const workItemRows = this.db
            .prepare(`SELECT wi.id, wi.title, wi.sequence_id, wi.identifier_prefix, p.slug AS project_slug
         FROM work_items_fts fts
         JOIN work_items wi ON wi.id = fts.work_item_id
         JOIN pm_projects p ON p.id = wi.project_id
         WHERE work_items_fts MATCH ?
         AND wi.deleted_at IS NULL
         AND p.deleted_at IS NULL
         LIMIT ?`)
            .all(ftsQuery, limit);
        for (const row of workItemRows) {
            results.push({
                type: 'workItem',
                id: row.id,
                title: row.title,
                subtitle: `${row.identifier_prefix}-${row.sequence_id}`,
                url: `/projects/${row.project_slug}`,
            });
        }
        // Search pages
        const pageRows = this.db
            .prepare(`SELECT pg.id, pg.title, p.slug AS project_slug, p.identifier_prefix
         FROM pages_fts fts
         JOIN pages pg ON pg.id = fts.page_id
         JOIN pm_projects p ON p.id = pg.project_id
         WHERE pages_fts MATCH ?
         AND pg.deleted_at IS NULL
         AND p.deleted_at IS NULL
         LIMIT ?`)
            .all(ftsQuery, limit);
        for (const row of pageRows) {
            results.push({
                type: 'page',
                id: row.id,
                title: row.title,
                subtitle: row.identifier_prefix,
                url: `/projects/${row.project_slug}/pages`,
            });
        }
        return results;
    }
};
GlobalSearchUseCase = __decorate([
    injectable(),
    __param(0, inject('Database')),
    __metadata("design:paramtypes", [Object])
], GlobalSearchUseCase);
export { GlobalSearchUseCase };
