"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _icon = require("../../../components/icon/icon");
var _flex = _interopRequireDefault(require("../../../components/templates/flex"));
var _useStylesTab2 = _interopRequireDefault(require("./use-styles-tab"));
var _excluded = ["active", "onActivate", "index", "onMouseOver", "onMouseOut", "onRemove", "fixed", "icon", "children", "showBorderLeft", "isDragOverlay", "draggable", "handleProps", "listeners", "attributes", "id", "style", "dragging", "sorting", "collapsed", "rootProps"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
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; }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.indexOf(n) >= 0) continue; t[n] = r[n]; } return t; }
var Tab = /*#__PURE__*/(0, _react.forwardRef)(function (_ref, ref) {
  var active = _ref.active,
    onActivate = _ref.onActivate,
    index = _ref.index,
    mouseOver = _ref.onMouseOver,
    mouseOut = _ref.onMouseOut,
    onRemove = _ref.onRemove,
    fixed = _ref.fixed,
    icon = _ref.icon,
    children = _ref.children,
    showBorderLeft = _ref.showBorderLeft,
    isDragOverlay = _ref.isDragOverlay,
    draggable = _ref.draggable,
    handleProps = _ref.handleProps,
    listeners = _ref.listeners,
    attributes = _ref.attributes,
    id = _ref.id,
    style = _ref.style,
    dragging = _ref.dragging,
    sorting = _ref.sorting,
    collapsed = _ref.collapsed,
    rootProps = _ref.rootProps,
    rest = _objectWithoutPropertiesLoose(_ref, _excluded);
  var _useState = (0, _react.useState)(),
    hover = _useState[0],
    setHover = _useState[1];
  var _useStylesTab = (0, _useStylesTab2["default"])({
      active: active,
      showBorderLeft: showBorderLeft,
      isDragOverlay: isDragOverlay
    }),
    rootStyles = _useStylesTab.rootStyles;
  var onClickTab = (0, _react.useCallback)(function (event) {
    if (event) event.preventDefault();
    if (onActivate) onActivate();
  }, [onActivate]);
  var onMouseOver = (0, _react.useCallback)(function (event) {
    setHover(true);
    if (mouseOver) mouseOver(event);
  }, [mouseOver]);
  var onMouseOut = (0, _react.useCallback)(function (event) {
    setHover(false);
    if (mouseOut) mouseOut(event);
  }, [mouseOut]);
  var onCloseTab = (0, _react.useCallback)(function (event) {
    event.preventDefault();
    event.stopPropagation();
    if (onRemove) onRemove(index, active);
  }, [onRemove, index, active]);
  var renderIcon = (0, _react.useCallback)(function (iconProp) {
    return /*#__PURE__*/_react["default"].cloneElement(iconProp, {
      color: active ? "text" : "textLite"
    });
  }, [active]);
  var closable = hover && !fixed;
  return /*#__PURE__*/_react["default"].createElement(_flex["default"], _extends({}, rootStyles, {
    ref: ref
  }, !isDragOverlay && {
    onClick: onClickTab,
    onMouseOver: onMouseOver,
    onMouseLeave: onMouseOut
  }, {
    tabIndex: "0",
    "data-index": index,
    "data-id": id,
    style: style
  }, attributes, rootProps), /*#__PURE__*/_react["default"].createElement(_flex["default"], null, closable && !isDragOverlay && /*#__PURE__*/_react["default"].createElement(_icon.Icon, {
    name: "x",
    size: "small",
    color: active ? "text" : "textLite",
    onClick: onCloseTab
  }), !closable && icon && renderIcon(icon)), !collapsed && /*#__PURE__*/_react["default"].createElement(_flex["default"], rest, children), (draggable || isDragOverlay) && /*#__PURE__*/_react["default"].createElement(_icon.Icon, _extends({
    name: "rearrange",
    width: "10px",
    height: "10px",
    color: hover ? active ? "text" : "textLite" : "textNoFocus"
  }, handleProps, listeners, {
    cursor: sorting || dragging ? "grabbing" : "grab"
  })));
});
Tab.displayName = "Tab";
var _default = exports["default"] = Tab;