"use strict";

var _typeof = require("@babel/runtime-corejs3/helpers/typeof");
var _WeakMap = require("@babel/runtime-corejs3/core-js-stable/weak-map");
var _Object$getOwnPropertyDescriptor = require("@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor");
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.makeColumnResizeTransform = makeColumnResizeTransform;
exports.useColumnResizeTransform = useColumnResizeTransform;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/slicedToArray"));
var _extends2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/extends"));
var _taggedTemplateLiteral2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/taggedTemplateLiteral"));
var _getOwnPropertySymbols = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols"));
var _slice = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/slice"));
var _concat = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/concat"));
var _react = _interopRequireWildcard(require("react"));
var _rxjs = require("rxjs");
var op = _interopRequireWildcard(require("rxjs/operators"));
var _styledComponents = _interopRequireDefault(require("styled-components"));
var _internals = require("../internals");
var _utils = require("../utils");
var _warnTransformsDeprecated = require("./warnTransformsDeprecated");
var _templateObject;
function _getRequireWildcardCache(e) { if ("function" != typeof _WeakMap) return null; var r = new _WeakMap(), t = new _WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && _Object$getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? _Object$getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
var __rest = void 0 && (void 0).__rest || function (s, e) {
  var t = {};
  for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  if (s != null && typeof _getOwnPropertySymbols.default === "function") for (var i = 0, p = (0, _getOwnPropertySymbols.default)(s); i < p.length; i++) {
    if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  }
  return t;
};
function clamp(min, x, max) {
  return Math.max(min, Math.min(max, x));
}
var RESIZE_EXPANDER_CLS = 'resize-expander';
var ResizeHandle = _styledComponents.default.span(_templateObject || (_templateObject = (0, _taggedTemplateLiteral2.default)(["\n  position: absolute;\n  top: 0;\n  bottom: 0;\n  right: -5px;\n  width: 10px;\n  cursor: col-resize;\n  z-index: 1;\n"])));
/** @deprecated transform 用法已经过时，请使用 pipeline 来对表格进行拓展 */
function makeColumnResizeTransform(_ref) {
  var sizes = _ref.sizes,
    onChangeSizes = _ref.onChangeSizes,
    _ref$minSize = _ref.minSize,
    minSize = _ref$minSize === void 0 ? 40 : _ref$minSize,
    _ref$maxSize = _ref.maxSize,
    maxSize = _ref$maxSize === void 0 ? Infinity : _ref$maxSize,
    appendExpander = _ref.appendExpander,
    _ref$expanderVisibili = _ref.expanderVisibility,
    expanderVisibility = _ref$expanderVisibili === void 0 ? 'visible' : _ref$expanderVisibili,
    disableUserSelectWhenResizing = _ref.disableUserSelectWhenResizing;
  (0, _warnTransformsDeprecated.warnTransformsDeprecated)('makeColumnResizeTransform');
  var startResize = function startResize(colIndex, e) {
    var startX = e.clientX;
    var startSize = sizes[colIndex];
    var nextSizes$ = (0, _rxjs.fromEvent)(window, 'mousemove').pipe(op.takeUntil((0, _rxjs.fromEvent)(window, 'mouseup')), op.map(function (e) {
      var movingX = e.clientX;
      var nextSizes = (0, _slice.default)(sizes).call(sizes);
      nextSizes[colIndex] = clamp(minSize, startSize + (movingX - startX), maxSize);
      return nextSizes;
    }));
    var prevUserSelect = '';
    var docElemStyle;
    if (disableUserSelectWhenResizing) {
      docElemStyle = document.documentElement.style;
      prevUserSelect = docElemStyle.userSelect;
      docElemStyle.userSelect = 'none';
    }
    nextSizes$.subscribe({
      next: onChangeSizes,
      complete: function complete() {
        if (disableUserSelectWhenResizing) {
          docElemStyle.userSelect = prevUserSelect;
        }
      }
    });
  };
  var innerTransform = (0, _utils.traverseColumn)(function (col, _ref2) {
    var range = _ref2.range;
    var _a;
    if (!(0, _utils.isLeafNode)(col)) {
      return col;
    }
    var prevTitle = _internals.internals.safeRenderHeader(col);
    return (0, _extends2.default)((0, _extends2.default)({}, col), {
      width: sizes[range.start],
      title: (/*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, prevTitle, /*#__PURE__*/_react.default.createElement(ResizeHandle, {
        className: "resize-handle",
        onMouseDown: function onMouseDown(e) {
          return startResize(range.start, e);
        }
      }))),
      headerCellProps: (0, _extends2.default)((0, _extends2.default)({}, col.headerCellProps), {
        style: (0, _extends2.default)((0, _extends2.default)({}, (_a = col.headerCellProps) === null || _a === void 0 ? void 0 : _a.style), {
          overflow: 'visible',
          position: 'relative'
        })
      })
    });
  });
  return function (input) {
    var _innerTransform = innerTransform(input),
      columns = _innerTransform.columns,
      dataSource = _innerTransform.dataSource;
    return {
      columns: appendExpander ? (0, _concat.default)(columns).call(columns, [{
        name: '',
        headerCellProps: {
          className: RESIZE_EXPANDER_CLS,
          style: {
            background: expanderVisibility === 'hidden' ? 'var(--bgcolor)' : undefined,
            border: expanderVisibility === 'hidden' ? 'none' : undefined
          }
        },
        getCellProps: function getCellProps() {
          return {
            className: RESIZE_EXPANDER_CLS,
            style: {
              visibility: expanderVisibility
            }
          };
        }
      }]) : columns,
      dataSource: dataSource
    };
  };
}
/** @deprecated transform 用法已经过时，请使用 pipeline 来对表格进行拓展 */
function useColumnResizeTransform(_a) {
  var defaultSizes = _a.defaultSizes,
    others = __rest(_a, ["defaultSizes"]);
  var _useState = (0, _react.useState)(defaultSizes),
    _useState2 = (0, _slicedToArray2.default)(_useState, 2),
    sizes = _useState2[0],
    onChangeSizes = _useState2[1];
  return makeColumnResizeTransform((0, _extends2.default)({
    sizes: sizes,
    onChangeSizes: onChangeSizes
  }, others));
}