/**
 * SQLite WorkItemRelation Repository Implementation
 *
 * Implements IWorkItemRelationRepository using better-sqlite3.
 */
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);
};
import { injectable } from 'tsyringe';
import { toDatabase, fromDatabase, } from '../persistence/sqlite/mappers/work-item-relation.mapper.js';
let SQLiteWorkItemRelationRepository = class SQLiteWorkItemRelationRepository {
    db;
    constructor(db) {
        this.db = db;
    }
    async create(relation) {
        const row = toDatabase(relation);
        const stmt = this.db.prepare(`
      INSERT INTO work_item_relations (
        id, source_work_item_id, target_work_item_id, relation_type, created_at
      ) VALUES (
        @id, @source_work_item_id, @target_work_item_id, @relation_type, @created_at
      )
    `);
        stmt.run(row);
    }
    async delete(id) {
        const stmt = this.db.prepare('DELETE FROM work_item_relations WHERE id = ?');
        stmt.run(id);
    }
    async listByWorkItem(workItemId) {
        const stmt = this.db.prepare('SELECT * FROM work_item_relations WHERE source_work_item_id = ? OR target_work_item_id = ? ORDER BY created_at ASC');
        const rows = stmt.all(workItemId, workItemId);
        return rows.map(fromDatabase);
    }
    async findExisting(sourceId, targetId, relationType) {
        const stmt = this.db.prepare('SELECT * FROM work_item_relations WHERE source_work_item_id = ? AND target_work_item_id = ? AND relation_type = ?');
        const row = stmt.get(sourceId, targetId, relationType);
        return row ? fromDatabase(row) : null;
    }
};
SQLiteWorkItemRelationRepository = __decorate([
    injectable(),
    __metadata("design:paramtypes", [Object])
], SQLiteWorkItemRelationRepository);
export { SQLiteWorkItemRelationRepository };
