"use strict";

var __assign = void 0 && (void 0).__assign || function () {
  __assign = Object.assign || function (t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];

      for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
      }
    }

    return t;
  };

  return __assign.apply(this, arguments);
};

var __createBinding = void 0 && (void 0).__createBinding || (Object.create ? function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  Object.defineProperty(o, k2, {
    enumerable: true,
    get: function get() {
      return m[k];
    }
  });
} : function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  o[k2] = m[k];
});

var __setModuleDefault = void 0 && (void 0).__setModuleDefault || (Object.create ? function (o, v) {
  Object.defineProperty(o, "default", {
    enumerable: true,
    value: v
  });
} : function (o, v) {
  o["default"] = v;
});

var __importStar = void 0 && (void 0).__importStar || function (mod) {
  if (mod && mod.__esModule) return mod;
  var result = {};
  if (mod != null) for (var k in mod) {
    if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  }

  __setModuleDefault(result, mod);

  return result;
};

var __importDefault = void 0 && (void 0).__importDefault || function (mod) {
  return mod && mod.__esModule ? mod : {
    "default": mod
  };
};

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

var jsx_runtime_1 = require("react/jsx-runtime");
/* eslint-disable @typescript-eslint/no-unused-vars */


var react_1 = __importStar(require("react"));

var input_1 = require("../../input");

var react_draggable_1 = __importDefault(require("react-draggable"));

var map_1 = __importDefault(require("lodash/map"));

var includes_1 = __importDefault(require("lodash/includes"));

var cloneDeep_1 = __importDefault(require("lodash/cloneDeep"));

var isObject_1 = __importDefault(require("lodash/isObject"));

require("../style/item-list.css");

var icon_1 = __importDefault(require("../../icon"));

var reduce_1 = __importDefault(require("lodash/reduce"));

var scrollbar_1 = __importDefault(require("../../scrollbar"));

var tooltip_1 = __importDefault(require("../../tooltip"));

var getIconName = function getIconName(geometryType) {
  switch (geometryType) {
    case 'point':
      return 'location';

    case 'line':
      return 'line';

    case 'polygon':
      return 'layer';

    case 'point_to_polygon':
      return 'location-to';

    default:
      return 'location';
  }
};

exports.ItemList = react_1["default"].memo(function (props) {
  var recId = props.recId,
      dataSources = props.dataSources,
      prefixCls = props.prefixCls,
      selectDataSourceId = props.selectDataSourceId,
      handleSelPkgChange = props.handleSelPkgChange,
      handleSelRecChange = props.handleSelRecChange,
      dataSourceDataUniqueKey = props.dataSourceDataUniqueKey,
      getTooltipContainer = props.getTooltipContainer;
  var moreThanOneRec = dataSources.length >= 1 || dataSources.length === 1 && dataSources[0].data.length > 1;

  var _a = react_1.useState(dataSources),
      _dataSources = _a[0],
      setInnerDataSources = _a[1];

  var _b = react_1.useState(160),
      searchWidth = _b[0],
      setSearchWidth = _b[1];

  var _c = react_1.useState(160),
      originWidth = _c[0],
      setOriginWidth = _c[1];

  var _d = react_1.useState(0),
      originClientX = _d[0],
      setOriginClientX = _d[1];

  var totalDataLen = react_1.useMemo(function () {
    return reduce_1["default"](dataSources, function (acc, _a) {
      var data = _a.data;
      return acc + data.length;
    }, 0);
  }, [dataSources]);
  var style = react_1.useMemo(function () {
    return {
      width: searchWidth,
      left: Math.min(-searchWidth, 0)
    };
  }, [searchWidth]);
  var handleSearch = react_1.useCallback(function (e) {
    var _a;

    var v = (_a = e.target) === null || _a === void 0 ? void 0 : _a.value;

    if (v) {
      var ds = cloneDeep_1["default"](dataSources);
      var curDataSources = ds.reduce(function (acc, i) {
        if (i.objectType.includes(v)) {
          acc.push(i);
          return acc;
        } else {
          var validRec = i.data.filter(function (d) {
            var _a;

            return ((_a = i.infoCfg) === null || _a === void 0 ? void 0 : _a.some(function (cfg) {
              return includes_1["default"](d[cfg.key], v);
            })) || Object.values(d).some(function (val) {
              return includes_1["default"](val, v);
            });
          });

          if (validRec.length > 0) {
            acc.push(__assign(__assign({}, i), {
              data: validRec
            }));
          }
        }

        return acc;
      }, []);
      setInnerDataSources(curDataSources);
    }
  }, [dataSources]);
  var handleChange = react_1.useCallback(function (e) {
    var v = e.target.value;

    if (v === '') {
      setInnerDataSources(dataSources);
    }
  }, [dataSources]);

  var _handleSelPkgChange = react_1.useCallback(function (e) {
    var el = e.currentTarget;
    el && handleSelPkgChange(el.dataset.pkgUuid || '');
  }, [handleSelPkgChange]);

  var _handleSelRecChange = react_1.useCallback(function (e) {
    var el = e.currentTarget;

    if (el) {
      var pkgUuid = el.dataset.pkgUuid || '';
      var recUuid = el.dataset.recUuid || '';
      handleSelRecChange(pkgUuid, recUuid);
    }
  }, [handleSelRecChange]);

  var onDragStart = react_1.useCallback(function (e) {
    var clientX = e.clientX;
    setOriginClientX(clientX);
  }, []);
  var onDrag = react_1.useCallback(function (e) {
    var clientX = e.clientX;
    var x = clientX - originClientX;
    var curWidth = originWidth - x;
    setSearchWidth(Math.max(160, curWidth));
  }, [originWidth, originClientX]);
  var onDragEnd = react_1.useCallback(function () {
    setOriginWidth(searchWidth);
  }, [searchWidth]);
  react_1.useEffect(function () {
    setInnerDataSources(dataSources);
  }, [dataSources]);
  return jsx_runtime_1.jsx(jsx_runtime_1.Fragment, {
    children: moreThanOneRec && jsx_runtime_1.jsxs("div", __assign({
      className: prefixCls + "-list",
      style: style
    }, {
      children: [jsx_runtime_1.jsx(react_draggable_1["default"], __assign({
        axis: "x",
        onStart: onDragStart,
        onDrag: onDrag,
        onStop: onDragEnd,
        bounds: {
          right: 10
        }
      }, {
        children: jsx_runtime_1.jsx("div", {
          className: prefixCls + "-list-draggable"
        }, void 0)
      }), void 0), jsx_runtime_1.jsxs("div", __assign({
        className: prefixCls + "-list-cnt"
      }, {
        children: ["\u5171", totalDataLen, "\u6761\u6570\u636E"]
      }), void 0), jsx_runtime_1.jsx(input_1.Input, {
        prefixIcon: "search",
        placeholder: "\u641C\u7D22\u6570\u636E",
        size: "compact",
        onPressEnter: handleSearch,
        onChange: handleChange
      }, void 0), jsx_runtime_1.jsx(scrollbar_1["default"], {
        children: jsx_runtime_1.jsx("div", __assign({
          className: prefixCls + "-list-content"
        }, {
          children: _dataSources.map(function (ds) {
            var dsUuid = ds[ds.dataSourceUniqueKey || 'packageUuid'];
            return jsx_runtime_1.jsxs(react_1.Fragment, {
              children: [jsx_runtime_1.jsx("div", __assign({
                "data-pkg-uuid": dsUuid,
                className: prefixCls + "-list-content-item " + prefixCls + "-list-content-pkg-item",
                onClick: _handleSelPkgChange
              }, {
                children: jsx_runtime_1.jsx(tooltip_1["default"], __assign({
                  title: ds.objectType,
                  mouseEnterDelay: 0.5
                }, {
                  children: jsx_runtime_1.jsx("span", {
                    children: ds.objectType
                  }, void 0)
                }), void 0)
              }), void 0), dsUuid === selectDataSourceId && map_1["default"](ds.data, function (d) {
                var _a, _b;

                var firstColumnKey = (_b = (_a = ds.infoCfg) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.key;
                var name = d.name || (firstColumnKey ? d[firstColumnKey] : '');

                if (isObject_1["default"](name)) {
                  try {
                    name = JSON.stringify(name);
                  } catch (e) {}
                }

                var _id = typeof d[dataSourceDataUniqueKey] === 'number' ? d[dataSourceDataUniqueKey].toString() : d[dataSourceDataUniqueKey]; // eslint-disable-next-line @typescript-eslint/ban-ts-comment
                // @ts-ignore


                var _recId = typeof recId === 'number' ? recId.toString() : recId;

                return jsx_runtime_1.jsx(tooltip_1["default"], __assign({
                  title: name,
                  mouseEnterDelay: 0.5,
                  getTooltipContainer: getTooltipContainer
                }, {
                  children: jsx_runtime_1.jsxs("div", __assign({
                    "data-rec-uuid": d[dataSourceDataUniqueKey],
                    "data-pkg-uuid": dsUuid,
                    className: prefixCls + "-list-content-item " + prefixCls + "-list-content-rec-item",
                    onClick: _handleSelRecChange
                  }, {
                    children: [jsx_runtime_1.jsx("div", __assign({
                      className: prefixCls + "-list-content-rec-item-checked-icon"
                    }, {
                      children: _recId === _id && jsx_runtime_1.jsx(icon_1["default"], {
                        icon: "done-check",
                        size: 14
                      }, void 0)
                    }), void 0), ds.geometryType && jsx_runtime_1.jsx(icon_1["default"], {
                      icon: getIconName(ds.geometryType),
                      size: 16
                    }, void 0), jsx_runtime_1.jsx("div", __assign({
                      className: prefixCls + "-list-content-rec-item-name"
                    }, {
                      children: name
                    }), void 0)]
                  }), void 0)
                }), d[dataSourceDataUniqueKey]);
              })]
            }, dsUuid);
          })
        }), void 0)
      }, void 0)]
    }), void 0)
  }, void 0);
});

