"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const is = require("../_util/is.js");
var isAcceptFile = function(file, accept) {
  if (accept && file) {
    var accepts = is.isArray(accept) ? accept : accept.split(",").map(function(x) {
      return x.trim();
    }).filter(function(x) {
      return x;
    });
    var fileExtension_1 = file.name.indexOf(".") > -1 ? "." + file.name.split(".").pop() : "";
    return accepts.some(function(type) {
      var typeText = type && type.toLowerCase();
      var fileType = (file.type || "").toLowerCase();
      if (typeText === fileType) {
        return true;
      }
      if (/^\*(\/\*)?$/.test(typeText)) {
        return true;
      }
      if (/\/\*/.test(typeText)) {
        return fileType.replace(/\/.*$/, "") === typeText.replace(/\/.*$/, "");
      }
      if (/\..*/.test(typeText)) {
        var suffixList = [typeText];
        if (typeText === ".jpg" || typeText === ".jpeg") {
          suffixList = [".jpg", ".jpeg"];
        }
        return suffixList.indexOf(fileExtension_1) > -1;
      }
      return false;
    });
  }
  return !!file;
};
var getFiles = function(fileList, accept) {
  if (!fileList) {
    return;
  }
  var files = [].slice.call(fileList);
  if (accept) {
    files = files.filter(function(file) {
      return isAcceptFile(file, accept);
    });
  }
  return files;
};
var loopDirectory = function(items, accept, callback) {
  var files = [];
  var restFileCount = 0;
  var onFinish = function() {
    !restFileCount && callback(files);
  };
  var _loopDirectory = function(item) {
    restFileCount += 1;
    if (item.isFile) {
      item.file(function(file) {
        restFileCount -= 1;
        if (isAcceptFile(file, accept)) {
          Object.defineProperty(file, "webkitRelativePath", {
            value: item.fullPath.replace(/^\//, "")
          });
          files.push(file);
        }
        onFinish();
      });
      return;
    }
    if (item.isDirectory) {
      var reader_1 = item.createReader();
      var flag_1 = false;
      var readEntries_1 = function() {
        reader_1.readEntries(function(entries) {
          if (!flag_1) {
            restFileCount -= 1;
            flag_1 = true;
          }
          if (entries.length === 0) {
            onFinish();
          } else {
            readEntries_1();
            entries.forEach(_loopDirectory);
          }
        });
      };
      readEntries_1();
      return;
    }
    restFileCount -= 1;
    onFinish();
  };
  var list = [].slice.call(items);
  list.forEach(function(item) {
    if (item.webkitGetAsEntry) {
      _loopDirectory(item.webkitGetAsEntry());
    }
  });
};
exports.getFiles = getFiles;
exports.isAcceptFile = isAcceptFile;
exports.loopDirectory = loopDirectory;
//# sourceMappingURL=util.js.map
