
import { Record } from "./fable_modules/fable-library-js.5.0.0-rc.1/Types.js";
import { record_type, bool_type, option_type, string_type } from "./fable_modules/fable-library-js.5.0.0-rc.1/Reflection.js";
import * as fs from "fs";
import * as os from "os";
import * as process from "process";
import * as path_2 from "path";
import * as util from "../src/util.js";
import { trimEnd, substring, replace, trimStart } from "./fable_modules/fable-library-js.5.0.0-rc.1/String.js";
import { fromFloat64, toInt64_unchecked } from "./fable_modules/fable-library-js.5.0.0-rc.1/BigInt.js";
import { map, sort, item } from "./fable_modules/fable-library-js.5.0.0-rc.1/Array.js";
import { comparePrimitives } from "./fable_modules/fable-library-js.5.0.0-rc.1/Util.js";

export class CmdLineOptions extends Record {
    constructor(outDir, libDir, benchmark, optimize, sourceMaps, typedArrays, language, printAst) {
        super();
        this.outDir = outDir;
        this.libDir = libDir;
        this.benchmark = benchmark;
        this.optimize = optimize;
        this.sourceMaps = sourceMaps;
        this.typedArrays = typedArrays;
        this.language = language;
        this.printAst = printAst;
    }
}

export function CmdLineOptions_$reflection() {
    return record_type("Fable.Compiler.Platform.CmdLineOptions", [], CmdLineOptions, () => [["outDir", option_type(string_type)], ["libDir", option_type(string_type)], ["benchmark", bool_type], ["optimize", bool_type], ["sourceMaps", bool_type], ["typedArrays", option_type(bool_type)], ["language", string_type], ["printAst", bool_type]]);
}

export const JS_fs = fs;

export const JS_os = os;

export const JS_proc = process;

export const JS_path = path_2;

export const JS_util = util;

export function readAllBytes(filePath) {
    return JS_fs.readFileSync(filePath);
}

export function readAllText(filePath) {
    return trimStart(JS_fs.readFileSync(filePath, "utf8"), "﻿");
}

export function writeAllText(filePath, text) {
    JS_fs.writeFileSync(filePath, text);
}

export function measureTime(f, x) {
    const startTime = JS_proc.hrtime();
    const res = f(x);
    const elapsed = JS_proc.hrtime(startTime);
    return [res, toInt64_unchecked(fromFloat64((item(0, elapsed) * 1000) + (item(1, elapsed) / 1000000)))];
}

export function ensureDirExists(dir) {
    JS_util.ensureDirExists(dir);
}

export function serializeToJson(data) {
    return JS_util.serializeToJson(data);
}

export function copyFolder(from, dest) {
    JS_util.copyFolder(from, dest);
}

export function runCmdAndExitIfFails(cmd) {
    JS_util.runCmdAndExitIfFails(cmd);
}

export function normalizePath(path) {
    return replace(path, "\\", "/");
}

export function normalizeFullPath(path) {
    return replace(JS_path.resolve(path), "\\", "/");
}

export function getRelativePath(path, pathTo) {
    const relPath = replace(JS_path.relative(path, pathTo), "\\", "/");
    if (relPath.startsWith("./") ? true : relPath.startsWith("../")) {
        return relPath;
    }
    else {
        return "./" + relPath;
    }
}

export function getHomePath() {
    return JS_os.homedir();
}

export function getDirFiles(path, extension) {
    let array;
    return sort(map((x_1) => replace(x_1, "\\", "/"), (array = JS_util.getDirFiles(path), array.filter((x) => x.endsWith(extension)))), {
        Compare: (x_2, y) => (comparePrimitives(x_2, y) | 0),
    });
}

export function getGlobFiles(path) {
    let normPath, i;
    if ((path.indexOf("*") >= 0) ? true : (path.indexOf("?") >= 0)) {
        return getDirFiles((normPath = replace(path, "\\", "/"), (i = (normPath.lastIndexOf("/") | 0), (i < 0) ? "" : substring(normPath, 0, i))), ".fs");
    }
    else {
        return [path];
    }
}

export function Path_Combine(path1, path2) {
    return ((path1.length === 0) ? path1 : (trimEnd(path1, "\\", "/") + "/")) + trimStart(path2, "\\", "/");
}

export function Path_ChangeExtension(path, ext) {
    const i = path.lastIndexOf(".") | 0;
    if (i < 0) {
        return path;
    }
    else {
        return substring(path, 0, i) + ext;
    }
}

export function Path_GetFileName(path) {
    const normPath = trimEnd(replace(path, "\\", "/"), "/");
    return substring(normPath, normPath.lastIndexOf("/") + 1);
}

export function Path_GetFileNameWithoutExtension(path) {
    const path_1 = Path_GetFileName(path);
    return substring(path_1, 0, path_1.lastIndexOf("."));
}

export function Path_GetDirectoryName(path) {
    const normPath = replace(path, "\\", "/");
    const i = normPath.lastIndexOf("/") | 0;
    if (i < 0) {
        return ".";
    }
    else {
        return substring(normPath, 0, i);
    }
}

