var __assign = this && this.__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);
};

import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import * as React from 'react';
import classNames from 'classnames';
import { v4 as uuidV4 } from 'uuid';
import "./style/map-info-window.css";
import { useCallback, useEffect, useMemo, useState } from 'react';
import { ItemList } from "./components/item-list";
import { ItemDetail } from "./components/item-detail";
import map from 'lodash/map';
import find from 'lodash/find';
import every from 'lodash/every';
import toastApi from "../toast";
var prefixCls = 'dmc-map-info-window';

var convert2Str = function convert2Str(v) {
  if (typeof v === 'number') {
    return v.toString();
  }

  return v;
};

var MapInfoWindow = function MapInfoWindow(props) {
  var _a, _b;

  var className = props.className,
      onClose = props.onClose,
      dataSources = props.dataSources,
      dataSourceDataUniqueKey = props.dataSourceDataUniqueKey,
      handleShowIndexChange = props.handleShowIndexChange,
      _c = props.showClose,
      showClose = _c === void 0 ? true : _c,
      headerTools = props.headerTools,
      renderHeaderTools = props.renderHeaderTools,
      footerTools = props.footerTools,
      renderFooterTools = props.renderFooterTools,
      style = props.style,
      getPreviewPopupContainer = props.getPreviewPopupContainer;

  var _dataSourceDataUniqueKey = dataSourceDataUniqueKey || 'id';

  var _d = useState(dataSources || []),
      _dataSources = _d[0],
      setInnerDataSources = _d[1]; // 左侧列表选中数据包（ds）uuid


  var _e = useState(''),
      dsId = _e[0],
      setDsId = _e[1]; // 左侧列表选中记录 (ds > data) uuid


  var _f = useState(''),
      recId = _f[0],
      setRecId = _f[1];

  var _g = useState(_dataSources[0]),
      curDataSource = _g[0],
      setCurDataSource = _g[1];

  var _h = useState((_b = (_a = _dataSources[0]) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b[0]),
      curRecord = _h[0],
      setCurRecord = _h[1];

  var hasMultiRecs = useMemo(function () {
    var _a;

    return _dataSources.length > 1 || ((_a = _dataSources[0]) === null || _a === void 0 ? void 0 : _a.data.length) > 1;
  }, [_dataSources]);
  var handleSelPkgChange = useCallback(function (id) {
    var _a;

    var curDs = find(_dataSources, function (i) {
      return i.packageUuid === id;
    }) || {};
    setDsId(id);
    setCurDataSource(curDs);

    if (!find(curDs === null || curDs === void 0 ? void 0 : curDs.data, function (i) {
      return convert2Str(i[_dataSourceDataUniqueKey]) === convert2Str(recId);
    })) {
      var rec = ((_a = curDs === null || curDs === void 0 ? void 0 : curDs.data) === null || _a === void 0 ? void 0 : _a[0]) || {};
      setRecId(rec[_dataSourceDataUniqueKey]);
      setCurRecord(rec);
    }
  }, [_dataSourceDataUniqueKey, _dataSources, recId]);
  var handleSelRecChange = useCallback(function (pkgId, recId) {
    var curDs = find(_dataSources, function (i) {
      return i.packageUuid === dsId;
    }) || {};
    var _a = curDs.data,
        data = _a === void 0 ? [] : _a;
    setDsId(pkgId);
    setRecId(recId);
    setCurDataSource(curDs);
    setCurRecord(find(data, function (i) {
      return convert2Str(i[_dataSourceDataUniqueKey]) === convert2Str(recId);
    }) || {});
  }, [_dataSourceDataUniqueKey, _dataSources, dsId]);
  useEffect(function () {
    var _a, _b, _c, _d; // validation


    if (!every(dataSources, function (ds) {
      return ds.packageUuid && Array.isArray(ds.data);
    })) {
      toastApi.error('MapInfoWindow DataSource 参数错误');
      throw new Error('MapInfoWindow DataSource 参数错误');
    }

    if (!dataSourceDataUniqueKey && !every(dataSources, function (ds) {
      return every(ds.data, function (d) {
        return d[_dataSourceDataUniqueKey];
      });
    })) {
      toastApi.error('MapInfoWindow DataSource 内部参数 data 错误 - 没有唯一标识符');
      throw new Error('MapInfoWindow DataSource 内部参数 data 错误 - 未通过 dataSourceDataUniqueKey 设置唯一标识符');
    } // convert


    var ds = map(dataSources, function (ds) {
      return __assign(__assign({}, ds), {
        data: map(ds.data, function (i) {
          var _a;

          return __assign(__assign({}, i), (_a = {}, _a[_dataSourceDataUniqueKey] = i[_dataSourceDataUniqueKey] || uuidV4(), _a));
        }) || []
      });
    });
    setInnerDataSources(ds);
    setCurDataSource(ds === null || ds === void 0 ? void 0 : ds[0]);
    setCurRecord(((_a = ds === null || ds === void 0 ? void 0 : ds[0]) === null || _a === void 0 ? void 0 : _a.data[0]) || {});
    setDsId((_b = ds === null || ds === void 0 ? void 0 : ds[0]) === null || _b === void 0 ? void 0 : _b.packageUuid);
    setRecId((_d = (_c = ds === null || ds === void 0 ? void 0 : ds[0]) === null || _c === void 0 ? void 0 : _c.data[0]) === null || _d === void 0 ? void 0 : _d[_dataSourceDataUniqueKey]);
  }, [_dataSourceDataUniqueKey, dataSourceDataUniqueKey, dataSources]);
  useEffect(function () {
    if (recId && dsId) {
      handleShowIndexChange === null || handleShowIndexChange === void 0 ? void 0 : handleShowIndexChange({
        packageUuid: dsId,
        recordUuid: recId
      });
    }
  }, [handleShowIndexChange, recId, dsId]);
  return _jsx(_Fragment, {
    children: curDataSource && curRecord && _jsxs("div", __assign({
      className: classNames(prefixCls, className),
      style: style
    }, {
      children: [hasMultiRecs && _jsx(ItemList, {
        recId: recId,
        prefixCls: prefixCls,
        dataSources: _dataSources,
        selectDataSourceId: dsId,
        handleSelPkgChange: handleSelPkgChange,
        handleSelRecChange: handleSelRecChange,
        dataSourceDataUniqueKey: _dataSourceDataUniqueKey
      }, void 0), _jsx(ItemDetail, {
        prefixCls: prefixCls,
        record: curRecord,
        dataSource: curDataSource,
        showClose: showClose,
        onClose: onClose,
        headerTools: headerTools,
        renderHeaderTools: renderHeaderTools,
        footerTools: footerTools,
        renderFooterTools: renderFooterTools,
        hasMultiRecs: hasMultiRecs,
        getPreviewPopupContainer: getPreviewPopupContainer
      }, void 0), _jsx("div", __assign({
        className: prefixCls + "-triangle-border"
      }, {
        children: _jsx("div", {
          className: prefixCls + "-triangle"
        }, void 0)
      }), void 0)]
    }), void 0)
  }, void 0);
};

MapInfoWindow.displayName = 'MapInfoWindow';
export default MapInfoWindow;
var MapInfoWindowMemo = /*#__PURE__*/React.memo(MapInfoWindow);
MapInfoWindowMemo.displayName = 'MapInfoWindowMemo';
export { MapInfoWindowMemo };

