import { createVersionFromCommit } from "./create-version-from-commit.js";
/**
 * Creates a new version branching from a version's commit id (defaults to active when `from` is omitted).
 *
 * For branching from a specific commit id, use `createVersionFromCommit`.
 */
export async function createVersion(args) {
    const executeInTransaction = async (trx) => {
        // Resolve base commit from from/active
        let baseCommitId;
        if (args.from) {
            const base = await trx
                .selectFrom("version")
                .select(["commit_id"])
                .where("id", "=", args.from.id)
                .executeTakeFirstOrThrow();
            baseCommitId = base.commit_id;
        }
        else {
            const active = await trx
                .selectFrom("active_version")
                .innerJoin("version", "version.id", "active_version.version_id")
                .select("version.commit_id")
                .executeTakeFirstOrThrow();
            baseCommitId = active.commit_id;
        }
        return createVersionFromCommit({
            lix: { ...args.lix, db: trx },
            commit: { id: baseCommitId },
            id: args.id,
            name: args.name,
            inheritsFrom: args.inheritsFrom,
        });
    };
    if (args.lix.db.isTransaction) {
        return executeInTransaction(args.lix.db);
    }
    else {
        return args.lix.db.transaction().execute(executeInTransaction);
    }
}
//# sourceMappingURL=create-version.js.map