'use strict';

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

require('./_commonjsHelpers-06173234.js');
require('./_to-object-329a650b.js');
require('./es6.string.iterator-cc0bbaa4.js');
require('./_iter-detect-60b2f026.js');
var toConsumableArray = require('./toConsumableArray-9a3476fe.js');
require('./core.get-iterator-method-f62321d4.js');
require('./_object-pie-68c5bcbc.js');
var _extends = require('./extends-60837c34.js');
require('./_object-sap-5e74ba08.js');
var React = require('react');
var React__default = _interopDefault(React);
var keys = require('./keys-a586b7a0.js');

var sortByName = function sortByName(arr) {
  var items = [].concat(toConsumableArray._toConsumableArray(arr));
  items.sort(function (a, b) {
    var nameA = a.name.toUpperCase();
    var nameB = b.name.toUpperCase();
    if (nameA < nameB) {
      return -1;
    }
    if (nameA > nameB) {
      return 1;
    }
    return 0;
  });
  return items;
};

var AlphabeticList = function AlphabeticList(_ref) {
  var _ref$dataRecord = _ref.dataRecord,
      dataRecord = _ref$dataRecord === undefined ? [] : _ref$dataRecord;

  var dataList = {};
  if (dataRecord && dataRecord.length > 0) {
    var sortedData = sortByName(dataRecord);
    sortedData.forEach(function (item) {
      var firstLetter = item.name.charAt(0).toUpperCase();
      if (!dataList[firstLetter]) {
        dataList[firstLetter] = [];
      }
      dataList[firstLetter].push(item);
    });
  }

  var alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
  var itemsRef = React.useRef([]);

  var renderAlbhabetList = function renderAlbhabetList() {
    var style = {
      fontWeight: '600',
      fontSize: '30px',
      textDecoration: 'none',
      padding: '0 10px',
      color: 'grey'
    };
    return React__default.createElement(
      'div',
      { className: 'd-flex flex-wrap mb-3' },
      alphabet.map(function (letter, i) {
        if (dataList[letter]) {
          return React__default.createElement(
            'a',
            {
              key: i,
              onClick: function onClick() {
                var index = keys._Object$keys(dataList).findIndex(function (i) {
                  return i === letter;
                });
                if (itemsRef.current && itemsRef.current[index]) {
                  window.scrollTo(0, itemsRef.current[index].offsetTop);
                }
              } },
            React__default.createElement(
              'span',
              {
                style: _extends._extends({}, style, {
                  color: 'black'
                }) },
              letter
            )
          );
        }
        return React__default.createElement(
          'span',
          { key: i, style: style },
          letter
        );
      })
    );
  };

  React.useEffect(function () {
    itemsRef.current = itemsRef.current.slice(0, keys._Object$keys(dataList).length);
  }, [dataList]);

  return React__default.createElement(
    React__default.Fragment,
    null,
    renderAlbhabetList(),
    keys._Object$keys(dataList).map(function (letter, index) {
      return React__default.createElement(
        React__default.Fragment,
        { key: index },
        React__default.createElement(
          'h3',
          { ref: function ref(el) {
              return itemsRef.current[index] = el;
            } },
          letter
        ),
        React__default.createElement(
          'ul',
          null,
          dataList[letter].map(function (item, i) {
            return React__default.createElement(
              'li',
              { key: i },
              React__default.createElement(
                'a',
                { href: item.url },
                item.name
              )
            );
          })
        )
      );
    })
  );
};

module.exports = AlphabeticList;
