"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: !0
}), exports.commonDelete = commonDelete, exports.commonGet = commonGet, exports.commonPost = commonPost, 
exports.commonPut = commonPut, exports.compleHeaders = void 0, exports.getToken = getToken, 
exports.getUser = getUser, exports.setToken = setToken, exports.setUser = setUser;

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")), _amosProcessor = require("amos-processor"), _config = _interopRequireDefault(require("amos-processor/lib/config")), _amosTool = require("amos-tool"), _amosFramework = require("amos-framework"), _payload = _interopRequireDefault(require("./payload"));

function ownKeys(e, o) {
  var r = Object.keys(e);
  if (Object.getOwnPropertySymbols) {
    var t = Object.getOwnPropertySymbols(e);
    o && (t = t.filter((function(o) {
      return Object.getOwnPropertyDescriptor(e, o).enumerable;
    }))), r.push.apply(r, t);
  }
  return r;
}

function _objectSpread(e) {
  for (var o = 1; o < arguments.length; o++) {
    var r = null != arguments[o] ? arguments[o] : {};
    o % 2 ? ownKeys(Object(r), !0).forEach((function(o) {
      (0, _defineProperty2.default)(e, o, r[o]);
    })) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(r)) : ownKeys(Object(r)).forEach((function(o) {
      Object.defineProperty(e, o, Object.getOwnPropertyDescriptor(r, o));
    }));
  }
  return e;
}

var lsTool = _amosTool.Store.lsTool, defaultHeaders = function() {
  return window.isIotPluginsToAmos ? window.iotRequestInfo.staticHeader : {
    appKey: "AMOS_IOT",
    product: "AMOS_IOT_WEB"
  };
}, compleHeaders = function() {
  return _objectSpread(_objectSpread({}, defaultHeaders()), {}, {
    token: getToken()
  });
};

function commonGet(e) {
  return (0, _amosProcessor.amosRequest)(e, {
    method: "GET",
    headers: compleHeaders(),
    reject: function(e) {
      console.log("请求失败，服务器错误！"), _amosFramework.message.danger(e.message || "请求失败，服务器错误！");
    }
  }).then((function(e) {
    return (0, _payload.default)(e);
  }));
}

function commonPost(e, o, r) {
  return (0, _amosProcessor.amosRequest)(e, {
    headers: compleHeaders(),
    method: "POST",
    body: o,
    reject: function(e) {
      console.log("请求失败，服务器错误！"), _amosFramework.message.danger(e.message || "请求失败，服务器错误！");
    }
  }).then((function(e) {
    return (0, _payload.default)(e);
  }));
}

function commonPut(e, o, r) {
  return (0, _amosProcessor.amosRequest)(e, {
    headers: compleHeaders(),
    method: "PUT",
    body: o,
    reject: function(e) {
      console.log("请求失败，服务器错误！"), _amosFramework.message.danger(e.message || "请求失败，服务器错误！");
    }
  }).then((function(e) {
    return (0, _payload.default)(e);
  }));
}

function commonDelete(e, o, r) {
  return (0, _amosProcessor.amosRequest)(e, {
    headers: compleHeaders(),
    method: "DELETE",
    body: o,
    reject: function(e) {
      console.log("请求失败，服务器错误！"), _amosFramework.message.danger(e.message || "请求失败，服务器错误！");
    }
  }).then((function(e) {
    return (0, _payload.default)(e);
  }));
}

function getToken() {
  return lsTool.read(_config.default.token);
}

function setToken(e) {
  return lsTool.write(_config.default.token, e);
}

function getUser() {
  return lsTool.read(_config.default.userId);
}

function setUser(e) {
  return lsTool.write(_config.default.userId, e);
}

exports.compleHeaders = compleHeaders;