// Copyright Metatype OÜ, licensed under the Mozilla Public License Version 2.0.
// SPDX-License-Identifier: MPL-2.0
import { Runtime } from "../runtimes/mod.js";
import { runtimes } from "../wit.js";
import { Typedef } from "../types.js";
import { t } from "../index.js";
import { genRef } from "./../typegraph.js";
export class PrismaRuntime extends Runtime {
    constructor(name, connectionStringSecret) {
        const id = runtimes.registerPrismaRuntime({
            name,
            connectionStringSecret,
        });
        super(id);
        this.name = name;
        this.connectionStringSecret = connectionStringSecret;
    }
    findUnique(model) {
        if (typeof model == "string") {
            model = genRef(model);
        }
        const type = runtimes.prismaFindUnique(this._id, model._id);
        return t.Func.fromTypeFunc(type);
    }
    findMany(model) {
        if (typeof model == "string") {
            model = genRef(model);
        }
        const type = runtimes.prismaFindMany(this._id, model._id);
        return t.Func.fromTypeFunc(type);
    }
    findFirst(model) {
        if (typeof model == "string") {
            model = genRef(model);
        }
        const type = runtimes.prismaFindFirst(this._id, model._id);
        return t.Func.fromTypeFunc(type);
    }
    aggregate(model) {
        if (typeof model == "string") {
            model = genRef(model);
        }
        const type = runtimes.prismaAggregate(this._id, model._id);
        return t.Func.fromTypeFunc(type);
    }
    count(model) {
        if (typeof model == "string") {
            model = genRef(model);
        }
        const type = runtimes.prismaCount(this._id, model._id);
        return t.Func.fromTypeFunc(type);
    }
    groupBy(model) {
        if (typeof model == "string") {
            model = genRef(model);
        }
        const type = runtimes.prismaGroupBy(this._id, model._id);
        return t.Func.fromTypeFunc(type);
    }
    create(model) {
        if (typeof model == "string") {
            model = genRef(model);
        }
        const type = runtimes.prismaCreateOne(this._id, model._id);
        return t.Func.fromTypeFunc(type);
    }
    createMany(model) {
        if (typeof model == "string") {
            model = genRef(model);
        }
        const type = runtimes.prismaCreateMany(this._id, model._id);
        return t.Func.fromTypeFunc(type);
    }
    update(model) {
        if (typeof model == "string") {
            model = genRef(model);
        }
        const type = runtimes.prismaUpdateOne(this._id, model._id);
        return t.Func.fromTypeFunc(type);
    }
    updateMany(model) {
        if (typeof model == "string") {
            model = genRef(model);
        }
        const type = runtimes.prismaUpdateMany(this._id, model._id);
        return t.Func.fromTypeFunc(type);
    }
    upsert(model) {
        if (typeof model == "string") {
            model = genRef(model);
        }
        const type = runtimes.prismaUpsertOne(this._id, model._id);
        return t.Func.fromTypeFunc(type);
    }
    delete(model) {
        if (typeof model == "string") {
            model = genRef(model);
        }
        const type = runtimes.prismaDeleteOne(this._id, model._id);
        return t.Func.fromTypeFunc(type);
    }
    deleteMany(model) {
        if (typeof model == "string") {
            model = genRef(model);
        }
        const type = runtimes.prismaDeleteMany(this._id, model._id);
        return t.Func.fromTypeFunc(type);
    }
    execute(query, parameters, effect) {
        const type = runtimes.prismaExecute(this._id, query, parameters._id, effect);
        return t.Func.fromTypeFunc(type);
    }
    queryRaw(query, parameters, output) {
        const type = runtimes.prismaQueryRaw(this._id, query, parameters ? parameters._id : undefined, output._id);
        return t.Func.fromTypeFunc(type);
    }
    link(targetType, name, arg) {
        return prismaLink(targetType, name, arg !== null && arg !== void 0 ? arg : {});
    }
}
function prismaLink(targetType, name, arg) {
    if (typeof targetType == "string") {
        targetType = genRef(targetType);
    }
    arg = arg !== null && arg !== void 0 ? arg : {};
    const typeId = runtimes.prismaLink({
        targetType: targetType._id,
        relationshipName: name,
        foreignKey: arg.fkey,
        targetField: arg.field,
        unique: arg.unique,
    });
    return new Typedef(typeId, {});
}
