"use strict";

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

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

var _one = require("remax/one");

var _indexModule = _interopRequireDefault(require("./styles/index.module.less"));

var _configProvider = require("../config-provider");

var _noData = _interopRequireDefault(require("../no-data"));

var _loading = _interopRequireDefault(require("../loading"));

var _shadow = _interopRequireDefault(require("../shadow"));

var _needWrap = _interopRequireDefault(require("../need-wrap"));

var _classnames = _interopRequireDefault(require("classnames"));

var _excluded = ["dataSource", "columns", "bodyStyle", "bodyCls", "rowStyle", "rowCls", "headerStyle", "headerCls", "itemStyle", "itemCls", "className", "noDataStyle", "noDataCls", "loading", "doubleColor", "shadow", "align"];

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

var convert = require('color-convert');

var _default = function _default(_ref) {
  var dataSource = _ref.dataSource,
      columns = _ref.columns,
      bodyStyle = _ref.bodyStyle,
      bodyCls = _ref.bodyCls,
      rowStyle = _ref.rowStyle,
      rowCls = _ref.rowCls,
      headerStyle = _ref.headerStyle,
      headerCls = _ref.headerCls,
      itemStyle = _ref.itemStyle,
      itemCls = _ref.itemCls,
      className = _ref.className,
      noDataStyle = _ref.noDataStyle,
      noDataCls = _ref.noDataCls,
      loading = _ref.loading,
      doubleColor = _ref.doubleColor,
      shadow = _ref.shadow,
      align = _ref.align,
      props = _objectWithoutProperties(_ref, _excluded);

  var _useConfig = (0, _configProvider.useConfig)(),
      brandPrimary = _useConfig.brandPrimary;

  var isBetween = align === 'between';

  var getJustify = function getJustify(i) {
    return isBetween ? i === 0 ? 'flex-start' : i === ((columns === null || columns === void 0 ? void 0 : columns.length) || 0) - 1 ? 'flex-end' : undefined : undefined;
  };

  return /*#__PURE__*/_react.default.createElement(_needWrap.default, {
    wrap: _shadow.default,
    need: shadow !== false,
    wrapProps: shadow
  }, /*#__PURE__*/_react.default.createElement(_one.View, _objectSpread({
    className: (0, _classnames.default)(_indexModule.default.table, className)
  }, props), /*#__PURE__*/_react.default.createElement(_one.View, {
    className: (0, _classnames.default)(_indexModule.default.header, headerCls)
  }, columns === null || columns === void 0 ? void 0 : columns.map(function (_ref2, i) {
    var title = _ref2.title;
    return /*#__PURE__*/_react.default.createElement(_one.View, {
      key: i,
      className: (0, _classnames.default)(_indexModule.default.item, itemCls),
      style: {
        justifyContent: getJustify(i)
      }
    }, title);
  })), /*#__PURE__*/_react.default.createElement(_one.View, {
    className: (0, _classnames.default)(_indexModule.default.body, bodyCls),
    style: bodyStyle
  }, loading ? /*#__PURE__*/_react.default.createElement(_loading.default, {
    type: 'inline'
  }) : /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, !(dataSource === null || dataSource === void 0 ? void 0 : dataSource.length) && /*#__PURE__*/_react.default.createElement(_noData.default, {
    style: noDataStyle,
    className: noDataCls
  }), dataSource === null || dataSource === void 0 ? void 0 : dataSource.map(function (item, i) {
    return /*#__PURE__*/_react.default.createElement(_one.View, {
      key: i,
      className: (0, _classnames.default)(_indexModule.default.row, rowCls),
      style: _objectSpread(_objectSpread({}, rowStyle), i % 2 ? {
        backgroundColor: doubleColor || "rgba(".concat(convert.hex.rgb(brandPrimary).join(','), ", 0.1)")
      } : undefined)
    }, columns === null || columns === void 0 ? void 0 : columns.map(function (_ref3, i) {
      var dataIndex = _ref3.dataIndex,
          _ref3$render = _ref3.render,
          render = _ref3$render === void 0 ? function (v) {
        return v;
      } : _ref3$render;
      return /*#__PURE__*/_react.default.createElement(_one.View, {
        style: _objectSpread({
          justifyContent: getJustify(i)
        }, itemStyle),
        className: (0, _classnames.default)(_indexModule.default.item, itemCls),
        key: i
      }, render(item[dataIndex], item, i));
    }));
  })))));
};

exports.default = _default;