/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
//
//function openFileDialog(callback) {
//    $("#open-file-input").off("change");
//    if (typeof callback != "undefined") {
//        $("#open-file-input").on("change", function () {
//            callback($("#open-file-input").val());
//        });
//    }
//    $("#open-file-input").click();
//}

/**
 * Open a save file dialog and passes the file path back.
 * @param {function} callback
 * @param {string} accept HTML5 accept="thisvar" (optional)
 * @returns {undefined}
 */
function openFileDialog(callback, accept) {
    var dialog = document.createElement("input");
    dialog.setAttribute("type", "file");
    if (typeof accept != "undefined") {
        dialog.setAttribute("accept", accept);
    }
    dialog.onchange = function () {
        callback(dialog.value, this.files[0]);
    }
    dialog.dispatchEvent(new MouseEvent("click", {
        "view": window,
        "bubbles": false,
        "cancelable": false
    }));
}

/**
 * Open a save file dialog and passes the file path back.
 * @param {function} callback
 * @param {string} defaultfilename something like file.pdf (optional)
 * @param {string} accept HTML5 accept="thisvar" (optional)
 * @returns {undefined}
 */
function openSaveFileDialog(callback, defaultfilename, accept) {
    var dialog = document.createElement("input");
    dialog.setAttribute("type", "file");
    if (typeof defaultfilename == "undefined") {
        defaultfilename = "";
    }
    dialog.setAttribute("nwsaveas", defaultfilename);
    if (typeof accept != "undefined") {
        dialog.setAttribute("accept", accept);
    }
    dialog.onchange = function () {
        callback(dialog.value);
    }
    dialog.dispatchEvent(new MouseEvent("click", {
        "view": window,
        "bubbles": false,
        "cancelable": false
    }));
}

function getFileAsString(path) {
    const fs = require("fs");
    return fs.readFileSync(path, "utf8");
}

function getFileAsUint8Array(path) {
    const fs = require("fs");
    return fs.readFileSync(path, null);
}

function writeToFile(path, data) {
    const fs = require("fs");
    fs.writeFileSync(path, data);
}

function appendToFile(path, data) {
    const fs = require("fs");
    fs.appendFileSync(path, data);
}

function copyFile(source, dest) {
    const fs = require("fs");
    fs.copyFileSync(source, dest);
}

function deleteFile(path) {
    const fs = require('fs');
    fs.unlinkSync(path);
}

function getBasename(fullpath) {
    var path = require("path");
    return path.basename(fullpath);
}