"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.installFs = installFs;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const evaluator_1 = require("../language/evaluator");
function asString(obj) {
    if (obj instanceof evaluator_1.StringObj) {
        return obj.value;
    }
    return null;
}
/**
 * Filesystem I/O builtins for Nexus.
 */
function installFs(env) {
    env.set("fs_read", new evaluator_1.BuiltinObj((...args) => {
        if (args.length !== 1) {
            return new evaluator_1.ErrorObj("fs_read: want 1 string path");
        }
        const p = asString(args[0]);
        if (p === null) {
            return new evaluator_1.ErrorObj("fs_read: path must be string");
        }
        try {
            return new evaluator_1.StringObj(fs.readFileSync(p, "utf8"));
        }
        catch (err) {
            return new evaluator_1.ErrorObj(`fs_read failed: ${err instanceof Error ? err.message : String(err)}`);
        }
    }));
    env.set("fs_write", new evaluator_1.BuiltinObj((...args) => {
        if (args.length !== 2) {
            return new evaluator_1.ErrorObj("fs_write: want path, contents");
        }
        const p = asString(args[0]);
        const contents = asString(args[1]);
        if (p === null || contents === null) {
            return new evaluator_1.ErrorObj("fs_write: args must be strings");
        }
        try {
            fs.mkdirSync(path.dirname(p), { recursive: true });
            fs.writeFileSync(p, contents, "utf8");
            return new evaluator_1.IntegerObj(contents.length);
        }
        catch (err) {
            return new evaluator_1.ErrorObj(`fs_write failed: ${err instanceof Error ? err.message : String(err)}`);
        }
    }));
    env.set("fs_exists", new evaluator_1.BuiltinObj((...args) => {
        if (args.length !== 1) {
            return new evaluator_1.ErrorObj("fs_exists: want 1 path");
        }
        const p = asString(args[0]);
        if (p === null) {
            return new evaluator_1.ErrorObj("fs_exists: path must be string");
        }
        return new evaluator_1.BooleanObj(fs.existsSync(p));
    }));
    env.set("fs_list", new evaluator_1.BuiltinObj((...args) => {
        if (args.length !== 1) {
            return new evaluator_1.ErrorObj("fs_list: want 1 directory path");
        }
        const p = asString(args[0]);
        if (p === null) {
            return new evaluator_1.ErrorObj("fs_list: path must be string");
        }
        try {
            return new evaluator_1.StringObj(fs.readdirSync(p).join("\n"));
        }
        catch (err) {
            return new evaluator_1.ErrorObj(`fs_list failed: ${err instanceof Error ? err.message : String(err)}`);
        }
    }));
}
//# sourceMappingURL=fs.js.map