import { mergeVersion } from "../version/merge-version.js";
export async function acceptChangeProposal(args) {
    const { lix, proposal: proposalRef } = args;
    const id = proposalRef.id;
    // 1) Load the proposal
    const proposal = await lix.db
        .selectFrom("change_proposal")
        .where("id", "=", id)
        .select(["id", "source_version_id", "target_version_id", "status"])
        .executeTakeFirstOrThrow();
    // 2) Merge in its own transaction (mergeVersion manages transaction scope internally)
    await mergeVersion({
        lix,
        source: { id: proposal.source_version_id },
        target: { id: proposal.target_version_id },
    });
    // 3) Mark accepted, delete the proposal row (releasing FKs), then delete source version.
    await lix.db.transaction().execute(async (trx) => {
        await trx
            .updateTable("change_proposal_by_version")
            .set({ status: "accepted" })
            .where("id", "=", id)
            .where("lixcol_version_id", "=", "global")
            .execute();
        await trx
            .deleteFrom("change_proposal_by_version")
            .where("id", "=", id)
            .where("lixcol_version_id", "=", "global")
            .execute();
        await trx
            .deleteFrom("version")
            .where("id", "=", proposal.source_version_id)
            .execute();
    });
}
//# sourceMappingURL=accept-change-proposal.js.map