"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = configure;
exports.getConfig = getConfig;
exports.overwriteConfigMergeProps = overwriteConfigMergeProps;
exports.overwriteDefaultConfig = overwriteDefaultConfig;

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

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

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

var _mobx = require("mobx");

var _noop = _interopRequireDefault(require("lodash/noop"));

var _isObject = _interopRequireDefault(require("lodash/isObject"));

var _enum = require("../data-set/enum");

var _FeedBack = _interopRequireDefault(require("../data-set/FeedBack"));

var _ValidationMessageReportFormatter = require("../validator/ValidationMessageReportFormatter");

var _defaultStatus;

var defaultLookupCache = {
  maxAge: 1000 * 60 * 10,
  max: 100
};
var defaultStatus = (_defaultStatus = {}, (0, _defineProperty2["default"])(_defaultStatus, _enum.RecordStatus.add, 'add'), (0, _defineProperty2["default"])(_defaultStatus, _enum.RecordStatus.update, 'update'), (0, _defineProperty2["default"])(_defaultStatus, _enum.RecordStatus["delete"], 'delete'), _defaultStatus);
var defaultFormatter = {
  jsonDate: 'YYYY-MM-DD HH:mm:ss',
  date: 'YYYY-MM-DD',
  dateTime: 'YYYY-MM-DD HH:mm:ss',
  time: 'HH:mm:ss',
  year: 'YYYY',
  month: 'YYYY-MM',
  week: 'GGGG-Wo'
};
var defaultAttachment = {
  defaultFileKey: 'file',
  defaultFileSize: 0,
  defaultChunkSize: 5 * 1024 * 1024,
  defaultChunkThreads: 3,
  downloadAllMode: _enum.DownloadAllMode.readOnly,
  getDownloadUrl: function getDownloadUrl(_ref) {
    var attachment = _ref.attachment;
    return attachment.url;
  }
};
var defaultConfig = {
  lookupCache: defaultLookupCache,
  lookupUrl: function lookupUrl(code) {
    return "/common/code/".concat(code, "/");
  },
  lookupAxiosMethod: 'post',
  lovDefineUrl: function lovDefineUrl(code) {
    return "/sys/lov/lov_define?code=".concat(code);
  },
  lovQueryUrl: function lovQueryUrl(code) {
    return "/common/lov/dataset/".concat(code);
  },
  dataKey: 'rows',
  totalKey: 'total',
  countKey: 'needCountFlag',
  statusKey: '__status',
  tlsKey: '__tls',
  status: defaultStatus,
  feedback: _FeedBack["default"],
  formatter: defaultFormatter,
  attachment: defaultAttachment,
  confirm: function confirm(_message, _dataSet, _source) {
    return Promise.resolve(true);
  },
  validationMessageFormatter: _ValidationMessageReportFormatter.defaultValidationMessageFormatter,
  validationMessageReportFormatter: _ValidationMessageReportFormatter.defaultValidationMessageReportFormatter,
  xlsx: _noop["default"],
  useLookupBatch: _noop["default"],
  useLovDefineBatch: _noop["default"]
};

var defaultGlobalConfig = _mobx.observable.map(defaultConfig);

var globalConfig = _mobx.observable.map();

var baseMergeProps = ['transport', 'feedback', 'formatter', 'attachment'];

function getConfig(key) {
  if (!globalConfig.has(key)) {
    return defaultGlobalConfig.get(key);
  }

  var custom = globalConfig.get(key);

  if (baseMergeProps.includes(key)) {
    return (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, defaultGlobalConfig.get(key)), custom);
  }

  return custom;
}

function overwriteDefaultConfig(config) {
  var mergeProps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : baseMergeProps;
  (0, _mobx.runInAction)(function () {
    Object.keys(config).forEach(function (key) {
      var configKey = key;
      var value = config[configKey];

      if (mergeProps && mergeProps.includes(configKey) && (0, _isObject["default"])(value)) {
        defaultGlobalConfig.set(configKey, (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, (0, _mobx.toJS)(defaultGlobalConfig.get(configKey))), value));
      } else {
        defaultGlobalConfig.set(configKey, config[configKey]);
      }
    });
  });
}

function overwriteConfigMergeProps(customMergeProps) {
  baseMergeProps.push.apply(baseMergeProps, (0, _toConsumableArray2["default"])(customMergeProps));
}

function configure(config) {
  var mergeProps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : baseMergeProps;
  (0, _mobx.runInAction)(function () {
    Object.keys(config).forEach(function (key) {
      var configKey = key;
      var value = config[configKey];

      if (mergeProps && mergeProps.includes(configKey) && (0, _isObject["default"])(value)) {
        globalConfig.set(configKey, (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, (0, _mobx.toJS)(globalConfig.get(configKey))), value));
      } else {
        globalConfig.set(configKey, config[configKey]);
      }
    });
  });
}
//# sourceMappingURL=index.js.map
