"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.PbiDataSource = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _utils = require("../../../_utils/utils");
var _coreLibVueBeta = require("@cverse/core-lib-vue-beta");
var _type = require("../type");
var PbiDataSource = exports.PbiDataSource = /*#__PURE__*/function () {
  function PbiDataSource() {
    this.uuid = void 0;
    this.pbiService = void 0;
    /**
     * 用于初始化时缓存pbiService，避免多次请求
     */
    this.saveDataSourceMap = void 0;
    this.uuid = (0, _utils.uuid)();
    this.saveDataSourceMap = {};
  }
  var _proto = PbiDataSource.prototype;
  _proto.setPbiService = function setPbiService(pbiService) {
    this.pbiService = pbiService;
  };
  _proto.getViewJsonByRuleId = /*#__PURE__*/function () {
    var _getViewJsonByRuleId = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee(ruleId) {
      return _regenerator["default"].wrap(function _callee$(_context) {
        while (1) switch (_context.prev = _context.next) {
          case 0:
            _context.next = 2;
            return _coreLibVueBeta.PbiService.getViewJsonByRuleId({
              ruleId: ruleId
            });
          case 2:
            return _context.abrupt("return", _context.sent);
          case 3:
          case "end":
            return _context.stop();
        }
      }, _callee);
    }));
    function getViewJsonByRuleId(_x) {
      return _getViewJsonByRuleId.apply(this, arguments);
    }
    return getViewJsonByRuleId;
  }()
  /**
  * 编辑态，选中了万能后，开始初始化，并缓存1个小时。
  * @param type 
  */
  ;
  _proto.initPbiService =
  /*#__PURE__*/
  function () {
    var _initPbiService = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee2() {
      var pbiService;
      return _regenerator["default"].wrap(function _callee2$(_context2) {
        while (1) switch (_context2.prev = _context2.next) {
          case 0:
            pbiService = this.getInitPbiService();
            if (pbiService) {
              _context2.next = 6;
              break;
            }
            _context2.next = 4;
            return this.initPbiServiceFnInEdit();
          case 4:
            pbiService = _context2.sent;
            this.setInitPbiService(pbiService);
          case 6:
            console.log('pbiService', pbiService);
            this.setPbiService(pbiService);
          case 8:
          case "end":
            return _context2.stop();
        }
      }, _callee2, this);
    }));
    function initPbiService() {
      return _initPbiService.apply(this, arguments);
    }
    return initPbiService;
  }();
  _proto.getPbiListType = function getPbiListType(chainId) {
    var _this = this;
    var result = [];
    var type = _type.DialogConfigType.ENGINE;
    if (this.pbiService) {
      var newChainId = typeof chainId === 'string' ? parseInt(chainId) : chainId;
      var typeList = this.pbiService.getPbiListByType(newChainId, type, false);
      result = typeList.map(function (item) {
        item.value = item.id;
        item.label = _this.handleLabelName(item, type);
        item.keyList = item.keyList || [];
        item.urlAddress = item.urlAddress;
        item.handlerName = item.handlerName;
        item.ruleId = item.ruleId;
        return item;
      });
    }
    return result;
  };
  _proto.handleLabelName = function handleLabelName(item, type) {
    var result = item.name;
    if (type === _type.DialogConfigType.ENGINE) {
      result = item.name.length ? item.name + "(" + item.ruleId + ")" : "ruleId:" + item.ruleId;
    }
    return result;
  };
  _proto.getInitPbiService = function getInitPbiService() {
    var result = false;
    var res = this.saveDataSourceMap;
    var type = _type.DialogConfigType.ENGINE;
    if (!res || !res[type]) {
      return result;
    }
    var _res$type = res[type],
      value = _res$type.value,
      expired = _res$type.expired;
    var now = new Date();
    if (now.getTime() > expired) {
      return result;
    }
    return value;
  }

  /**
   * 针对编辑态，数据源初始化时的处理
   * @param projectApiType 
   */;
  _proto.initPbiServiceFnInEdit =
  /*#__PURE__*/
  function () {
    var _initPbiServiceFnInEdit = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee3() {
      var projectApiType, pbiService, hostname, newWebGid;
      return _regenerator["default"].wrap(function _callee3$(_context3) {
        while (1) switch (_context3.prev = _context3.next) {
          case 0:
            projectApiType = _type.DialogConfigType.ENGINE;
            pbiService = new _coreLibVueBeta.PbiService(null);
            hostname = document.location.hostname;
            hostname = hostname.replace('www.', '');
            newWebGid = (0, _utils.getLocationParamsByName)('webGid');
            if (!newWebGid) {
              _context3.next = 10;
              break;
            }
            _context3.next = 8;
            return pbiService.loadPbiList({
              gid: newWebGid,
              projectApiType: projectApiType
            });
          case 8:
            _context3.next = 12;
            break;
          case 10:
            _context3.next = 12;
            return pbiService.loadPbiList({
              url: hostname,
              projectApiType: projectApiType
            });
          case 12:
            return _context3.abrupt("return", pbiService);
          case 13:
          case "end":
            return _context3.stop();
        }
      }, _callee3);
    }));
    function initPbiServiceFnInEdit() {
      return _initPbiServiceFnInEdit.apply(this, arguments);
    }
    return initPbiServiceFnInEdit;
  }();
  _proto.setInitPbiService = function setInitPbiService(value) {
    var expired = new Date().getTime() + 60 * 60 * 1000;
    this.saveDataSourceMap[_type.DialogConfigType.ENGINE] = {
      value: value,
      expired: expired
    };
  };
  return PbiDataSource;
}();