// Copyright Metatype OÜ, licensed under the Mozilla Public License Version 2.0.
// SPDX-License-Identifier: MPL-2.0
import * as t from "../types.js";
import { runtimes } from "../wit.js";
import Policy from "../policy.js";
import { Runtime } from "./mod.js";
import { fx } from "../index.js";
export class DenoRuntime extends Runtime {
    constructor() {
        super(runtimes.getDenoRuntime());
    }
    func(inp, out, { code, secrets = [], effect = fx.read() }) {
        const matId = runtimes.registerDenoFunc({ code, secrets }, effect);
        const mat = {
            _id: matId,
            code,
            secrets,
            effect,
        };
        return t.func(inp, out, mat);
    }
    import(inp, out, { name, module, effect = fx.read(), secrets = [] }) {
        const matId = runtimes.importDenoFunction({
            funcName: name,
            module,
            secrets,
        }, effect);
        const mat = {
            _id: matId,
            name,
            module,
            secrets,
            effect,
        };
        return t.func(inp, out, mat);
    }
    identity(inp) {
        const mat = {
            _id: runtimes.getPredefinedDenoFunc({ name: "identity" }),
            name: "identity",
        };
        return t.func(inp, inp, mat);
    }
    static(out, value) {
        const mat = {
            _id: runtimes.registerDenoStatic({
                value: JSON.stringify(value),
            }, out._id),
        };
        return t.func(t.struct({}), out, mat);
    }
    policy(name, data) {
        const params = typeof data === "string"
            ? { code: data, secrets: [] }
            : { secrets: [], ...data };
        return Policy.create(name, runtimes.registerDenoFunc(params, fx.read()));
    }
    importPolicy(data, name) {
        var _a;
        const policyName = name !== null && name !== void 0 ? name : `__imp_${data.module}_${data.name}`.replace(/[^a-zA-Z0-9_]/g, "_");
        return Policy.create(policyName, runtimes.importDenoFunction({
            funcName: data.name,
            module: data.module,
            secrets: (_a = data.secrets) !== null && _a !== void 0 ? _a : [],
        }, fx.read()));
    }
}
