import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import React, { useCallback, useContext, useEffect, useMemo, useState } from 'react';
import noop from 'lodash/noop';
import ConfigContext from '../../../es/config-provider/ConfigContext';
import BoardWithContext from './BoardWithContext';
import { ViewMode, ViewField } from './enum';
import DataSet from '../data-set';
import { FieldType } from '../data-set/enum';
export var VIEWLISTDS = '__VIEWLISTDS__';

var Board = function Board(props) {
  var _useContext = useContext(ConfigContext),
      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 = useState(!$customizable),
      _useState2 = _slicedToArray(_useState, 2),
      loaded = _useState2[0],
      setLoaded = _useState2[1];

  var _useState3 = useState(),
      _useState4 = _slicedToArray(_useState3, 2),
      customized = _useState4[0],
      setCustomized = _useState4[1];

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

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

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

  var loadCustomized = useCallback( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
    var customizedLoad, _defaultView, res, remoteCustomized, defaultView;

    return _regeneratorRuntime.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
            }, _defineProperty(_defaultView, ViewField.viewName, '初始列表视图'), _defineProperty(_defaultView, ViewField.viewMode, ViewMode.table), _defineProperty(_defaultView, 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]);
  useEffect(function () {
    if ($customizable) {
      loadCustomized();
    }
  }, [$customizable, loadCustomized]);
  return loaded ? /*#__PURE__*/React.createElement(BoardWithContext, _extends({}, props, {
    customizedDS: customizedDS,
    customized: customized,
    customizedCode: customizedCode,
    setCustomized: setCustomized,
    customizable: $customizable
  })) : null;
};

Board.displayName = 'Board';
Board.defaultProps = {
  onChange: noop,
  viewMode: ViewMode.table,
  queryFields: {},
  autoQuery: false,
  viewVisible: true
};
export default Board;
//# sourceMappingURL=Board.js.map
