"use strict";

function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _styledComponents = _interopRequireDefault(require("styled-components"));
var _ = require("../../..");
var _FlexTableProvider = require("../shared/FlexTableProvider");
var _components = require("./components");
var _shared = require("../shared");
var _utils = require("../../../common/utils");
var _excluded = ["className", "expanded", "initiallyExpanded", "hoverHighlight", "hideSeparator", "clickRowToExpand", "rowType", "separatorColor", "onExpandToggle", "expandChildren", "expandItems", "children"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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 && Object.prototype.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() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
var getExpandableStyles = function getExpandableStyles(p) {
  return "\n  padding-left: ".concat(p.expandable ? p.theme.spacing.unit(2) : 0, "px;\n  border-left: ").concat(p.theme.spacing.unit(0.5), "px solid ").concat(p.expanded && p.expandable ? p.theme.color.cta : 'transparent', ";\n   \n ").concat(function () {
    if (p.hideSeparator) {
      return '';
    }
    if (p.expandable && p.expanded) {
      return '';
    }
    return "border-bottom: 1px solid ".concat(p.separatorColor(p.theme), ";");
  }(), "\n\n & > * {\n  &:first-child {\n    padding-left: 0;\n  }\n  ").concat(!p.expandable ? '&:nth-last-child(2) { padding-right: 0; }' : '', "\n }\n");
};
var StyledRow = (0, _styledComponents.default)(_.Flexbox).withConfig({
  displayName: "Row__StyledRow",
  componentId: "sc-1iamenj-0"
})(["background:", ";", " margin-right:0;margin-left:0;cursor:", ";", ""], function (p) {
  return p.theme.color.tableRowBackground;
}, function (p) {
  return p.$hoverHighlight && !p.$expanded && "&:hover {\n      background: ".concat(p.theme.color.tableRowHover, ";\n    }");
}, function (_ref) {
  var $clickRowToExpand = _ref.$clickRowToExpand;
  return $clickRowToExpand ? 'pointer' : 'auto';
}, (0, _shared.getStylesForSizes)(function (p) {
  return {
    expanded: p.$expanded,
    hideSeparator: p.$hideSeparator,
    separatorColor: p.$separatorColor,
    xs: p.$xs,
    sm: p.$sm,
    md: p.$md,
    lg: p.$lg,
    xl: p.$xl
  };
}, {
  expandable: getExpandableStyles
}));
var Row = function Row(_ref2) {
  var className = _ref2.className,
    expanded = _ref2.expanded,
    _ref2$initiallyExpand = _ref2.initiallyExpanded,
    initiallyExpanded = _ref2$initiallyExpand === void 0 ? false : _ref2$initiallyExpand,
    _ref2$hoverHighlight = _ref2.hoverHighlight,
    hoverHighlight = _ref2$hoverHighlight === void 0 ? true : _ref2$hoverHighlight,
    _ref2$hideSeparator = _ref2.hideSeparator,
    hideSeparator = _ref2$hideSeparator === void 0 ? false : _ref2$hideSeparator,
    _ref2$clickRowToExpan = _ref2.clickRowToExpand,
    clickRowToExpand = _ref2$clickRowToExpan === void 0 ? false : _ref2$clickRowToExpan,
    _ref2$rowType = _ref2.rowType,
    rowType = _ref2$rowType === void 0 ? 'content' : _ref2$rowType,
    _ref2$separatorColor = _ref2.separatorColor,
    separatorColor = _ref2$separatorColor === void 0 ? function (theme) {
      return theme.colorTokens.neutral.border_weak;
    } : _ref2$separatorColor,
    onExpandToggle = _ref2.onExpandToggle,
    expandChildren = _ref2.expandChildren,
    expandItems = _ref2.expandItems,
    children = _ref2.children,
    htmlProps = _objectWithoutProperties(_ref2, _excluded);
  if (clickRowToExpand && 'onClick' in htmlProps) {
    (0, _utils.assert)(false, 'FlexTable.Row: You can not use clickRowToExpand in combination with a custom onClick function');
  }
  var _useFlexTable = (0, _FlexTableProvider.useFlexTable)('expandable'),
    xsRow = _useFlexTable.xs,
    smRow = _useFlexTable.sm,
    mdRow = _useFlexTable.md,
    lgRow = _useFlexTable.lg,
    xlRow = _useFlexTable.xl;
  var _useFlexTable2 = (0, _FlexTableProvider.useFlexTable)('columnDistance'),
    xsFlexbox = _useFlexTable2.xs,
    smFlexbox = _useFlexTable2.sm,
    mdFlexbox = _useFlexTable2.md,
    lgFlexbox = _useFlexTable2.lg,
    xlFlexbox = _useFlexTable2.xl;
  var controlledExpand = expanded !== undefined;
  var _useState = (0, _react.useState)(controlledExpand ? expanded : initiallyExpanded),
    _useState2 = _slicedToArray(_useState, 2),
    expand = _useState2[0],
    setExpand = _useState2[1];
  var onExpandToggleClick = function onExpandToggleClick() {
    if (onExpandToggle) {
      onExpandToggle(!expand);
    }
    if (!controlledExpand) {
      setExpand(!expand);
    }
  };
  (0, _react.useEffect)(function () {
    if (controlledExpand) {
      setExpand(expanded);
    }
  }, [controlledExpand, expanded]);
  return _react.default.createElement(_react.default.Fragment, null, _react.default.createElement(StyledRow, _extends({
    container: true,
    role: "row",
    className: className,
    alignItems: "center",
    $expanded: !!expand,
    $hideSeparator: hideSeparator,
    $hoverHighlight: hoverHighlight,
    $separatorColor: separatorColor,
    $clickRowToExpand: clickRowToExpand,
    $xs: xsRow,
    $sm: smRow,
    $md: mdRow,
    $lg: lgRow,
    $xl: xlRow,
    gap: xsFlexbox === null || xsFlexbox === void 0 ? void 0 : xsFlexbox.columnDistance,
    sm: {
      gap: smFlexbox === null || smFlexbox === void 0 ? void 0 : smFlexbox.columnDistance
    },
    md: {
      gap: mdFlexbox === null || mdFlexbox === void 0 ? void 0 : mdFlexbox.columnDistance
    },
    lg: {
      gap: lgFlexbox === null || lgFlexbox === void 0 ? void 0 : lgFlexbox.columnDistance
    },
    xl: {
      gap: xlFlexbox === null || xlFlexbox === void 0 ? void 0 : xlFlexbox.columnDistance
    },
    onClick: clickRowToExpand ? onExpandToggleClick : undefined
  }, htmlProps), children, _react.default.createElement(_components.ExpandElement, {
    rowType: rowType,
    expanded: expand,
    onExpandToggle: onExpandToggleClick,
    disabled: !expandChildren && !expandItems,
    setExpand: setExpand
  })), expand && _react.default.createElement(_components.ExpandRow, {
    expandItems: expandItems,
    expandChildren: expandChildren,
    separatorColor: separatorColor
  }));
};
Row.ExpandItem = _components.ExpandItem;
Row.ExpandItems = _components.ExpandItems;
Row.ExpandRow = _components.ExpandRow;
var _default = exports.default = Row;