'use strict';
'use warning';

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _classPrivateFieldSet2 = _interopRequireDefault(require("@babel/runtime/helpers/classPrivateFieldSet"));

var _fs = require("fs");

var _lodash = _interopRequireDefault(require("lodash"));

var _indexModule = require("./../../index.module.d");

function _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); }

function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }

var _options = /*#__PURE__*/new WeakMap();

/**
 *
 */
var Upload = function Upload(options) {
  (0, _classCallCheck2["default"])(this, Upload);

  _classPrivateFieldInitSpec(this, _options, {
    writable: true,
    value: void 0
  });

  (0, _defineProperty2["default"])(this, "Single", /*#__PURE__*/function () {
    var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(request) {
      var AppName, uploadPath;
      return _regenerator["default"].wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              AppName = _indexModule.Server.CONFIG.serverName.split(" ").join("");
              uploadPath = _indexModule.Server.CONFIG.options.uploadDir;
              return _context2.abrupt("return", new Promise(function (resolve, rejected) {
                if ((0, _fs.existsSync)(uploadPath)) {
                  try {
                    var data = {};
                    var requestBody = Object.keys(request.body);
                    requestBody.map( /*#__PURE__*/function () {
                      var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(keys) {
                        var RenameFiles;
                        return _regenerator["default"].wrap(function _callee$(_context) {
                          while (1) {
                            switch (_context.prev = _context.next) {
                              case 0:
                                if (!request.body[keys].file) {
                                  _context.next = 12;
                                  break;
                                }

                                RenameFiles = "".concat(AppName, "_").concat(Date.now(), "_").concat(request.body[keys].filename);
                                _context.t0 = _fs.writeFileSync;
                                _context.t1 = "".concat(uploadPath, "/").concat(RenameFiles);
                                _context.next = 6;
                                return request.body[keys].toBuffer();

                              case 6:
                                _context.t2 = _context.sent;
                                _context.next = 9;
                                return (0, _context.t0)(_context.t1, _context.t2);

                              case 9:
                                data[keys] = RenameFiles;
                                _context.next = 13;
                                break;

                              case 12:
                                data[keys] = request.body[keys].value;

                              case 13:
                              case "end":
                                return _context.stop();
                            }
                          }
                        }, _callee);
                      }));

                      return function (_x2) {
                        return _ref2.apply(this, arguments);
                      };
                    }());
                    resolve({
                      status: true,
                      code: 200,
                      msg: "Berhasil",
                      data: data
                    });
                  } catch (e) {
                    rejected({
                      status: false,
                      code: 500,
                      msg: "Error Detected. Tidak Ada Folder Upload Di temukan",
                      error: e
                    });
                  }
                } else {
                  rejected({
                    status: false,
                    code: 404,
                    msg: "Error Detected. Tidak Ada Folder Upload Di temukan"
                  });
                }
              }));

            case 3:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2);
    }));

    return function (_x) {
      return _ref.apply(this, arguments);
    };
  }());
  (0, _classPrivateFieldSet2["default"])(this, _options, _lodash["default"].extend({
    limits: {
      fieldNameSize: 100,
      // Max field name size in bytes
      fieldSize: 1000000,
      // Max field value size in bytes
      fields: 10,
      // Max number of non-file fields
      fileSize: 100,
      // For multipart forms, the max file size
      files: 1,
      // Max number of file fields
      headerPairs: 2000 // Max number of header key=>value pairs

    }
  }, options));
}
/**
 * @param request
 * @returns {Promise<unknown>}
 * @constructor
 */
;

var _default = Upload;
exports["default"] = _default;
//# sourceMappingURL=index.js.map