"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _react = _interopRequireDefault(require("react"));
var _wixUiIconsCommon = require("@wix/wix-ui-icons-common");
var _cloneDeep = _interopRequireDefault(require("lodash/cloneDeep"));
var _uniqueId = _interopRequireDefault(require("lodash/uniqueId"));
var _easingSt = require("../Foundation/stylable/easing.st.css.js");
var _constants = require("./constants");
var _TimeTableSt = require("./TimeTable.st.css.js");
var _SortableListBase = _interopRequireDefault(require("../SortableListBase"));
var _AddItem = _interopRequireDefault(require("../AddItem"));
var _Item = _interopRequireDefault(require("./components/Item"));
var _IconThemeContext = require("../WixDesignSystemIconThemeProvider/IconThemeContext");
var _jsxFileName = "/home/builduser/work/57e038ea7326c1ec/packages/wix-design-system/dist/cjs/TimeTable/TimeTable.jsx";
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; }
function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2["default"])(o), (0, _possibleConstructorReturn2["default"])(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2["default"])(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
var getSortableListBaseItems = function getSortableListBaseItems(columns) {
  return columns.map(function (_ref) {
    var items = _ref.items,
      _ref$disabled = _ref.disabled,
      columnDisabled = _ref$disabled === void 0 ? false : _ref$disabled;
    return items.map(function (_ref2) {
      var content = _ref2.content,
        disabled = _ref2.disabled,
        draggable = _ref2.draggable;
      var isDisabled = disabled !== undefined ? disabled : columnDisabled;
      var isDraggable = draggable !== undefined ? draggable : !isDisabled;
      return {
        id: (0, _uniqueId["default"])(),
        content: content,
        draggable: !!isDraggable,
        disabled: !!isDisabled
      };
    });
  });
};
var TimeTable = /*#__PURE__*/function (_React$PureComponent) {
  function TimeTable() {
    var _this;
    (0, _classCallCheck2["default"])(this, TimeTable);
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _callSuper(this, TimeTable, [].concat(args));
    _this.state = {
      hoveredColumnIndex: null,
      dragging: false,
      columns: [],
      sortableListBaseItems: [],
      groupName: (0, _uniqueId["default"])('time-table')
    };
    _this._renderHeader = function () {
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: _TimeTableSt.classes.headerContainer,
        __self: _this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 57,
          columnNumber: 5
        }
      }, _this.props.columns.map(function (_ref3, index) {
        var _ref3$active = _ref3.active,
          active = _ref3$active === void 0 ? false : _ref3$active,
          _ref3$disabled = _ref3.disabled,
          disabled = _ref3$disabled === void 0 ? false : _ref3$disabled,
          title = _ref3.title,
          subtitle = _ref3.subtitle,
          _ref3$droppable = _ref3.droppable,
          droppable = _ref3$droppable === void 0 ? !disabled : _ref3$droppable;
        return /*#__PURE__*/_react["default"].createElement("div", {
          key: index,
          className: (0, _TimeTableSt.st)(_TimeTableSt.classes.header, {
            active: active,
            disabled: disabled,
            droppable: droppable
          }),
          __self: _this,
          __source: {
            fileName: _jsxFileName,
            lineNumber: 69,
            columnNumber: 11
          }
        }, /*#__PURE__*/_react["default"].createElement("div", {
          "data-hook": _constants.DataHook.Title,
          className: (0, _TimeTableSt.st)(_TimeTableSt.classes.title, {
            active: active
          }),
          __self: _this,
          __source: {
            fileName: _jsxFileName,
            lineNumber: 73,
            columnNumber: 13
          }
        }, title), /*#__PURE__*/_react["default"].createElement("div", {
          "data-hook": _constants.DataHook.Subtitle,
          className: (0, _TimeTableSt.st)(_TimeTableSt.classes.subtitle, {
            active: active
          }),
          __self: _this,
          __source: {
            fileName: _jsxFileName,
            lineNumber: 79,
            columnNumber: 13
          }
        }, subtitle));
      }));
    };
    _this._renderContent = function (icons) {
      var _this$props = _this.props,
        columns = _this$props.columns,
        insertPosition = _this$props.insertPosition,
        onAdd = _this$props.onAdd,
        height = _this$props.height;
      var _this$state = _this.state,
        sortableListBaseItems = _this$state.sortableListBaseItems,
        hoveredColumnIndex = _this$state.hoveredColumnIndex,
        groupName = _this$state.groupName;
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: _TimeTableSt.classes.contentContainer,
        __self: _this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 96,
          columnNumber: 7
        }
      }, columns.map(function (_ref4, columnIndex) {
        var active = _ref4.active,
          disabled = _ref4.disabled,
          _ref4$droppable = _ref4.droppable,
          droppable = _ref4$droppable === void 0 ? !disabled : _ref4$droppable,
          actions = _ref4.actions;
        var columnIsHovered = hoveredColumnIndex === columnIndex;
        var actionsVisible = !disabled && columnIsHovered;
        var addItemVisible = actionsVisible && !!onAdd && !(actions && actions.length);
        return /*#__PURE__*/_react["default"].createElement("div", (0, _extends2["default"])({
          key: columnIndex,
          "data-hook": _constants.DataHook.Column,
          onMouseEnter: function onMouseEnter() {
            return _this._handleMouseEnter(columnIndex);
          },
          onMouseLeave: _this._handleMouseLeave,
          className: (0, _TimeTableSt.st)(_TimeTableSt.classes.column, {
            droppable: droppable
          })
        }, (0, _defineProperty2["default"])((0, _defineProperty2["default"])((0, _defineProperty2["default"])({}, _constants.DataAttribute.ColumnActive, !!active), _constants.DataAttribute.ColumnDisabled, !!disabled), _constants.DataAttribute.ColumnDroppable, droppable), {
          __self: _this,
          __source: {
            fileName: _jsxFileName,
            lineNumber: 108,
            columnNumber: 15
          }
        }), /*#__PURE__*/_react["default"].createElement(_SortableListBase["default"], {
          usePortal: true,
          className: _TimeTableSt.classes.content,
          groupName: groupName,
          onDragStart: _this._handleDragStart,
          onDragEnd: _this._handleDragEnd,
          containerId: String(columnIndex),
          items: sortableListBaseItems[columnIndex],
          droppable: droppable,
          onDrop: _this._handleDrop,
          canDrag: _this._handleCanDrag,
          renderItem: _this._renderItem,
          insertPosition: insertPosition,
          animationDuration: 500,
          animationTiming: _easingSt.stVars['ease-7'],
          style: {
            height: _this.state.dragging ? "calc(".concat(height, " - ").concat(_TimeTableSt.stVars.headerHeight, " - ").concat(_TimeTableSt.stVars.columnHeaderMargin, " - ").concat(_TimeTableSt.stVars.addItemPlaceholder, ")") : undefined
          },
          __self: _this,
          __source: {
            fileName: _jsxFileName,
            lineNumber: 122,
            columnNumber: 17
          }
        }), addItemVisible && _this._renderAddItemButton(columnIndex, icons), actionsVisible && _this._renderActions(columnIndex));
      }));
    };
    _this._renderItem = function (_ref6) {
      var isPlaceholder = _ref6.isPlaceholder,
        previewStyles = _ref6.previewStyles,
        _ref6$item = _ref6.item,
        content = _ref6$item.content,
        disabled = _ref6$item.disabled,
        draggable = _ref6$item.draggable;
      return /*#__PURE__*/_react["default"].createElement("div", (0, _extends2["default"])({
        "data-hook": _constants.DataHook.Item,
        className: (0, _TimeTableSt.st)(_TimeTableSt.classes.item, {
          isPlaceholder: isPlaceholder
        }),
        style: previewStyles
      }, (0, _defineProperty2["default"])((0, _defineProperty2["default"])({}, _constants.DataAttribute.ItemDraggable, draggable), _constants.DataAttribute.ItemDisabled, disabled), {
        __self: _this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 185,
          columnNumber: 5
        }
      }), typeof content === 'function' ? content({
        Item: _Item["default"],
        disabled: disabled,
        draggable: draggable
      }) : /*#__PURE__*/_react["default"].createElement(_Item["default"], {
        disabled: disabled,
        draggable: draggable,
        __self: _this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 197,
          columnNumber: 9
        }
      }, content));
    };
    _this._renderActions = function (columnIndex) {
      if (!_this.props.columns || !_this.props.columns[columnIndex] || !_this.props.columns[columnIndex].actions) {
        return null;
      }
      var column = _this.props.columns[columnIndex];
      var columnActions = column.actions;
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: _TimeTableSt.classes.actionsContainer,
        __self: _this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 216,
          columnNumber: 7
        }
      }, columnActions.map(function (action, index) {
        return /*#__PURE__*/_react["default"].createElement(_AddItem["default"], {
          key: index,
          className: _TimeTableSt.classes.addItemButton,
          dataHook: _constants.DataHook.ColumnAction,
          showIcon: false,
          onClick: function onClick(e) {
            action.onClick(e, column, columnIndex);
          },
          __self: _this,
          __source: {
            fileName: _jsxFileName,
            lineNumber: 219,
            columnNumber: 13
          }
        }, /*#__PURE__*/_react["default"].createElement("span", {
          className: _TimeTableSt.classes.addItemLabel,
          __self: _this,
          __source: {
            fileName: _jsxFileName,
            lineNumber: 228,
            columnNumber: 15
          }
        }, action.prefixIcon, action.label));
      }));
    };
    _this._renderAddItemButton = function (columnIndex, icons) {
      return /*#__PURE__*/_react["default"].createElement("div", {
        __self: _this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 240,
          columnNumber: 5
        }
      }, /*#__PURE__*/_react["default"].createElement(_AddItem["default"], {
        className: _TimeTableSt.classes.addItemButton,
        dataHook: _constants.DataHook.AddItemButton,
        showIcon: false,
        onClick: function onClick() {
          return _this.props.onAdd(columnIndex);
        },
        __self: _this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 241,
          columnNumber: 7
        }
      }, /*#__PURE__*/_react["default"].createElement("span", {
        className: _TimeTableSt.classes.addItemLabel,
        __self: _this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 247,
          columnNumber: 9
        }
      }, /*#__PURE__*/_react["default"].createElement(icons.AddSmall, {
        __self: _this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 248,
          columnNumber: 11
        }
      }), _this.props.addItemButtonLabel)));
    };
    _this._handleDrop = function (_ref8) {
      var addedItemIndex = _ref8.addedIndex,
        removedItemIndex = _ref8.removedIndex,
        addedToContainerId = _ref8.addedToContainerId,
        removedFromContainerId = _ref8.removedFromContainerId;
      var columns = _this.props.columns;
      var addedToColumnIndex = Number(addedToContainerId);
      var removedFromColumnIndex = Number(removedFromContainerId);
      _this.setState({
        hoveredColumnIndex: addedToColumnIndex
      });
      if (addedToColumnIndex === removedFromColumnIndex && addedItemIndex === removedItemIndex) {
        return;
      }
      var newColumns = (0, _cloneDeep["default"])(columns);
      var removedItem = columns[removedFromColumnIndex].items[removedItemIndex];
      newColumns[removedFromColumnIndex].items.splice(removedItemIndex, 1);
      newColumns[addedToColumnIndex].items.splice(addedItemIndex, 0, removedItem);
      if (_this.props.onChange) {
        _this.props.onChange(newColumns, {
          addedToColumnIndex: addedToColumnIndex,
          removedFromColumnIndex: removedFromColumnIndex,
          addedItemIndex: addedItemIndex,
          removedItemIndex: removedItemIndex
        });
      }
    };
    _this._handleMouseEnter = function (columnIndex) {
      if (!_this.state.dragging) {
        _this.setState({
          hoveredColumnIndex: columnIndex
        });
      }
    };
    _this._handleMouseLeave = function () {
      if (!_this.state.dragging) {
        _this.setState({
          hoveredColumnIndex: null
        });
      }
    };
    _this._handleDragStart = function () {
      _this.setState({
        dragging: true,
        hoveredColumnIndex: null
      });
    };
    _this._handleDragEnd = function () {
      _this.setState({
        dragging: false
      });
    };
    _this._handleCanDrag = function (_ref9) {
      var item = _ref9.item;
      return !!item.draggable;
    };
    return _this;
  }
  (0, _inherits2["default"])(TimeTable, _React$PureComponent);
  return (0, _createClass2["default"])(TimeTable, [{
    key: "render",
    value: function render() {
      var _this2 = this;
      var _this$props2 = this.props,
        dataHook = _this$props2.dataHook,
        height = _this$props2.height;
      var dragging = this.state.dragging;
      return /*#__PURE__*/_react["default"].createElement(_IconThemeContext.IconThemeContext.Consumer, {
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 159,
          columnNumber: 7
        }
      }, function (_ref0) {
        var _themeIcons$TimeTable;
        var _ref0$icons = _ref0.icons,
          themeIcons = _ref0$icons === void 0 ? {} : _ref0$icons;
        var icons = {
          AddSmall: ((_themeIcons$TimeTable = themeIcons.TimeTable) == null ? void 0 : _themeIcons$TimeTable.AddSmall) || _wixUiIconsCommon.AddSmall
        };
        return /*#__PURE__*/_react["default"].createElement("div", {
          className: (0, _TimeTableSt.st)(_TimeTableSt.classes.root, {
            dragging: dragging
          }),
          style: {
            height: height
          },
          "data-hook": dataHook,
          __self: _this2,
          __source: {
            fileName: _jsxFileName,
            lineNumber: 166,
            columnNumber: 13
          }
        }, _this2._renderHeader(), _this2._renderContent(icons));
      });
    }
  }], [{
    key: "getDerivedStateFromProps",
    value: function getDerivedStateFromProps(nextProps, prevState) {
      if (nextProps.columns === prevState.columns) {
        return null;
      }
      return _objectSpread(_objectSpread({}, prevState), {}, {
        columns: nextProps.columns,
        sortableListBaseItems: getSortableListBaseItems(nextProps.columns)
      });
    }
  }]);
}(_react["default"].PureComponent);
TimeTable.displayName = 'TimeTable';
TimeTable.defaultProps = {
  columns: [],
  insertPosition: 'any',
  height: '283px'
};
var _default = exports["default"] = TimeTable;