"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];

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

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

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

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

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

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

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

var _react = _interopRequireWildcard(require("react"));

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

var _ConfigContext = _interopRequireDefault(require("../../../lib/config-provider/ConfigContext"));

var _BoardWithContext = _interopRequireDefault(require("./BoardWithContext"));

var _enum = require("./enum");

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

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

var VIEWLISTDS = '__VIEWLISTDS__';
exports.VIEWLISTDS = VIEWLISTDS;

var Board = function Board(props) {
  var _useContext = (0, _react.useContext)(_ConfigContext["default"]),
      getConfig = _useContext.getConfig,
      getCustomizable = _useContext.getCustomizable;

  var viewVisible = props.viewVisible,
      dataSet = props.dataSet,
      customizedCode = props.customizedCode,
      _props$customizable = props.customizable,
      customizable = _props$customizable === void 0 ? customizedCode ? getCustomizable('Board') : undefined : _props$customizable;
  var $customizable = customizedCode ? customizable : false;

  var _useState = (0, _react.useState)(!$customizable),
      _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
      loaded = _useState2[0],
      setLoaded = _useState2[1];

  var _useState3 = (0, _react.useState)(),
      _useState4 = (0, _slicedToArray2["default"])(_useState3, 2),
      customized = _useState4[0],
      setCustomized = _useState4[1];

  var customizedDS = (0, _react.useMemo)(function () {
    return new _dataSet["default"]({
      autoLocateFirst: false,
      paging: false,
      fields: [{
        name: _enum.ViewField.viewProps,
        type: _enum2.FieldType.json
      }, {
        name: _enum.ViewField.viewName,
        type: _enum2.FieldType.string
      }, {
        name: _enum.ViewField.viewMode,
        type: _enum2.FieldType.string,
        transformResponse: function transformResponse(value) {
          return value.split('_VIEW')[0].toLocaleLowerCase();
        },
        transformRequest: function transformRequest(value) {
          return value.concat('_VIEW').toLocaleUpperCase();
        }
      }, {
        name: _enum.ViewField.id
      }, {
        name: _enum.ViewField.activeKey
      }, {
        name: _enum.ViewField.groupField,
        bind: "".concat(_enum.ViewField.viewProps, ".").concat(_enum.ViewField.groupField)
      }, {
        name: _enum.ViewField.viewHeight,
        bind: "".concat(_enum.ViewField.viewProps, ".").concat(_enum.ViewField.viewHeight)
      }, {
        name: _enum.ViewField.cardWidth,
        bind: "".concat(_enum.ViewField.viewProps, ".").concat(_enum.ViewField.cardWidth)
      }, {
        name: _enum.ViewField.showLabel,
        bind: "".concat(_enum.ViewField.viewProps, ".").concat(_enum.ViewField.showLabel)
      }, {
        name: _enum.ViewField.displayFields,
        bind: "".concat(_enum.ViewField.viewProps, ".").concat(_enum.ViewField.displayFields)
      }, {
        name: _enum.ViewField.combineSort,
        bind: "".concat(_enum.ViewField.viewProps, ".").concat(_enum.ViewField.combineSort)
      }, {
        name: _enum.ViewField.sort,
        bind: "".concat(_enum.ViewField.viewProps, ".").concat(_enum.ViewField.sort)
      }],
      events: {
        load: function load(_ref) {
          var dataSet = _ref.dataSet;
          var activeRecord = dataSet.find(function (record) {
            return record.get(_enum.ViewField.activeKey);
          });

          if (activeRecord && viewVisible) {
            var viewType = activeRecord.get(_enum.ViewField.viewMode);

            if (viewVisible[viewType] !== false) {
              dataSet.current = dataSet.find(function (record) {
                return record.get(_enum.ViewField.activeKey);
              });
            } else {
              dataSet.current = dataSet.find(function (record) {
                return record.get(_enum.ViewField.id) === '__DEFAULT__';
              });
            }
          } else {
            dataSet.current = dataSet.find(function (record) {
              return record.get(_enum.ViewField.id) === '__DEFAULT__';
            });
          }
        }
      }
    });
  }, [customizedCode]);
  /**
   * 加载当前默认个性化视图数据(单个)
   */

  var loadCustomized = (0, _react.useCallback)( /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
    var customizedLoad, _defaultView, res, remoteCustomized, defaultView;

    return _regenerator["default"].wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            if (!customizedCode) {
              _context.next = 14;
              break;
            }

            setLoaded(false);
            customizedLoad = getConfig('customizedLoad');
            _context.prev = 3;
            _context.next = 6;
            return customizedLoad(customizedCode, 'Board', {
              type: 'default'
            });

          case 6:
            res = _context.sent;
            remoteCustomized = res ? [res] : []; // console.log('remoteCustomized', customizedCode, remoteCustomized, res)

            defaultView = (_defaultView = {
              code: customizedCode
            }, (0, _defineProperty2["default"])(_defaultView, _enum.ViewField.viewName, '初始列表视图'), (0, _defineProperty2["default"])(_defaultView, _enum.ViewField.viewMode, _enum.ViewMode.table), (0, _defineProperty2["default"])(_defaultView, _enum.ViewField.id, '__DEFAULT__'), _defaultView);

            if (remoteCustomized && remoteCustomized.length) {
              remoteCustomized.push(defaultView);
              customizedDS.loadData(remoteCustomized);
            } else {
              customizedDS.loadData([defaultView]);
            }

            dataSet.setState(VIEWLISTDS, customizedDS);

          case 11:
            _context.prev = 11;
            setLoaded(true);
            return _context.finish(11);

          case 14:
          case "end":
            return _context.stop();
        }
      }
    }, _callee, null, [[3,, 11, 14]]);
  })), [customizedCode]);
  (0, _react.useEffect)(function () {
    if ($customizable) {
      loadCustomized();
    }
  }, [$customizable, loadCustomized]);
  return loaded ? /*#__PURE__*/_react["default"].createElement(_BoardWithContext["default"], (0, _extends2["default"])({}, props, {
    customizedDS: customizedDS,
    customized: customized,
    customizedCode: customizedCode,
    setCustomized: setCustomized,
    customizable: $customizable
  })) : null;
};

Board.displayName = 'Board';
Board.defaultProps = {
  onChange: _noop["default"],
  viewMode: _enum.ViewMode.table,
  queryFields: {},
  autoQuery: false,
  viewVisible: true
};
var _default = Board;
exports["default"] = _default;
//# sourceMappingURL=Board.js.map
