'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _extends = Object.assign || 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; };

var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _getClassName2 = require('babel-plugin-react-css-modules/dist/browser/getClassName');

var _getClassName3 = _interopRequireDefault(_getClassName2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _omit = require('lodash/omit');

var _omit2 = _interopRequireDefault(_omit);

var _chunk = require('lodash/chunk');

var _chunk2 = _interopRequireDefault(_chunk);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _reactCollapse = require('react-collapse');

var _reactCollapse2 = _interopRequireDefault(_reactCollapse);

var _Item = require('./Item');

var _Item2 = _interopRequireDefault(_Item);

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

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

var _styleModuleImportMap = {
  './collapsible.scss': {
    'base': 'flive_collapsible_base',
    'plain-content-wrapper': 'flive_collapsible_plain-content-wrapper',
    'plain-content': 'flive_collapsible_plain-content',
    'opened': 'flive_collapsible_opened',
    'cards': 'flive_collapsible_cards',
    'card-2': 'flive_collapsible_card-2',
    'card-3': 'flive_collapsible_card-3',
    'card-4': 'flive_collapsible_card-4',
    'card-5': 'flive_collapsible_card-5',
    'card-6': 'flive_collapsible_card-6',
    'card-7': 'flive_collapsible_card-7',
    'card-8': 'flive_collapsible_card-8',
    'card-9': 'flive_collapsible_card-9',
    'card-10': 'flive_collapsible_card-10',
    'card-11': 'flive_collapsible_card-11',
    'card-12': 'flive_collapsible_card-12',
    'last-card': 'flive_collapsible_last-card'
  }
};

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

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

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

var propTypes = {
  className: _propTypes2.default.string,
  children: _propTypes2.default.func.isRequired,
  variant: _propTypes2.default.oneOf(['cards']),
  items: _propTypes2.default.arrayOf(_propTypes2.default.shape({
    id: _propTypes2.default.string.isRequired
  })).isRequired,
  itemRender: _propTypes2.default.func.isRequired,
  columns: _propTypes2.default.oneOf(['12', '11', '10', '9', '8', '7', '6', '5', '4', '3', '2']),
  selectedId: _propTypes2.default.string,
  refId: _propTypes2.default.string,
  component: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.object, _propTypes2.default.func]),
  duration: _propTypes2.default.number,
  withHashTag: _propTypes2.default.bool,
  scrollJump: _propTypes2.default.bool,
  collapse: _propTypes2.default.bool
};

var defaultProps = {
  className: undefined,
  duration: 500,
  component: 'div',
  variant: 'cards',
  columns: '4',
  selectedId: undefined,
  refId: 'id',
  children: null,
  withHashTag: false,
  scrollJump: false,
  collapse: true
};

var Collapsible = function (_PureComponent) {
  _inherits(Collapsible, _PureComponent);

  function Collapsible() {
    var _ref;

    var _temp, _this, _ret;

    _classCallCheck(this, Collapsible);

    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Collapsible.__proto__ || Object.getPrototypeOf(Collapsible)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
      selectedId: undefined
    }, _this.handlerClick = function (item) {
      return function () {
        var refId = _this.props.refId;
        var oldSelectedId = _this.state.selectedId;

        var id = item[refId];
        var selectedId = typeof _this.state.selectedId !== 'undefined' ? _this.state.selectedId : _this.props.selectedId;
        _this.setState({
          selectedId: selectedId !== id ? id : false,
          oldSelectedId: oldSelectedId
        });
      };
    }, _temp), _possibleConstructorReturn(_this, _ret);
  }

  _createClass(Collapsible, [{
    key: 'cardsRender',
    value: function cardsRender() {
      var _this2 = this;

      var _props = this.props,
          initialItems = _props.items,
          columns = _props.columns;

      var itemJunks = (0, _chunk2.default)(initialItems, parseInt(columns, 10));
      return itemJunks.reduce(function (resultArray, items, contentRow) {
        items.forEach(function (item, row) {
          resultArray.push(_this2.renderCard({ item: item, row: row }));
        });
        resultArray.push(_this2.renderPlainContent({ items: items, contentRow: contentRow }));
        return resultArray;
      }, []);
    }
  }, {
    key: 'renderCard',
    value: function renderCard(_ref2) {
      var item = _ref2.item,
          row = _ref2.row;
      var _props2 = this.props,
          itemRender = _props2.itemRender,
          initialColumns = _props2.columns,
          scrollJump = _props2.scrollJump,
          duration = _props2.duration,
          refId = _props2.refId;

      var selectedId = typeof this.state.selectedId !== 'undefined' ? this.state.selectedId : this.props.selectedId;
      var columns = parseInt(initialColumns, 10);
      return _react2.default.createElement(
        _Item2.default,
        {
          key: item[refId],
          to: 'content-row-' + item[refId],
          id: 'content-row-' + item[refId],
          onClick: this.handlerClick(item),

          active: selectedId === item[refId],
          scrollJump: scrollJump,
          duration: duration,
          className: (0, _getClassName3.default)((0, _classnames2.default)('card-' + columns, { 'last-card': row + 1 === columns }), _styleModuleImportMap)
        },
        itemRender(item)
      );
    }
  }, {
    key: 'renderPlainContent',
    value: function renderPlainContent(_ref3) {
      var items = _ref3.items,
          contentRow = _ref3.contentRow;
      var _props3 = this.props,
          children = _props3.children,
          collapse = _props3.collapse,
          refId = _props3.refId;
      var oldSelectedId = this.state.oldSelectedId;

      var selectedId = typeof this.state.selectedId !== 'undefined' ? this.state.selectedId : this.props.selectedId;
      var selectedItem = items.find(function (item) {
        return item[refId] === selectedId;
      });
      var oldSelectedItem = items.find(function (item) {
        return item[refId] === oldSelectedId;
      });

      var ComponentWrapper = collapse ? _reactCollapse2.default : 'div';
      return _react2.default.createElement(
        'div',
        {
          className: 'flive_collapsible_plain-content-wrapper',
          key: 'content-row-' + contentRow
        },
        (collapse || !!selectedItem) && _react2.default.createElement(
          ComponentWrapper,
          { isOpened: !!selectedItem },
          _react2.default.createElement(
            'div',
            { className: 'flive_collapsible_plain-content' },
            (selectedItem || oldSelectedItem) && children(selectedItem || oldSelectedItem)
          )
        )
      );
    }
  }, {
    key: 'render',
    value: function render() {
      var _props4 = this.props,
          Component = _props4.component,
          className = _props4.className,
          variant = _props4.variant,
          rest = _objectWithoutProperties(_props4, ['component', 'className', 'variant']);

      return _react2.default.createElement(
        Component,
        _extends({
          onClick: this.handleClick
        }, (0, _omit2.default)(rest, Object.keys(propTypes)), {
          className: (className ? className + ' ' : '') + (0, _getClassName3.default)((0, _classnames2.default)('base', variant), _styleModuleImportMap)
        }),
        variant === 'cards' && this.cardsRender()
      );
    }
  }]);

  return Collapsible;
}(_react.PureComponent);

Collapsible.propTypes = propTypes;
Collapsible.defaultProps = defaultProps;

exports.default = Collapsible;
module.exports = exports['default'];