// Generated by ReScript, PLEASE EDIT WITH CARE

import * as Js_exn from "rescript/lib/es6/js_exn.js";
import * as Caml_obj from "rescript/lib/es6/caml_obj.js";
import * as Caml_option from "rescript/lib/es6/caml_option.js";
import * as Core__Option from "@rescript/core/src/Core__Option.res.js";
import * as Caml_js_exceptions from "rescript/lib/es6/caml_js_exceptions.js";
import * as PluginShell from "@tauri-apps/plugin-shell";

async function run(program, argsOpt, cwd) {
  var args = argsOpt !== undefined ? argsOpt : [];
  try {
    var cmd = new PluginShell.Command(program, args, {
          cwd: cwd
        });
    var output = await cmd.execute();
    return {
            TAG: "Ok",
            _0: output
          };
  }
  catch (raw_err){
    var err = Caml_js_exceptions.internalToOCamlException(raw_err);
    if (err.RE_EXN_ID === Js_exn.$$Error) {
      return {
              TAG: "Error",
              _0: Core__Option.getOr(err._1.message, "Command failed")
            };
    } else {
      return {
              TAG: "Error",
              _0: "Command failed"
            };
    }
  }
}

async function runAndCapture(program, argsOpt, cwd) {
  var args = argsOpt !== undefined ? argsOpt : [];
  var output = await run(program, args, cwd);
  if (output.TAG !== "Ok") {
    return {
            TAG: "Error",
            _0: output._0
          };
  }
  var output$1 = output._0;
  if (output$1.code === 0) {
    return {
            TAG: "Ok",
            _0: output$1.stdout
          };
  } else {
    return {
            TAG: "Error",
            _0: "Command failed with code " + output$1.code.toString() + ": " + output$1.stderr
          };
  }
}

function openUrl(url) {
  return PluginShell.open(url);
}

function openFile(path) {
  return PluginShell.open(path);
}

function openWith(path, app) {
  return PluginShell.open(path, app);
}

function make(program) {
  return {
          program: program,
          args: [],
          options: {}
        };
}

function args(builder, args$1) {
  return {
          program: builder.program,
          args: builder.args.concat(args$1),
          options: builder.options
        };
}

function arg(builder, arg$1) {
  return {
          program: builder.program,
          args: builder.args.concat([arg$1]),
          options: builder.options
        };
}

function cwd(builder, dir) {
  var newrecord = Caml_obj.obj_dup(builder.options);
  return {
          program: builder.program,
          args: builder.args,
          options: (newrecord.cwd = dir, newrecord)
        };
}

function env(builder, key, value) {
  var existing = Core__Option.getOr(builder.options.env, {});
  existing[key] = value;
  var newrecord = Caml_obj.obj_dup(builder.options);
  return {
          program: builder.program,
          args: builder.args,
          options: (newrecord.env = existing, newrecord)
        };
}

function encoding(builder, enc) {
  var newrecord = Caml_obj.obj_dup(builder.options);
  return {
          program: builder.program,
          args: builder.args,
          options: (newrecord.encoding = enc, newrecord)
        };
}

function toCommand(builder) {
  return new PluginShell.Command(builder.program, builder.args, builder.options);
}

async function execute_(builder) {
  try {
    var cmd = toCommand(builder);
    var output = await cmd.execute();
    return {
            TAG: "Ok",
            _0: output
          };
  }
  catch (raw_err){
    var err = Caml_js_exceptions.internalToOCamlException(raw_err);
    if (err.RE_EXN_ID === Js_exn.$$Error) {
      return {
              TAG: "Error",
              _0: Core__Option.getOr(err._1.message, "Command failed")
            };
    } else {
      return {
              TAG: "Error",
              _0: "Command failed"
            };
    }
  }
}

async function spawn_(builder) {
  try {
    var cmd = toCommand(builder);
    var child = await cmd.spawn();
    return {
            TAG: "Ok",
            _0: child
          };
  }
  catch (raw_err){
    var err = Caml_js_exceptions.internalToOCamlException(raw_err);
    if (err.RE_EXN_ID === Js_exn.$$Error) {
      return {
              TAG: "Error",
              _0: Core__Option.getOr(err._1.message, "Failed to spawn process")
            };
    } else {
      return {
              TAG: "Error",
              _0: "Failed to spawn process"
            };
    }
  }
}

var CommandBuilder = {
  make: make,
  args: args,
  arg: arg,
  cwd: cwd,
  env: env,
  encoding: encoding,
  toCommand: toCommand,
  execute_: execute_,
  spawn_: spawn_
};

function makeCommand(prim0, prim1, prim2) {
  return new PluginShell.Command(prim0, prim1 !== undefined ? Caml_option.valFromOption(prim1) : undefined, prim2 !== undefined ? Caml_option.valFromOption(prim2) : undefined);
}

function sidecar(prim0, prim1, prim2) {
  return PluginShell.Command.sidecar(prim0, prim1 !== undefined ? Caml_option.valFromOption(prim1) : undefined, prim2 !== undefined ? Caml_option.valFromOption(prim2) : undefined);
}

function execute(prim) {
  return prim.execute();
}

function spawn(prim) {
  return prim.spawn();
}

function onStdout(prim0, prim1) {
  return prim0.on(prim1);
}

function onStderr(prim0, prim1) {
  return prim0.on(prim1);
}

function onClose(prim0, prim1) {
  return prim0.on(prim1);
}

function write(prim0, prim1) {
  return prim0.write(prim1);
}

function kill(prim) {
  return prim.kill();
}

function open_(prim0, prim1) {
  return PluginShell.open(prim0, prim1 !== undefined ? Caml_option.valFromOption(prim1) : undefined);
}

export {
  makeCommand ,
  sidecar ,
  execute ,
  spawn ,
  onStdout ,
  onStderr ,
  onClose ,
  write ,
  kill ,
  open_ ,
  run ,
  runAndCapture ,
  openUrl ,
  openFile ,
  openWith ,
  CommandBuilder ,
}
/* @tauri-apps/plugin-shell Not a pure module */
