"use strict";

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

var _axios = _interopRequireDefault(require("../utils/axios"));

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 domain = "https://skynet-apis.tita.cloud"; // const domain = 'http://127.0.0.1:8080'

exports.domain = domain;

var transMetasToTableData = function transMetasToTableData() {
  var dataSource = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var listData = [];
  dataSource.forEach(function (dataItem) {
    var data = {};
    dataItem.forEach(function (field) {
      var name = field.name,
          value = field.value;
      data[name] = value;
    });
    listData.push(data);
  });
  return listData;
};

exports.transMetasToTableData = transMetasToTableData;
var storageKey = "DEMO_CHART_URI_";

var fetchAssetsVersion = function fetchAssetsVersion() {
  return _axios["default"].post("".concat(domain, "/api/v1/metas/list"), {
    filter: {
      fields: [],
      order: {
        name: "create_time_unix",
        orderBy: "desc"
      },
      limit: 1
    },
    objectName: "superset_assets_version"
  }).then(function (res) {
    if (!res.data) {
      return [];
    }

    return transMetasToTableData(res.data.list);
  });
};

exports.fetchAssetsVersion = fetchAssetsVersion;

var fetchAssetsUri = function fetchAssetsUri(version) {
  return _axios["default"].post("".concat(domain, "/api/v1/metas/list"), {
    filter: {
      fields: [{
        name: "version",
        operator: "=",
        value: version,
        type: "string"
      }],
      order: {
        name: "create_time_unix",
        orderBy: "desc"
      },
      limit: 20
    },
    objectName: "superset_assets"
  }).then(function (res) {
    if (!res.data) {
      return [];
    }

    sessionStorage.setItem("".concat(storageKey, "version"), JSON.stringify(transMetasToTableData(res.data.list)));
    return transMetasToTableData(res.data.list);
  });
};

exports.fetchAssetsUri = fetchAssetsUri;

var hasCachedAssets = function hasCachedAssets(resVersion) {
  var _JSON$parse$;

  var cacheAssets = sessionStorage.getItem("".concat(storageKey, "version"));
  var cacheVersion = cacheAssets && ((_JSON$parse$ = JSON.parse(cacheAssets)[0]) === null || _JSON$parse$ === void 0 ? void 0 : _JSON$parse$.version);
  return cacheAssets !== null && cacheVersion === resVersion;
};

var getAssetsWithCache = /*#__PURE__*/function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
    var _res$;

    var res, cacheAssets, resVersion, assetsUri;
    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _context.next = 2;
            return fetchAssetsVersion();

          case 2:
            res = _context.sent;
            cacheAssets = sessionStorage.getItem("".concat(storageKey, "version"));
            resVersion = res === null || res === void 0 ? void 0 : (_res$ = res[0]) === null || _res$ === void 0 ? void 0 : _res$.version;

            if (resVersion) {
              _context.next = 7;
              break;
            }

            return _context.abrupt("return", []);

          case 7:
            if (!hasCachedAssets(resVersion)) {
              _context.next = 9;
              break;
            }

            return _context.abrupt("return", JSON.parse(cacheAssets));

          case 9:
            _context.next = 11;
            return fetchAssetsUri(resVersion);

          case 11:
            assetsUri = _context.sent;
            return _context.abrupt("return", assetsUri);

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

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

var _default = getAssetsWithCache;
exports["default"] = _default;