"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.MapInfoWindowMemo = void 0;

var jsx_runtime_1 = require("react/jsx-runtime");

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

var classnames_1 = __importDefault(require("classnames"));

require("./style/map-info-window.css");

var react_1 = require("react");

var item_list_1 = require("./components/item-list");

var item_detail_1 = require("./components/item-detail");

var find_1 = __importDefault(require("lodash/find"));

var every_1 = __importDefault(require("lodash/every"));

var toast_1 = __importDefault(require("../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,
      dataSourceRecordUniqueKey = props.dataSourceRecordUniqueKey,
      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 = dataSourceRecordUniqueKey || 'id'; // 左侧列表选中数据包（ds）uuid


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


  var _e = react_1.useState(''),
      recId = _e[0],
      setRecId = _e[1];

  var _f = react_1.useState(dataSources[0]),
      curDataSource = _f[0],
      setCurDataSource = _f[1];

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

  var hasMultiRecs = react_1.useMemo(function () {
    var _a, _b;

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

    var curDs = find_1["default"](dataSources, function (i) {
      var _dataSourceUniqueKey = i.dataSourceUniqueKey || 'packageUuid';

      return i[_dataSourceUniqueKey] === id;
    }) || {};
    setDsId(id);
    setCurDataSource(curDs);

    if (!find_1["default"](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 = react_1.useCallback(function (pkgId, recId) {
    var curDs = find_1["default"](dataSources, function (i) {
      var _dataSourceUniqueKey = i.dataSourceUniqueKey || 'packageUuid';

      return i[_dataSourceUniqueKey] === dsId;
    }) || {};
    var _a = curDs.data,
        data = _a === void 0 ? [] : _a;
    setDsId(pkgId);
    setRecId(recId);
    setCurDataSource(curDs);
    setCurRecord(find_1["default"](data, function (i) {
      return convert2Str(i[_dataSourceDataUniqueKey]) === convert2Str(recId);
    }) || {});
  }, [_dataSourceDataUniqueKey, dataSources, dsId]);
  react_1.useEffect(function () {
    var _a; // validation


    if (!every_1["default"](dataSources, function (ds) {
      var _dataSourceUniqueKey = ds.dataSourceUniqueKey || 'packageUuid';

      return ds[_dataSourceUniqueKey] && Array.isArray(ds.data);
    })) {
      toast_1["default"].error('MapInfoWindow DataSource 参数错误');
      throw new Error('MapInfoWindow DataSource 参数错误');
    }

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

    var firstDs = dataSources[0];
    var curRecord = ((_a = firstDs === null || firstDs === void 0 ? void 0 : firstDs.data) === null || _a === void 0 ? void 0 : _a[0]) || {};
    var curDsId = firstDs === null || firstDs === void 0 ? void 0 : firstDs[firstDs.dataSourceUniqueKey || 'packageUuid'];
    setCurDataSource(firstDs);
    setCurRecord(curRecord);
    setDsId(curDsId);
    setRecId(curRecord[_dataSourceDataUniqueKey]);
  }, [_dataSourceDataUniqueKey, dataSourceRecordUniqueKey, dataSources]);
  react_1.useEffect(function () {
    if (recId && dsId) {
      handleShowIndexChange === null || handleShowIndexChange === void 0 ? void 0 : handleShowIndexChange({
        packageUuid: dsId,
        recordUuid: recId
      });
    }
  }, [handleShowIndexChange, recId, dsId]);
  return jsx_runtime_1.jsx(jsx_runtime_1.Fragment, {
    children: curDataSource && curRecord && jsx_runtime_1.jsxs("div", __assign({
      className: classnames_1["default"](prefixCls, className),
      style: style
    }, {
      children: [hasMultiRecs && jsx_runtime_1.jsx(item_list_1.ItemList, {
        recId: recId,
        prefixCls: prefixCls,
        dataSources: dataSources,
        selectDataSourceId: dsId,
        handleSelPkgChange: handleSelPkgChange,
        handleSelRecChange: handleSelRecChange,
        dataSourceDataUniqueKey: _dataSourceDataUniqueKey
      }, void 0), jsx_runtime_1.jsx(item_detail_1.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_runtime_1.jsx("div", __assign({
        className: prefixCls + "-triangle-border"
      }, {
        children: jsx_runtime_1.jsx("div", {
          className: prefixCls + "-triangle"
        }, void 0)
      }), void 0)]
    }), void 0)
  }, void 0);
};

MapInfoWindow.displayName = 'MapInfoWindow';
exports["default"] = MapInfoWindow;
var MapInfoWindowMemo = React.memo(MapInfoWindow);
exports.MapInfoWindowMemo = MapInfoWindowMemo;
MapInfoWindowMemo.displayName = 'MapInfoWindowMemo';

