// Copyright Metatype OÜ, licensed under the Mozilla Public License Version 2.0.
// SPDX-License-Identifier: MPL-2.0
import "../_dnt.polyfills.js";
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);
        Object.defineProperty(this, "name", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        Object.defineProperty(this, "connectionStringSecret", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        this.name = name;
        this.connectionStringSecret = connectionStringSecret;
    }
    /** create a function for a prisma `findUnique` query */
    findUnique(model) {
        if (typeof model == "string") {
            model = genRef(model);
        }
        const type = runtimes.prismaFindUnique(this._id, model._id);
        return t.Func.fromTypeFunc(type);
    }
    /** create a function for a prisma `findMany` query */
    findMany(model) {
        if (typeof model == "string") {
            model = genRef(model);
        }
        const type = runtimes.prismaFindMany(this._id, model._id);
        return t.Func.fromTypeFunc(type);
    }
    /** create a function for a prisma `findFirst` query */
    findFirst(model) {
        if (typeof model == "string") {
            model = genRef(model);
        }
        const type = runtimes.prismaFindFirst(this._id, model._id);
        return t.Func.fromTypeFunc(type);
    }
    /** create a function for a prisma `aggregate` query */
    aggregate(model) {
        if (typeof model == "string") {
            model = genRef(model);
        }
        const type = runtimes.prismaAggregate(this._id, model._id);
        return t.Func.fromTypeFunc(type);
    }
    /** create a function for a prisma `groupBy` query */
    groupBy(model) {
        if (typeof model == "string") {
            model = genRef(model);
        }
        const type = runtimes.prismaGroupBy(this._id, model._id);
        return t.Func.fromTypeFunc(type);
    }
    /** create a function for a prisma `create` query */
    create(model) {
        if (typeof model == "string") {
            model = genRef(model);
        }
        const type = runtimes.prismaCreateOne(this._id, model._id);
        return t.Func.fromTypeFunc(type);
    }
    /** create a function for a prisma `createMany` query */
    createMany(model) {
        if (typeof model == "string") {
            model = genRef(model);
        }
        const type = runtimes.prismaCreateMany(this._id, model._id);
        return t.Func.fromTypeFunc(type);
    }
    /** create a function for a prisma `update` query */
    update(model) {
        if (typeof model == "string") {
            model = genRef(model);
        }
        const type = runtimes.prismaUpdateOne(this._id, model._id);
        return t.Func.fromTypeFunc(type);
    }
    /** create a function for a prisma `updateMany` query */
    updateMany(model) {
        if (typeof model == "string") {
            model = genRef(model);
        }
        const type = runtimes.prismaUpdateMany(this._id, model._id);
        return t.Func.fromTypeFunc(type);
    }
    /** create a function for a prisma `upsert` query */
    upsert(model) {
        if (typeof model == "string") {
            model = genRef(model);
        }
        const type = runtimes.prismaUpsertOne(this._id, model._id);
        return t.Func.fromTypeFunc(type);
    }
    /** create a function for a prisma `delete` query */
    delete(model) {
        if (typeof model == "string") {
            model = genRef(model);
        }
        const type = runtimes.prismaDeleteOne(this._id, model._id);
        return t.Func.fromTypeFunc(type);
    }
    /** create a function for a prisma `deleteMany` query */
    deleteMany(model) {
        if (typeof model == "string") {
            model = genRef(model);
        }
        const type = runtimes.prismaDeleteMany(this._id, model._id);
        return t.Func.fromTypeFunc(type);
    }
    /** create a function for prisma `execute` */
    execute(query, parameters, effect) {
        const type = runtimes.prismaExecute(this._id, query, parameters._id, effect);
        return t.Func.fromTypeFunc(type);
    }
    /** create a function for prisma `queryRaw` */
    queryRaw(query, parameters, output) {
        const type = runtimes.prismaQueryRaw(this._id, query, parameters ? parameters._id : undefined, output._id);
        return t.Func.fromTypeFunc(type);
    }
    /** define a relationship */
    link(targetType, name, arg) {
        return prismaLink(targetType, name, arg ?? {});
    }
}
function prismaLink(targetType, name, arg) {
    if (typeof targetType == "string") {
        targetType = genRef(targetType);
    }
    arg = arg ?? {};
    const typeId = runtimes.prismaLink({
        targetType: targetType._id,
        relationshipName: name,
        foreignKey: arg.fkey,
        targetField: arg.field,
        unique: arg.unique,
    });
    return new Typedef(typeId);
}
