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); }
import _omit from "ramda/src/omit";
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 _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 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) { _defineProperty(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 _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread 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 _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
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; }
import React, { useMemo, useState } from 'react';
import { Box, FlexTable, Typography } from '../../..';
import docs from '../FlexTable.mdx';
import { StyledBackground } from './storiesShared';
var meta = {
  component: FlexTable,
  title: 'Molecules / FlexTable / With lots of rows',
  parameters: {
    docs: {
      page: docs
    }
  }
};
export default meta;
var generateUniqueId = function generateUniqueId(rowIndex) {
  return "".concat(rowIndex, "_").concat(Math.random().toString(36).substr(2, 9));
};
var generateTableData = function generateTableData(rowsLength, columnsLength) {
  var _ref;
  return (_ref = _toConsumableArray(Array(rowsLength))) === null || _ref === void 0 ? void 0 : _ref.map(function (_, rowIndex) {
    var rowId = generateUniqueId(rowIndex);
    return _toConsumableArray(Array(columnsLength)).reduce(function (acc, __, columnIndex) {
      var keyName = "value".concat(columnIndex + 1);
      var id = generateUniqueId(columnIndex);
      return _objectSpread(_objectSpread({}, acc), {}, _defineProperty({
        rowId: rowId
      }, keyName, {
        value: "Cell ".concat(rowIndex + 1, "-").concat(columnIndex + 1),
        id: id
      }));
    }, {});
  });
};
var BigTableRow = function BigTableRow(_ref2) {
  var _Object$keys;
  var data = _ref2.data;
  var xsColumnKeys = Object.keys(data).filter(function (_, index) {
    return Boolean(index % 2);
  });
  var expandItems = xsColumnKeys === null || xsColumnKeys === void 0 ? void 0 : xsColumnKeys.map(function (key, index) {
    return {
      label: "Header ".concat(index * 2 + 1),
      value: data[key].value
    };
  }, {});
  return React.createElement(FlexTable.Row, {
    expandItems: expandItems
  }, (_Object$keys = Object.keys(_omit(['rowId'], data))) === null || _Object$keys === void 0 ? void 0 : _Object$keys.map(function (valueKey, index) {
    return React.createElement(FlexTable.Cell, {
      key: data[valueKey].id,
      columnId: "column".concat(index + 1),
      hidden: Boolean(index % 2),
      md: {
        hidden: false
      }
    }, data[valueKey].value);
  }));
};
var BigTableTemplate = function BigTableTemplate(_ref3) {
  var columns = _ref3.columns,
    rows = _ref3.rows;
  var BigTableExample = function BigTableExample() {
    var ReactComponent = function ReactComponent() {
      var _ref4;
      var rowsLength = rows;
      var columnsLength = columns;
      var _useState = useState({}),
        _useState2 = _slicedToArray(_useState, 2),
        sort = _useState2[0],
        setSort = _useState2[1];
      var tableData = useMemo(function () {
        return generateTableData(rowsLength, columnsLength);
      }, [rowsLength, columnsLength]);
      var sortedData = useMemo(function () {
        if (sort.sortOrder === 'none') {
          return tableData;
        }
        var getValue = function getValue(rowData) {
          return rowData[sort.columnId.replace('column', 'value')].value;
        };
        return tableData.slice(0).sort(function (rowA, rowB) {
          if (sort.sortOrder === 'ascending') {
            return getValue(rowB).localeCompare(getValue(rowA));
          }
          if (sort.sortOrder === 'descending') {
            return getValue(rowA).localeCompare(getValue(rowB));
          }
          return 0;
        });
      }, [tableData, sort]);
      return React.createElement(FlexTable, {
        expandable: true,
        md: {
          expandable: false
        }
      }, React.createElement(FlexTable.HeaderRow, null, (_ref4 = _toConsumableArray(Array(columnsLength))) === null || _ref4 === void 0 ? void 0 : _ref4.map(function (_, index) {
        return React.createElement(FlexTable.Header, {
          columnId: "column".concat(index + 1),
          key: "column".concat(index + 1),
          sortable: true,
          onSort: function onSort(columnId, nextSortOrder) {
            setSort({
              columnId: columnId,
              sortOrder: nextSortOrder
            });
          },
          hidden: Boolean(index % 2),
          md: {
            hidden: false
          }
        }, "Header ", index + 1);
      })), sortedData === null || sortedData === void 0 ? void 0 : sortedData.map(function (data) {
        return React.createElement(BigTableRow, {
          key: data.rowId,
          data: data
        });
      }));
    };
    return React.createElement(ReactComponent, null);
  };
  return React.createElement(StyledBackground, null, React.createElement(Typography, {
    type: "title3"
  }, "Big FlexTable"), React.createElement(BigTableExample, null));
};
export var BigTable = {
  render: function render() {
    return React.createElement(BigTableTemplate, {
      rows: 100,
      columns: 10
    });
  }
};
var BigTableWithoutStickyHeaderTemplate = function BigTableWithoutStickyHeaderTemplate(_ref5) {
  var columns = _ref5.columns,
    rows = _ref5.rows;
  var BigTableWithoutStickyHeaderExample = function BigTableWithoutStickyHeaderExample() {
    var ReactComponent = function ReactComponent() {
      var _ref6;
      var rowsLength = rows;
      var columnsLength = columns;
      var _useState3 = useState({}),
        _useState4 = _slicedToArray(_useState3, 2),
        sort = _useState4[0],
        setSort = _useState4[1];
      var tableData = useMemo(function () {
        return generateTableData(rowsLength, columnsLength);
      }, [rowsLength, columnsLength]);
      var sortedData = useMemo(function () {
        var getValue = function getValue(rowData) {
          return rowData[sort.columnId.replace('column', 'value')].value;
        };
        return tableData.sort(function (rowA, rowB) {
          if (sort.sortOrder === 'ascending') {
            return getValue(rowB).localeCompare(getValue(rowA));
          }
          if (sort.sortOrder === 'descending') {
            return getValue(rowA).localeCompare(getValue(rowB));
          }
          return 0;
        });
      }, [tableData, sort]);
      return React.createElement(FlexTable, {
        stickyHeader: false
      }, React.createElement(FlexTable.HeaderRow, null, (_ref6 = _toConsumableArray(Array(columnsLength))) === null || _ref6 === void 0 ? void 0 : _ref6.map(function (_, index) {
        return React.createElement(FlexTable.Header, {
          columnId: "column".concat(index + 1),
          key: "column".concat(index + 1),
          sortable: true,
          onSort: function onSort(columnId, nextSortOrder) {
            setSort({
              columnId: columnId,
              sortOrder: nextSortOrder
            });
          }
        }, "Header ", index + 1);
      })), sortedData === null || sortedData === void 0 ? void 0 : sortedData.map(function (data) {
        return React.createElement(BigTableRow, {
          key: data.rowId,
          data: data
        });
      }));
    };
    return React.createElement(ReactComponent, null);
  };
  return React.createElement(StyledBackground, null, React.createElement(Typography, {
    type: "title3"
  }, "Big FlexTable Without Sticky Header"), React.createElement(BigTableWithoutStickyHeaderExample, null));
};
export var BigTableWithoutStickyHeader = {
  render: function render() {
    return React.createElement(BigTableWithoutStickyHeaderTemplate, {
      rows: 100,
      columns: 10
    });
  }
};
var MultipleBigTablesWithStickyHeadersTemplate = function MultipleBigTablesWithStickyHeadersTemplate(_ref7) {
  var columns = _ref7.columns,
    rows = _ref7.rows;
  var ReactComponent = function ReactComponent() {
    var _ref8, _ref9;
    var rowsLength = rows;
    var columnsLength = columns;
    var _useState5 = useState({}),
      _useState6 = _slicedToArray(_useState5, 2),
      sort = _useState6[0],
      setSort = _useState6[1];
    var tableData = useMemo(function () {
      return generateTableData(rowsLength, columnsLength);
    }, [rowsLength, columnsLength]);
    var sortedData = useMemo(function () {
      var getValue = function getValue(rowData) {
        return rowData[sort.columnId.replace('column', 'value')].value;
      };
      return tableData.sort(function (rowA, rowB) {
        if (sort.sortOrder === 'ascending') {
          return getValue(rowB).localeCompare(getValue(rowA));
        }
        if (sort.sortOrder === 'descending') {
          return getValue(rowA).localeCompare(getValue(rowB));
        }
        return 0;
      });
    }, [tableData, sort]);
    return React.createElement(StyledBackground, null, React.createElement(Typography, {
      type: "title3"
    }, "Table 1 - Multiple Big Tables Demonstrating Proper Sticky Header"), React.createElement(Box, {
      mb: 10
    }, React.createElement(FlexTable, null, React.createElement(FlexTable.HeaderRow, null, (_ref8 = _toConsumableArray(Array(columnsLength))) === null || _ref8 === void 0 ? void 0 : _ref8.map(function (_, index) {
      return React.createElement(FlexTable.Header, {
        columnId: "column".concat(index + 1),
        key: "column".concat(index + 1),
        sortable: true,
        onSort: function onSort(columnId, nextSortOrder) {
          setSort({
            columnId: columnId,
            sortOrder: nextSortOrder
          });
        }
      }, "Table 1 Header ", index + 1);
    })), sortedData === null || sortedData === void 0 ? void 0 : sortedData.map(function (data) {
      return React.createElement(BigTableRow, {
        key: data.rowId,
        data: data
      });
    }))), React.createElement(Typography, {
      type: "title3"
    }, "Table 2 - Multiple Big Tables Demonstrating Proper Sticky Header"), React.createElement(FlexTable, null, React.createElement(FlexTable.HeaderRow, null, (_ref9 = _toConsumableArray(Array(columnsLength))) === null || _ref9 === void 0 ? void 0 : _ref9.map(function (_, index) {
      return React.createElement(FlexTable.Header, {
        columnId: "column".concat(index + 1),
        key: "column".concat(index + 1),
        sortable: true,
        onSort: function onSort(columnId, nextSortOrder) {
          setSort({
            columnId: columnId,
            sortOrder: nextSortOrder
          });
        }
      }, "Table 2 Header ", index + 1);
    })), sortedData === null || sortedData === void 0 ? void 0 : sortedData.map(function (data) {
      return React.createElement(BigTableRow, {
        key: data.rowId,
        data: data
      });
    })));
  };
  return React.createElement(ReactComponent, null);
};
export var MultipleBigTablesWithStickyHeaders = {
  render: function render() {
    return React.createElement(MultipleBigTablesWithStickyHeadersTemplate, {
      rows: 100,
      columns: 5
    });
  }
};