import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";

var convertToSamplesObject = function convertToSamplesObject(fp) {
  var ext = fp.split(".").slice(-1)[0].toLowerCase();

  if (["png", "jpg", "jpeg"].includes(ext)) {
    return {
      imageUrl: "file://".concat(fp)
    };
  }

  if (["pdf"].includes(ext)) {
    return {
      pdfUrl: "file://".concat(fp)
    };
  }

  if (["mp4", "webm", "mkv"].includes(ext)) {
    return {
      videoUrl: "file://".concat(fp)
    };
  }

  return null;
};

function promptAndGetSamplesLocalDirectory(_x) {
  return _promptAndGetSamplesLocalDirectory.apply(this, arguments);
}

function _promptAndGetSamplesLocalDirectory() {
  _promptAndGetSamplesLocalDirectory = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(_ref) {
    var electron, _yield$electron$remot, canceled, dirPaths, dirPath, fs, path;

    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            electron = _ref.electron;
            _context.next = 3;
            return electron.remote.dialog.showOpenDialog({
              title: "Select Directory to Import Files",
              properties: ["openDirectory"]
            });

          case 3:
            _yield$electron$remot = _context.sent;
            canceled = _yield$electron$remot.canceled;
            dirPaths = _yield$electron$remot.filePaths;

            if (!canceled) {
              _context.next = 8;
              break;
            }

            return _context.abrupt("return");

          case 8:
            dirPath = dirPaths[0];
            fs = electron.remote.require("fs");
            path = electron.remote.require("path");
            _context.next = 13;
            return fs.promises.readdir(dirPath);

          case 13:
            return _context.abrupt("return", _context.sent.filter(function (fn) {
              return fn.includes(".");
            }).map(function (fileName) {
              return path.join(dirPath, fileName);
            }).map(convertToSamplesObject).filter(Boolean));

          case 14:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));
  return _promptAndGetSamplesLocalDirectory.apply(this, arguments);
}

export default promptAndGetSamplesLocalDirectory;