"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var React = _react;
require("./index.scss");
var _img = _interopRequireDefault(require("../../assets/icon/position/img.png"));
var _location = _interopRequireDefault(require("../../assets/icon/position/location.png"));
var _searchBar = _interopRequireDefault(require("../search-bar/search-bar"));
var _textUtil = require("../../utils/textUtil");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
var ContentList = function NavigationList(_ref) {
  var open = _ref.open,
    itemArray = _ref.itemArray,
    setIsOpen = _ref.setIsOpen,
    setCurrent = _ref.setCurrent,
    currentLocation = _ref.currentLocation;
  var indexKeys = [];
  /**
   * 处理城市列表
   * @param itemArrays
   * @param searchValue
   */
  var handleItem = function handleItem(itemArrays, searchValue) {
    if (!itemArrays) {
      return {};
    }
    var groupName = (0, _textUtil.groupCitiesByInitial)(itemArrays);
    indexKeys = Object.keys(groupName);
    if (searchValue) {
      // 根据关键字过滤
      indexKeys.forEach(function (item) {
        var cityArray = groupName[item];
        var filterCity = cityArray.filter(function (locationItem) {
          return locationItem.address.indexOf(searchValue) != -1;
        });
        if (!filterCity || filterCity.length == 0) {
          // 如果不存在关键字匹配，则删掉整个key
          delete groupName[item];
        } else {
          groupName[item] = filterCity;
        }
      });
    }
    return groupName;
  };
  var _useState = (0, _react.useState)(handleItem(itemArray)),
    cityList = _useState[0],
    setCityList = _useState[1];
  console.log('cityList', cityList);
  var changeHandle = function changeHandle(position) {
    console.log('changeHandle', position);
    // 设置当前选中位置
    setCurrent(position);
    // 关闭位置列表
    setIsOpen();
  };
  var indexClickHandle = function indexClickHandle(index) {
    var indexStr = 'index-' + index;
    var dom = document.getElementById(indexStr);
    if (dom) {
      dom.scrollIntoView({
        behavior: 'smooth',
        block: 'start'
      });
    }
  };
  var handleSearch = function handleSearch(name) {
    var data = handleItem(itemArray, name);
    setCityList(data);
  };
  var hotCityArray = itemArray ? itemArray.filter(function (item) {
    return item.isHot;
  }) : [];
  return /*#__PURE__*/React.createElement("div", {
    className: "location-list",
    style: {
      display: open ? 'block' : 'none'
    }
  }, /*#__PURE__*/React.createElement("div", {
    className: "location-list-head"
  }, /*#__PURE__*/React.createElement("img", {
    src: _img["default"],
    className: "location-list-head-img",
    onClick: setIsOpen
  }), /*#__PURE__*/React.createElement(_searchBar["default"], {
    onConfirm: function onConfirm(value) {
      return handleSearch(value);
    }
  })), /*#__PURE__*/React.createElement("div", {
    className: "location-city-box location-list-location"
  }, /*#__PURE__*/React.createElement("img", {
    src: _location["default"],
    className: "location-list-location-img"
  }), /*#__PURE__*/React.createElement("span", {
    className: "location-list-location-text",
    onClick: setIsOpen
  }, currentLocation.address)), hotCityArray && hotCityArray.length ? /*#__PURE__*/React.createElement("div", {
    className: "location-list-hot"
  }, /*#__PURE__*/React.createElement("div", {
    className: "location-list-hot-title"
  }, "\u70ED\u95E8\u57CE\u5E02"), /*#__PURE__*/React.createElement("div", {
    className: "location-list-hot-content"
  }, hotCityArray ? hotCityArray.map(function (item) {
    return /*#__PURE__*/React.createElement("div", {
      className: "location-city-box location-list-hot-content-item",
      onClick: function onClick() {
        return changeHandle(item);
      }
    }, item.address);
  }) : '')) : '', /*#__PURE__*/React.createElement("div", {
    className: "location-list-city"
  }, cityList ? Object.keys(cityList).map(function (item) {
    return /*#__PURE__*/React.createElement("div", {
      className: "location-list-city-box"
    }, /*#__PURE__*/React.createElement("div", {
      className: "location-list-city-box-type",
      id: "index-" + item
    }, item), cityList[item].map(function (cityInfo) {
      return /*#__PURE__*/React.createElement("div", {
        className: "location-list-city-box-name",
        onClick: function onClick() {
          return changeHandle(cityInfo);
        }
      }, cityInfo.address);
    }));
  }) : ''), /*#__PURE__*/React.createElement("div", {
    className: "location-list-index-bar"
  }, cityList ? Object.keys(cityList).map(function (item) {
    return /*#__PURE__*/React.createElement("div", {
      className: "location-list-index-bar-name",
      onClick: function onClick() {
        return indexClickHandle(item);
      }
    }, item);
  }) : ''));
};
var _default = exports["default"] = ContentList;