"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = _interopRequireWildcard(require("react"));
var _classnames3 = _interopRequireDefault(require("classnames"));
var _reResizable = require("re-resizable");
var _reactIntlNext = require("react-intl-next");
var _primitives = require("@atlaskit/primitives");
var _experiments = require("@atlaskit/tmp-editor-statsig/experiments");
var _tooltip = _interopRequireDefault(require("@atlaskit/tooltip"));
var _breakout = require("../messages/breakout");
var _resizer = require("../styles/shared/resizer");
var _excluded = ["width", "children", "handleClassName", "className", "handleResize", "handleResizeStart", "handleResizeStop", "handleSize", "handleAlignmentMethod", "handlePositioning", "appearance", "handleStyles", "resizeRatio", "snap", "snapGap", "isHandleVisible", "handleHighlight", "handleTooltipContent", "needExtendedResizeZone", "childrenDOMRef", "labelComponent"]; // eslint-disable-next-line @atlaskit/design-system/no-emotion-primitives -- to be migrated to @atlaskit/primitives/compiled – go/akcss
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var resizerLabelStyles = (0, _primitives.xcss)({
  position: 'absolute',
  bottom: "var(--ds-space-0, 0px)",
  width: '100%',
  overflow: 'visible',
  display: 'flex',
  justifyContent: 'center',
  alignItems: 'center',
  height: "var(--ds-space-0, 0px)",
  zIndex: 'layer' // 400 same z-index as the floating toolbar
});
var SUPPORTED_HANDLES = ['left', 'right'];
var inheritedCSS = {
  position: 'inherit',
  height: 'inherit',
  width: 'inherit',
  display: 'inherit',
  flexDirection: 'inherit',
  justifyContent: 'inherit',
  alignItems: 'inherit'
};
var ResizerNext = function ResizerNext(props, ref) {
  var _useState = (0, _react.useState)(false),
    _useState2 = (0, _slicedToArray2.default)(_useState, 2),
    isResizing = _useState2[0],
    setIsResizing = _useState2[1];
  var resizable = (0, _react.useRef)(null);
  var resizeHandleThumbRef = (0, _react.useRef)(null);
  (0, _react.useImperativeHandle)(ref, function () {
    return {
      getResizerThumbEl: function getResizerThumbEl() {
        return resizeHandleThumbRef.current;
      }
    };
  }, [resizeHandleThumbRef]);
  var width = props.width,
    children = props.children,
    handleClassName = props.handleClassName,
    className = props.className,
    handleResize = props.handleResize,
    handleResizeStart = props.handleResizeStart,
    handleResizeStop = props.handleResizeStop,
    _props$handleSize = props.handleSize,
    handleSize = _props$handleSize === void 0 ? 'medium' : _props$handleSize,
    _props$handleAlignmen = props.handleAlignmentMethod,
    handleAlignmentMethod = _props$handleAlignmen === void 0 ? 'center' : _props$handleAlignmen,
    _props$handlePosition = props.handlePositioning,
    handlePositioning = _props$handlePosition === void 0 ? 'overlap' : _props$handlePosition,
    appearance = props.appearance,
    handleStyles = props.handleStyles,
    _props$resizeRatio = props.resizeRatio,
    resizeRatio = _props$resizeRatio === void 0 ? 1 : _props$resizeRatio,
    snap = props.snap,
    snapGap = props.snapGap,
    _props$isHandleVisibl = props.isHandleVisible,
    isHandleVisible = _props$isHandleVisibl === void 0 ? false : _props$isHandleVisibl,
    _props$handleHighligh = props.handleHighlight,
    handleHighlight = _props$handleHighligh === void 0 ? 'none' : _props$handleHighligh,
    handleTooltipContent = props.handleTooltipContent,
    _props$needExtendedRe = props.needExtendedResizeZone,
    needExtendedResizeZone = _props$needExtendedRe === void 0 ? true : _props$needExtendedRe,
    childrenDOMRef = props.childrenDOMRef,
    labelComponent = props.labelComponent,
    otherProps = (0, _objectWithoutProperties2.default)(props, _excluded);
  var onResizeStart = (0, _react.useCallback)(function (event) {
    // prevent creating a drag event on Firefox
    event.preventDefault();
    setIsResizing(true);
    handleResizeStart();
  }, [handleResizeStart]);
  var onResize = (0, _react.useCallback)(function (_event, _direction, _elementRef, delta) {
    if (!handleResize) {
      return;
    }
    var resizableCurrent = resizable.current;
    if (!resizableCurrent || !resizableCurrent.state.original) {
      return;
    }
    var originalState = {
      x: resizableCurrent.state.original.x,
      y: resizableCurrent.state.original.y,
      width: resizableCurrent.state.original.width,
      height: resizableCurrent.state.original.height
    };
    handleResize(originalState, delta);
  }, [handleResize]);
  var onResizeStop = (0, _react.useCallback)(function (_event, _direction, _elementRef, delta) {
    var resizableCurrent = resizable.current;
    if (!resizableCurrent || !resizableCurrent.state.original) {
      return;
    }
    var originalState = {
      x: resizableCurrent.state.original.x,
      y: resizableCurrent.state.original.y,
      width: resizableCurrent.state.original.width,
      height: resizableCurrent.state.original.height
    };
    setIsResizing(false);
    handleResizeStop(originalState, delta);
  }, [handleResizeStop]);
  var handles = {
    left: (0, _classnames3.default)(handleClassName !== null && handleClassName !== void 0 ? handleClassName : _resizer.resizerHandleClassName, 'left', handleSize, handleAlignmentMethod),
    right: (0, _classnames3.default)(handleClassName !== null && handleClassName !== void 0 ? handleClassName : _resizer.resizerHandleClassName, 'right', handleSize, handleAlignmentMethod)
  };
  var baseHandleStyles = {
    width: handlePositioning === 'adjacent' ? "var(--ds-space-100, 8px)" : "var(--ds-space-300, 24px)",
    zIndex: _resizer.resizerHandleZIndex,
    pointerEvents: 'auto',
    alignItems: handlePositioning === 'adjacent' ? 'center' : undefined
  };
  var offset = handlePositioning === 'adjacent' ? "calc(".concat(baseHandleStyles.width, " * -1)") : "calc(".concat(baseHandleStyles.width, " * -0.5)");

  // eslint-disable-next-line @atlassian/perf-linting/no-expensive-computations-in-render -- Ignored via go/ees017 (to be fixed)
  var nextHandleStyles = SUPPORTED_HANDLES.reduce(function (result, position) {
    return _objectSpread(_objectSpread({}, result), {}, (0, _defineProperty2.default)({}, position, _objectSpread(_objectSpread({}, baseHandleStyles), {}, (0, _defineProperty2.default)({}, position, offset), handleStyles === null || handleStyles === void 0 ? void 0 : handleStyles[position])));
  }, {});
  var resizerClassName = (0, _classnames3.default)(className, _resizer.resizerItemClassName, (0, _defineProperty2.default)({
    'is-resizing': isResizing,
    'display-handle': isHandleVisible
  }, _resizer.resizerDangerClassName, appearance === 'danger'));
  var resizerZoneClassName = (0, _classnames3.default)(_resizer.resizerHoverZoneClassName, (0, _defineProperty2.default)({}, _resizer.resizerExtendedZone, needExtendedResizeZone));
  var _useIntl = (0, _reactIntlNext.useIntl)(),
    formatMessage = _useIntl.formatMessage;
  var handleComponent = (0, _react.useMemo)(function () {
    return SUPPORTED_HANDLES.reduce(function (result, position) {
      var thumb = /*#__PURE__*/_react.default.createElement("button", {
        // eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop -- Ignored via go/DSP-18766
        className: _resizer.resizerHandleThumbClassName,
        "data-testid": "resizer-handle-".concat(position, "-thumb"),
        "aria-label": formatMessage(_breakout.messages.resizeHandle),
        contentEditable: false,
        ref: resizeHandleThumbRef,
        type: "button",
        tabIndex: -1 //We want to control focus on this button ourselves
      });
      if ((!handleHighlight || handleHighlight === 'none') && !handleTooltipContent) {
        return _objectSpread(_objectSpread({}, result), {}, (0, _defineProperty2.default)({}, position, thumb));
      }
      var thumbWithTrack =
      /*#__PURE__*/
      //It's important to have {thumb} element before the div, the thumb element is the one that gets focus and only the 1st element recives aria-descibedby attribute which is important for screen reader users
      _react.default.createElement(_react.default.Fragment, null, thumb, /*#__PURE__*/_react.default.createElement("div", {
        // eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop -- Ignored via go/DSP-18766
        className: (0, _classnames3.default)(_resizer.resizerHandleTrackClassName, handleHighlight),
        "data-testid": "resizer-handle-".concat(position, "-track")
      }));
      if (!!handleTooltipContent) {
        return _objectSpread(_objectSpread({}, result), {}, (0, _defineProperty2.default)({}, position,
        /*#__PURE__*/
        // eslint-disable-next-line @atlaskit/ui-styling-standard/enforce-style-prop -- Ignored via go/DSP-18766
        _react.default.createElement("div", {
          contentEditable: false,
          style: inheritedCSS
        }, /*#__PURE__*/_react.default.createElement(_tooltip.default, {
          content: handleTooltipContent,
          hideTooltipOnClick: true,
          position: "mouse",
          mousePosition: "auto-start",
          testId: "resizer-handle-".concat(position, "-tooltip")
        }, thumbWithTrack))));
      }
      return _objectSpread(_objectSpread({}, result), {}, (0, _defineProperty2.default)({}, position,
      /*#__PURE__*/
      // eslint-disable-next-line @atlaskit/ui-styling-standard/enforce-style-prop -- Ignored via go/DSP-18766
      _react.default.createElement("div", {
        contentEditable: false,
        style: inheritedCSS
      }, thumbWithTrack)));
    }, {});
  }, [handleHighlight, handleTooltipContent, formatMessage]);

  // snapGap is usually a constant, if snap.x?.length is 0 and snapGap has a value resizer cannot be resized
  var snapGapActual = (0, _react.useMemo)(function () {
    var _snap$x, _snap$y;
    if (!snap || ((_snap$x = snap.x) === null || _snap$x === void 0 ? void 0 : _snap$x.length) === 0 && ((_snap$y = snap.y) === null || _snap$y === void 0 ? void 0 : _snap$y.length) === 0) {
      return undefined;
    }
    return snapGap;
  }, [snap, snapGap]);
  return /*#__PURE__*/_react.default.createElement(_reResizable.Resizable, (0, _extends2.default)({
    ref: resizable
    // eslint-disable-next-line @atlassian/perf-linting/no-unstable-inline-props -- Ignored via go/ees017 (to be fixed)
    ,
    size: {
      width: width !== null && width !== void 0 ? width : 'auto',
      // just content itself (no paddings)
      height: 'auto'
    }
    // eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop -- Ignored via go/DSP-18766
    ,
    className: resizerClassName,
    handleClasses: handles,
    handleWrapperClass: _resizer.handleWrapperClass,
    handleStyles: nextHandleStyles,
    onResizeStart: onResizeStart,
    onResize: onResize,
    onResizeStop: onResizeStop,
    resizeRatio: resizeRatio,
    snapGap: snapGapActual,
    snap: snap,
    handleComponent: handleComponent
    // Ignored via go/ees005
    // eslint-disable-next-line react/jsx-props-no-spreading
  }, otherProps), /*#__PURE__*/_react.default.createElement("span", {
    className: resizerZoneClassName,
    ref: function ref(_ref) {
      return childrenDOMRef && childrenDOMRef(_ref);
    }
  }, children), labelComponent && (0, _experiments.editorExperiment)('single_column_layouts', true) && /*#__PURE__*/_react.default.createElement(_primitives.Box, {
    xcss: resizerLabelStyles
  }, labelComponent));
};
var _default_1 = /*#__PURE__*/(0, _react.forwardRef)(ResizerNext);
var _default = exports.default = _default_1;