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, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import React, { useCallback } from 'react';
import "../style/item-detail.css";
import Icon from "../../icon";
import get from 'lodash/get';
import keys from 'lodash/keys';
import map from 'lodash/map';
import classNames from 'classnames';
import isEmpty from 'lodash/isEmpty';
import PreviewList, { PreviewType } from "../../preview-list";
import dialogApi from "../../dialog/dialogApi";
import Scrollbar from "../../scrollbar"; // const alias = {
//   name: '名称',
//   address: '地址',
// };
// 为兼容旧版私有数据显示

var reverseAlias = {
  名称: 'name',
  地址: 'address'
};
var ImgList = /*#__PURE__*/React.memo(function (_a) {
  var imgs = _a.imgs,
      prefixCls = _a.prefixCls,
      getPreviewPopupContainer = _a.getPreviewPopupContainer;
  var RenderCell = useCallback(function (props) {
    var _a;

    return _jsx("div", {
      className: prefixCls + "-img-preview-list-item",
      style: {
        background: "url(" + ((_a = props === null || props === void 0 ? void 0 : props.item) === null || _a === void 0 ? void 0 : _a.url) + ") no-repeat center",
        backgroundSize: 'contain'
      }
    }, void 0);
  }, [prefixCls]);
  var RenderPreview = useCallback(function (props) {
    var _a;

    return _jsx("div", {
      className: prefixCls + "-img-preview-item",
      style: {
        background: "url(" + ((_a = props === null || props === void 0 ? void 0 : props.item) === null || _a === void 0 ? void 0 : _a.url) + ") no-repeat center",
        backgroundSize: 'contain'
      }
    }, void 0);
  }, [prefixCls]);
  var handlePreview = useCallback(function (e) {
    var _a;

    var imgIndex = (_a = e.currentTarget) === null || _a === void 0 ? void 0 : _a.dataset.index;
    var v = Number.parseInt(imgIndex || '0', 10);
    var numericImgIndex = isNaN(v) ? 0 : v;
    return dialogApi.open({
      centered: true,
      closable: true,
      maskClosable: true,
      mask: true,
      wrapClassName: prefixCls + "-img-preview",
      getContainer: getPreviewPopupContainer
    }, function (onClose) {
      return _jsx(PreviewList, {
        title: "\u56FE\u7247\u9884\u89C8",
        defaultSelectIndex: numericImgIndex,
        handleClose: onClose,
        showEdit: false,
        list: map(imgs, function (i) {
          return {
            url: i
          };
        }),
        type: PreviewType.normal,
        RenderCell: RenderCell,
        RenderPreview: RenderPreview
      }, void 0);
    });
  }, [RenderCell, RenderPreview, getPreviewPopupContainer, imgs, prefixCls]);
  return _jsx(_Fragment, {
    children: map(imgs, function (img, index) {
      return _jsx("div", __assign({
        "data-index": index,
        className: prefixCls + "-detail-body-img-list-item",
        style: {
          backgroundImage: "url(" + img + ")"
        },
        onClick: handlePreview
      }, {
        children: _jsx(Icon, {
          icon: "maximize",
          size: 40
        }, void 0)
      }), index + img);
    })
  }, void 0);
});
export var ItemDetail = /*#__PURE__*/React.memo(function (_a) {
  var _b;

  var prefixCls = _a.prefixCls,
      record = _a.record,
      dataSource = _a.dataSource,
      headerTools = _a.headerTools,
      renderHeaderTools = _a.renderHeaderTools,
      footerTools = _a.footerTools,
      renderFooterTools = _a.renderFooterTools,
      showClose = _a.showClose,
      onClose = _a.onClose,
      hasMultiRecs = _a.hasMultiRecs,
      getPreviewPopupContainer = _a.getPreviewPopupContainer;

  var _footerTools = (dataSource === null || dataSource === void 0 ? void 0 : dataSource.footerTools) || footerTools || [];

  var _headerTools = (dataSource === null || dataSource === void 0 ? void 0 : dataSource.headerTools) || headerTools || [];

  var _renderHeaderTools = (dataSource === null || dataSource === void 0 ? void 0 : dataSource.renderHeaderTools) || renderHeaderTools;

  var _renderFooterTools = (dataSource === null || dataSource === void 0 ? void 0 : dataSource.renderFooterTools) || renderFooterTools;

  var _c = dataSource || {},
      infoCfg = _c.infoCfg,
      _d = _c.infoWindowSettings,
      infoWindowSettings = _d === void 0 ? {} : _d;

  var _e = infoWindowSettings.decimalSetting,
      decimalSetting = _e === void 0 ? -1 : _e;
  var style = hasMultiRecs ? {
    minHeight: '380px'
  } : undefined;
  var toolStyle = {
    gridTemplateColumns: "repeat(" + _footerTools.length + ", 1fr)"
  };
  var toRenderList = infoCfg ? infoCfg.map(function (_a) {
    var key = _a.key,
        _b = _a.type,
        type = _b === void 0 ? '' : _b;
    var val = key in reverseAlias ? get(record, get(reverseAlias, key)) : get(record, key);

    if (type === 'number' && decimalSetting >= 0) {
      var numericVal = Number.parseFloat(val);
      val = isNaN(numericVal) ? '' : decimalSetting === 0 ? Math.round(numericVal) : numericVal.toFixed(decimalSetting);
    }

    return {
      key: key,
      val: val
    };
  }) : map(keys(record), function (key) {
    return {
      key: key,
      val: get(record, key)
    };
  });
  return _jsxs("div", __assign({
    style: style,
    className: classNames(prefixCls + "-detail", (_b = {}, _b[prefixCls + "-detail-multi-recs"] = hasMultiRecs, _b))
  }, {
    children: [_jsxs("div", __assign({
      className: prefixCls + "-detail-header"
    }, {
      children: [_jsx("div", __assign({
        className: prefixCls + "-detail-header-btns"
      }, {
        children: _renderHeaderTools ? _renderHeaderTools(dataSource) : map(_headerTools, function (_a) {
          var id = _a.id,
              name = _a.name,
              action = _a.action;
          return _jsx("div", __assign({
            className: prefixCls + "-detail-header-btn",
            onClick: action
          }, {
            children: _jsx(Icon, {
              icon: name,
              size: 14
            }, void 0)
          }), id);
        })
      }), void 0), _jsx("div", __assign({
        className: prefixCls + "-detail-header-close-btn",
        onClick: onClose
      }, {
        children: showClose && _jsx(Icon, {
          icon: "close",
          size: 14
        }, void 0)
      }), void 0)]
    }), void 0), _jsxs("div", __assign({
      className: prefixCls + "-detail-body"
    }, {
      children: [!isEmpty(record.imgs) && _jsx("div", __assign({
        className: prefixCls + "-detail-body-info-list-title"
      }, {
        children: "\u6570\u636E\u4FE1\u606F"
      }), void 0), _jsx(Scrollbar, {
        children: _jsx("div", __assign({
          className: prefixCls + "-detail-body-info-list"
        }, {
          children: map(toRenderList, function (_a) {
            var key = _a.key,
                val = _a.val;
            return _jsxs("div", __assign({
              className: prefixCls + "-detail-body-info-list-item"
            }, {
              children: [_jsx("div", __assign({
                className: prefixCls + "-detail-body-info-list-item-key"
              }, {
                children: key
              }), void 0), _jsx("div", __assign({
                className: prefixCls + "-detail-body-info-list-item-val"
              }, {
                children: val
              }), void 0)]
            }), key);
          })
        }), void 0)
      }, void 0), !isEmpty(record.imgs) && _jsxs(_Fragment, {
        children: [_jsx("div", __assign({
          className: prefixCls + "-detail-body-img-list-title"
        }, {
          children: "\u56FE\u7247\u4FE1\u606F"
        }), void 0), _jsx("div", __assign({
          className: prefixCls + "-detail-body-img-list"
        }, {
          children: _jsx(ImgList, {
            imgs: record.imgs,
            prefixCls: prefixCls,
            getPreviewPopupContainer: getPreviewPopupContainer
          }, void 0)
        }), void 0)]
      }, void 0)]
    }), void 0), _renderFooterTools || _footerTools.length ? _jsx("div", __assign({
      className: prefixCls + "-detail-footer",
      style: toolStyle
    }, {
      children: _renderFooterTools ? _renderFooterTools(dataSource) : map(_footerTools, function (_a, index) {
        var id = _a.id,
            name = _a.name,
            action = _a.action;
        return _jsxs("div", __assign({
          onClick: action,
          className: prefixCls + "-detail-footer-btn"
        }, {
          children: [_jsx("div", __assign({
            className: prefixCls + "-detail-footer-btn-name"
          }, {
            children: name
          }), void 0), _footerTools.length > 1 && index !== _footerTools.length - 1 && _jsx("div", {
            className: prefixCls + "-detail-footer-btn-split-line"
          }, void 0)]
        }), id);
      })
    }), void 0) : null]
  }), void 0);
});

