"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PkgFsBindings = void 0;
const node_1 = require("@zwave-js/core/bindings/fs/node");
const node_path_1 = __importDefault(require("node:path"));
// Ensures that the Z-Wave JS driver is looking for the right files in the right place
// when running inside a `pkg` bundle. In this case, it will resolve its embedded
// configuration dir to the path "/config", but the files reside in "node_modules/@zwave-js/config/config" instead.
const CONFIG_PATH = node_path_1.default.resolve('/config');
const CONFIG_PATH_IN_PKG = node_path_1.default.join(__dirname, `node_modules/@zwave-js/config/config`);
class PkgFsBindings {
    readFile(filePath) {
        filePath = node_path_1.default.normalize(filePath);
        if (filePath.startsWith(CONFIG_PATH)) {
            filePath = filePath.replace(CONFIG_PATH, CONFIG_PATH_IN_PKG);
        }
        return node_1.fs.readFile(filePath);
    }
    writeFile(filePath, data) {
        filePath = node_path_1.default.normalize(filePath);
        if (filePath.startsWith(CONFIG_PATH)) {
            // The pkg assets are readonly
            return;
        }
        return node_1.fs.writeFile(filePath, data);
    }
    copyFile(source, dest) {
        source = node_path_1.default.normalize(source);
        dest = node_path_1.default.normalize(dest);
        if (dest.startsWith(CONFIG_PATH)) {
            // The pkg assets are readonly
            return;
        }
        if (source.startsWith(CONFIG_PATH)) {
            source = source.replace(CONFIG_PATH, CONFIG_PATH_IN_PKG);
        }
        return node_1.fs.copyFile(source, dest);
    }
    open(filePath, flags) {
        filePath = node_path_1.default.normalize(filePath);
        if (filePath.startsWith(CONFIG_PATH) && flags.write) {
            // The pkg assets are readonly
            throw new Error(`${filePath} is not writable`);
        }
        if (filePath.startsWith(CONFIG_PATH)) {
            filePath = filePath.replace(CONFIG_PATH, CONFIG_PATH_IN_PKG);
        }
        return node_1.fs.open(filePath, flags);
    }
    readDir(dirPath) {
        dirPath = node_path_1.default.normalize(dirPath);
        if (dirPath.startsWith(CONFIG_PATH)) {
            dirPath = dirPath.replace(CONFIG_PATH, CONFIG_PATH_IN_PKG);
        }
        return node_1.fs.readDir(dirPath);
    }
    stat(filePath) {
        filePath = node_path_1.default.normalize(filePath);
        if (filePath.startsWith(CONFIG_PATH)) {
            filePath = filePath.replace(CONFIG_PATH, CONFIG_PATH_IN_PKG);
        }
        return node_1.fs.stat(filePath);
    }
    ensureDir(dirPath) {
        dirPath = node_path_1.default.normalize(dirPath);
        if (dirPath.startsWith(CONFIG_PATH)) {
            // The pkg assets are readonly
            return;
        }
        return node_1.fs.ensureDir(dirPath);
    }
    deleteDir(dirPath) {
        dirPath = node_path_1.default.normalize(dirPath);
        if (dirPath.startsWith(CONFIG_PATH)) {
            // The pkg assets are readonly
            return;
        }
        return node_1.fs.deleteDir(dirPath);
    }
    makeTempDir(prefix) {
        return node_1.fs.makeTempDir(prefix);
    }
}
exports.PkgFsBindings = PkgFsBindings;
