import { _ as _extends } from './extends-1559c147.js';
import { _ as _Object$keys } from './keys-31dcdb31.js';
import { _ as _toConsumableArray } from './toConsumableArray-f7074d7c.js';
import React__default, { useRef, useEffect } from 'react';
import './_commonjsHelpers-0c4b6f40.js';
import './core.get-iterator-method-ea258bb1.js';
import './_object-sap-7cee6f84.js';
import './_iter-detect-98db3b07.js';

var sortByName = function sortByName(arr) {
  var items = [].concat(_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 = 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 = _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({}, style, {
                  color: 'black'
                }) },
              letter
            )
          );
        }
        return React__default.createElement(
          'span',
          { key: i, style: style },
          letter
        );
      })
    );
  };

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

  return React__default.createElement(
    React__default.Fragment,
    null,
    renderAlbhabetList(),
    _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
              )
            );
          })
        )
      );
    })
  );
};

export { AlphabeticList as default };
