"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _react = _interopRequireWildcard(require("react"));

var _react2 = require("@storybook/react");

var _addonInfo = require("@storybook/addon-info");

var _index = _interopRequireDefault(require("./index"));

var _lodash = _interopRequireDefault(require("lodash"));

var _faker = _interopRequireDefault(require("faker"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var counter = 0;

var DemoTable =
/*#__PURE__*/
function (_Component) {
  _inherits(DemoTable, _Component);

  function DemoTable(props) {
    var _this;

    _classCallCheck(this, DemoTable);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(DemoTable).call(this, props));

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "_onRequestSort", function (column) {
      var columnData = _this.state.columnData;
      var orderBy = column.id;
      var order = '';

      if (column.sorted) {
        var newColumn = column;
        newColumn.sorted = true;

        if (column.order === 'asc') {
          order = 'desc';
        } else if (column.order === 'desc') {
          order = 'asc';
        }

        newColumn.order = order;

        var pos = _lodash.default.findIndex(columnData, {
          id: column.id
        });

        columnData[pos] = newColumn;

        _this.setState({
          columnData: columnData
        });
      } else {
        var _pos = _lodash.default.findIndex(columnData, {
          id: column.id
        });

        var alreadySortedColumn = _lodash.default.find(columnData, {
          sorted: true
        });

        alreadySortedColumn.sorted = false;
        columnData[_pos].sorted = true;
        columnData[_pos].order = 'asc';
        order = 'asc';

        _this.setState({
          columnData: columnData
        });
      }

      var data = order === 'desc' ? _this.state.data.sort(function (a, b) {
        return b[orderBy] < a[orderBy] ? -1 : 1;
      }) : _this.state.data.sort(function (a, b) {
        return a[orderBy] < b[orderBy] ? -1 : 1;
      });

      _this.setState({
        data: data
      });
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "_onRemoveClicked", function (row) {
      var data = _this.state.data;

      _lodash.default.remove(data, {
        id: row.id
      });

      _this.setState({
        data: data
      });
    });

    var _columnData = [{
      id: 'firstName',
      label: 'First Name',
      type: 'string'
    }, {
      id: 'lastName',
      label: 'Last Name',
      type: 'string'
    }, {
      id: 'email',
      label: 'Email',
      sorted: true,
      order: 'asc',
      type: 'string'
    }, {
      id: 'title',
      label: 'Title',
      type: 'string'
    }];

    var _data = [{
      id: 3,
      firstName: 'Eloy',
      lastName: 'Roberts',
      email: 'Anabel40@yahoo.com',
      title: 'Customer Interactions Associate'
    }, {
      id: 6,
      firstName: 'Adolphus',
      lastName: 'Cormier',
      email: 'Anne35@hotmail.com',
      title: 'Dynamic Accountability Administrator'
    }, {
      id: 4,
      firstName: 'Dewitt',
      lastName: 'Kozey',
      email: 'Cletus_King@gmail.com',
      title: 'Internal Interactions Orchestrator'
    }, {
      id: 5,
      firstName: 'Fausto',
      lastName: 'Bahringer',
      email: 'Deja5@gmail.com',
      title: 'Dynamic Infrastructure Orchestrator'
    }, {
      id: 2,
      firstName: 'Shemar',
      lastName: 'Ledner',
      email: 'Guillermo16@yahoo.com',
      title: 'Global Metrics Consultant'
    }, {
      id: 1,
      firstName: 'Toney',
      lastName: 'Ritchie',
      email: 'Hester40@yahoo.com',
      title: 'Future Integration Assistant'
    }, {
      id: 8,
      firstName: 'Al',
      lastName: 'Willms',
      email: 'Itzel76@hotmail.com',
      title: 'Lead Identity Supervisor'
    }, {
      id: 9,
      firstName: 'Kamren',
      lastName: 'Wyman',
      email: 'Magdalena50@yahoo.com',
      title: 'Lead Security Agent'
    }, {
      id: 0,
      firstName: 'Ricardo',
      lastName: 'Heidenreich',
      email: 'Nathanael.Aufderhar@yahoo.com',
      title: 'Dynamic Metrics Representative'
    }, {
      id: 7,
      firstName: 'Martin',
      lastName: 'Harvey',
      email: 'Vickie18@hotmail.com',
      title: 'Global Intranet Orchestrator'
    }].sort(function (a, b) {
      return a.email < b.email ? -1 : 1;
    });

    _this.state = {
      columnData: _columnData,
      data: _data
    };
    return _this;
  }

  _createClass(DemoTable, [{
    key: "render",
    value: function render() {
      var _this$state = this.state,
          columnData = _this$state.columnData,
          data = _this$state.data;
      return _react.default.createElement(_index.default, {
        columnData: columnData,
        data: data,
        sortable: true,
        onRequestSort: this._onRequestSort,
        onRemoveClicked: this._onRemoveClicked,
        loading: true
      });
    }
  }]);

  return DemoTable;
}(_react.Component);

exports.default = DemoTable;
(0, _react2.storiesOf)('Table', module).add('Default', (0, _addonInfo.withInfo)('Default')(function () {
  return _react.default.createElement("div", {
    style: {
      margin: '10px'
    }
  }, _react.default.createElement(DemoTable, null));
}));