"use strict";

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

var _axios = _interopRequireDefault(require("axios"));

var _window$BSGlobal, _window$BSGlobal$tena, _window$BSGlobal2, _window$BSGlobal2$log;

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

var tenantId = ((_window$BSGlobal = window.BSGlobal) === null || _window$BSGlobal === void 0 ? void 0 : (_window$BSGlobal$tena = _window$BSGlobal.tenantInfo) === null || _window$BSGlobal$tena === void 0 ? void 0 : _window$BSGlobal$tena.Id) || 0;
var userId = ((_window$BSGlobal2 = window.BSGlobal) === null || _window$BSGlobal2 === void 0 ? void 0 : (_window$BSGlobal2$log = _window$BSGlobal2.loginUserInfo) === null || _window$BSGlobal2$log === void 0 ? void 0 : _window$BSGlobal2$log.Id) || 0; // 开启mock

var mockEnabled = false;

var requestDomain = function requestDomain() {
  if (!window.location.hostname.match(/dev/)) return "";
  return "".concat(window.location.protocol, "//www.tita.gift");
};

var getPurePath = function getPurePath(path) {
  var purePath = path;
  var hasQuery = path.match(/\?/);
  if (hasQuery) purePath = path.split("?")[0];
  return purePath;
}; // 获取指定接口的MOCK数据


var getMockData = function getMockData(version, path, method) {
  var purePath = getPurePath(path);
  var mockData = localStorage.getItem("titaui-mock");
  if (!mockData) return;
  mockData = JSON.parse(mockData); // @ts-ignore
  // eslint-disable-next-line consistent-return

  return mockData["".concat(method, "_").concat(version, "::").concat(purePath)];
}; // 存储接口返回值


var storeMockData = function storeMockData(version, path, method, data) {
  var purePath = getPurePath(path);
  var mockData = localStorage.getItem("titaui-mock");
  if (!mockData) mockData = {};else mockData = JSON.parse(mockData);
  mockData["".concat(method, "_").concat(version, "::").concat(purePath)] = data; // localStorage.setItem("titaui-mock", JSON.stringify(mockData));
}; // 请求PROXY，用于处理MOCK开启与关于的数据请求行为


var mocking = function mocking(version, path, method, getRealRequest) {
  var mockRequest;

  if (mockEnabled) {
    var storageData = getMockData(version, path, method);

    if (!storageData) {
      mockRequest = getRealRequest;
    } else {
      return function () {
        return new Promise(function (resolve) {
          resolve(storageData);
        });
      };
    }
  } else {
    mockRequest = getRealRequest;
  }

  return mockRequest;
};

var rpost = function rpost(version) {
  return function (path) {
    var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
    var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
    var mockingRequest = mocking(version, path, "post", function () {
      return _axios["default"].post("".concat(requestDomain(), "/api/").concat(version, "/").concat(tenantId, "/").concat(userId, "/").concat(path), data, options).then(function (res) {
        return res.data;
      }).then(function (data) {
        storeMockData(version, path, "post", data);
        return data;
      });
    });
    return mockingRequest();
  };
};

exports.rpost = rpost;

var rget = function rget(version) {
  return function (path) {
    var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
    var mockingRequest = mocking(version, path, "get", function () {
      return _axios["default"].get("".concat(requestDomain(), "/api/").concat(version, "/").concat(tenantId, "/").concat(userId, "/").concat(path), options).then(function (res) {
        return res.data;
      }).then(function (data) {
        storeMockData(version, path, "get", data);
        return data;
      });
    });
    return mockingRequest();
  };
};

exports.rget = rget;

var rput = function rput(version) {
  return function (path) {
    var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
    var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
    var mockingRequest = mocking(version, path, "put", function () {
      return _axios["default"].put("".concat(requestDomain(), "/api/").concat(version, "/").concat(tenantId, "/").concat(userId, "/").concat(path), data, options).then(function (res) {
        return res.data;
      }).then(function (data) {
        storeMockData(version, path, "put", data);
        return data;
      });
    });
    return mockingRequest();
  };
};

exports.rput = rput;

var rdelete = function rdelete(version) {
  return function (path) {
    var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
    var mockingRequest = mocking(version, path, "delete", function () {
      return _axios["default"]["delete"]("".concat(requestDomain(), "/api/").concat(version, "/").concat(tenantId, "/").concat(userId, "/").concat(path), data).then(function (res) {
        return res.data;
      }).then(function (data) {
        storeMockData(version, path, "delete", data);
        return data;
      });
    });
    return mockingRequest();
  };
}; // 撰取数据


exports.rdelete = rdelete;

var postForTableList = /*#__PURE__*/function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
    var body,
        res,
        _args = arguments;
    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            body = _args.length > 0 && _args[0] !== undefined ? _args[0] : {};
            _context.next = 3;
            return _axios["default"].post("/api/v1/".concat(tenantId, "/").concat(userId, "/okr/home/cptDataDrill"), body);

          case 3:
            res = _context.sent;
            return _context.abrupt("return", res.data);

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

  return function postForTableList() {
    return _ref.apply(this, arguments);
  };
}();

exports.postForTableList = postForTableList;
var _default = {
  rget: rget,
  rput: rput,
  rdelete: rdelete,
  rpost: rpost,
  postForTableList: postForTableList
};
exports["default"] = _default;