import _slicedToArray from "@babel/runtime-corejs3/helpers/slicedToArray";
import _extends from "@babel/runtime-corejs3/helpers/extends";
import _taggedTemplateLiteral from "@babel/runtime-corejs3/helpers/taggedTemplateLiteral";

var _templateObject;

import _Object$getOwnPropertySymbols from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols";
import _sliceInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/slice";
import _concatInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/concat";

var __rest = this && this.__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 _Object$getOwnPropertySymbols === "function") for (var i = 0, p = _Object$getOwnPropertySymbols(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;
};

import React, { useState } from 'react';
import { fromEvent } from 'rxjs';
import * as op from 'rxjs/operators';
import styled from 'styled-components';
import { internals } from '../internals';
import { isLeafNode, traverseColumn } from '../utils';
import { warnTransformsDeprecated } from './warnTransformsDeprecated';

function clamp(min, x, max) {
  return Math.max(min, Math.min(max, x));
}

var RESIZE_EXPANDER_CLS = 'resize-expander';
var ResizeHandle = styled.span(_templateObject || (_templateObject = _taggedTemplateLiteral(["\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 来对表格进行拓展 */

export 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;
  warnTransformsDeprecated('makeColumnResizeTransform');

  var startResize = function startResize(colIndex, e) {
    var startX = e.clientX;
    var startSize = sizes[colIndex];
    var nextSizes$ = fromEvent(window, 'mousemove').pipe(op.takeUntil(fromEvent(window, 'mouseup')), op.map(function (e) {
      var movingX = e.clientX;

      var nextSizes = _sliceInstanceProperty(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 = traverseColumn(function (col, _ref2) {
    var range = _ref2.range;

    var _a;

    if (!isLeafNode(col)) {
      return col;
    }

    var prevTitle = internals.safeRenderHeader(col);
    return _extends(_extends({}, col), {
      width: sizes[range.start],
      title: /*#__PURE__*/React.createElement(React.Fragment, null, prevTitle, /*#__PURE__*/React.createElement(ResizeHandle, {
        className: "resize-handle",
        onMouseDown: function onMouseDown(e) {
          return startResize(range.start, e);
        }
      })),
      headerCellProps: _extends(_extends({}, col.headerCellProps), {
        style: _extends(_extends({}, (_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 ? _concatInstanceProperty(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 来对表格进行拓展 */

export function useColumnResizeTransform(_a) {
  var defaultSizes = _a.defaultSizes,
      others = __rest(_a, ["defaultSizes"]);

  var _useState = useState(defaultSizes),
      _useState2 = _slicedToArray(_useState, 2),
      sizes = _useState2[0],
      onChangeSizes = _useState2[1];

  return makeColumnResizeTransform(_extends({
    sizes: sizes,
    onChangeSizes: onChangeSizes
  }, others));
}