// Copyright Metatype OÜ, licensed under the Mozilla Public License Version 2.0.
// SPDX-License-Identifier: MPL-2.0
import "../_dnt.polyfills.js";
import * as t from "../types.js";
import { runtimes } from "../wit.js";
import { Runtime } from "./mod.js";
import { fx } from "../index.js";
export class PythonRuntime extends Runtime {
    constructor() {
        super(runtimes.registerPythonRuntime());
    }
    /** create a function from a lambda */
    fromLambda(inp, out, { code }) {
        const matId = runtimes.fromPythonLambda({
            runtime: this._id,
            effect: fx.read(),
        }, {
            fn: code,
            runtime: this._id,
        });
        return t.func(inp, out, {
            _id: matId,
            fn: code,
        });
    }
    /** create a function from a Python `def` function */
    fromDef(inp, out, { code }) {
        const name = code.trim().match(/def\s+([A-Za-z0-9_]+)/)?.[1];
        if (name == undefined) {
            throw new Error(`unable to extract def name from source code ${code}`);
        }
        const matId = runtimes.fromPythonDef({
            runtime: this._id,
            effect: fx.read(),
        }, {
            name: name,
            fn: code,
            runtime: this._id,
        });
        return t.func(inp, out, {
            _id: matId,
            name,
            fn: code,
        });
    }
    import(inp, out, { name, module, deps = [], effect = fx.read(), secrets = [] }) {
        const base = {
            runtime: this._id,
            effect,
        };
        const matId = runtimes.fromPythonModule(base, {
            file: module,
            runtime: this._id,
            deps: deps ?? [],
        });
        const pyModMatId = runtimes.fromPythonImport(base, {
            module: matId,
            funcName: name,
            secrets,
        });
        return t.func(inp, out, {
            _id: pyModMatId,
            module,
            name,
        });
    }
}
