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

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

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

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

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

var get_1 = __importDefault(require("lodash/get"));

var keys_1 = __importDefault(require("lodash/keys"));

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

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

var isEmpty_1 = __importDefault(require("lodash/isEmpty"));

var preview_list_1 = __importStar(require("../../preview-list"));

var dialogApi_1 = __importDefault(require("../../dialog/dialogApi"));

var scrollbar_1 = __importDefault(require("../../scrollbar")); // const alias = {
//   name: '名称',
//   address: '地址',
// };
// 为兼容旧版私有数据显示


var reverseAlias = {
  名称: 'name',
  地址: 'address'
};
var ImgList = react_1["default"].memo(function (_a) {
  var imgs = _a.imgs,
      prefixCls = _a.prefixCls,
      getPreviewPopupContainer = _a.getPreviewPopupContainer;
  var RenderCell = react_1.useCallback(function (props) {
    var _a;

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

    return jsx_runtime_1.jsx("div", {
      className: prefixCls + "-img-preview-item",
      style: {
        backgroundPosition: 'center',
        backgroundSize: 'contain',
        backgroundRepeat: 'no-repeat',
        backgroundImage: "url(" + ((_a = props === null || props === void 0 ? void 0 : props.item) === null || _a === void 0 ? void 0 : _a.url) + ")"
      }
    }, void 0);
  }, [prefixCls]);
  var handlePreview = react_1.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_1["default"].open({
      centered: true,
      closable: true,
      maskClosable: true,
      mask: true,
      wrapClassName: prefixCls + "-img-preview",
      getContainer: getPreviewPopupContainer
    }, function (onClose) {
      return jsx_runtime_1.jsx(preview_list_1["default"], {
        title: "\u56FE\u7247\u9884\u89C8",
        defaultSelectIndex: numericImgIndex,
        handleClose: onClose,
        showEdit: false,
        list: map_1["default"](imgs, function (i) {
          return {
            url: i
          };
        }),
        type: preview_list_1.PreviewType.normal,
        RenderCell: RenderCell,
        RenderPreview: RenderPreview
      }, void 0);
    });
  }, [RenderCell, RenderPreview, getPreviewPopupContainer, imgs, prefixCls]);
  return jsx_runtime_1.jsx(jsx_runtime_1.Fragment, {
    children: map_1["default"](imgs, function (img, index) {
      return jsx_runtime_1.jsx("div", __assign({
        "data-index": index,
        className: prefixCls + "-detail-body-img-list-item",
        style: {
          backgroundPosition: 'center',
          backgroundSize: 'contain',
          backgroundImage: "url(" + img + ")",
          backgroundRepeat: 'no-repeat'
        },
        onClick: handlePreview
      }, {
        children: jsx_runtime_1.jsx(icon_1["default"], {
          icon: "maximize",
          size: 40
        }, void 0)
      }), index + img);
    })
  }, void 0);
});
exports.ItemDetail = react_1["default"].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 reverseKey = get_1["default"](reverseAlias, key);
    var val = reverseKey && get_1["default"](record, reverseKey) || get_1["default"](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_1["default"](keys_1["default"](record), function (key) {
    return {
      key: key,
      val: get_1["default"](record, key)
    };
  });
  return jsx_runtime_1.jsxs("div", __assign({
    style: style,
    className: classnames_1["default"](prefixCls + "-detail", (_b = {}, _b[prefixCls + "-detail-multi-recs"] = hasMultiRecs, _b))
  }, {
    children: [jsx_runtime_1.jsxs("div", __assign({
      className: prefixCls + "-detail-header"
    }, {
      children: [jsx_runtime_1.jsx("div", __assign({
        className: prefixCls + "-detail-header-btns"
      }, {
        children: _renderHeaderTools ? _renderHeaderTools(dataSource) : map_1["default"](_headerTools, function (_a) {
          var id = _a.id,
              name = _a.name,
              action = _a.action;
          return jsx_runtime_1.jsx("div", __assign({
            className: prefixCls + "-detail-header-btn",
            onClick: action
          }, {
            children: jsx_runtime_1.jsx(icon_1["default"], {
              icon: name,
              size: 14
            }, void 0)
          }), id);
        })
      }), void 0), jsx_runtime_1.jsx("div", __assign({
        className: prefixCls + "-detail-header-close-btn",
        onClick: onClose
      }, {
        children: showClose && jsx_runtime_1.jsx(icon_1["default"], {
          icon: "close",
          size: 14
        }, void 0)
      }), void 0)]
    }), void 0), jsx_runtime_1.jsxs("div", __assign({
      className: prefixCls + "-detail-body"
    }, {
      children: [!isEmpty_1["default"](record.imgs) && jsx_runtime_1.jsx("div", __assign({
        className: prefixCls + "-detail-body-info-list-title"
      }, {
        children: "\u6570\u636E\u4FE1\u606F"
      }), void 0), jsx_runtime_1.jsx(scrollbar_1["default"], {
        children: jsx_runtime_1.jsx("div", __assign({
          className: prefixCls + "-detail-body-info-list"
        }, {
          children: map_1["default"](toRenderList, function (_a) {
            var key = _a.key,
                val = _a.val;
            return jsx_runtime_1.jsxs("div", __assign({
              className: prefixCls + "-detail-body-info-list-item"
            }, {
              children: [jsx_runtime_1.jsx("div", __assign({
                className: prefixCls + "-detail-body-info-list-item-key"
              }, {
                children: key
              }), void 0), jsx_runtime_1.jsx("div", __assign({
                className: prefixCls + "-detail-body-info-list-item-val"
              }, {
                children: val
              }), void 0)]
            }), key);
          })
        }), void 0)
      }, void 0), !isEmpty_1["default"](record.imgs) && jsx_runtime_1.jsxs(jsx_runtime_1.Fragment, {
        children: [jsx_runtime_1.jsx("div", __assign({
          className: prefixCls + "-detail-body-img-list-title"
        }, {
          children: "\u56FE\u7247\u4FE1\u606F"
        }), void 0), jsx_runtime_1.jsx("div", __assign({
          className: prefixCls + "-detail-body-img-list"
        }, {
          children: jsx_runtime_1.jsx(ImgList, {
            imgs: record.imgs,
            prefixCls: prefixCls,
            getPreviewPopupContainer: getPreviewPopupContainer
          }, void 0)
        }), void 0)]
      }, void 0)]
    }), void 0), _renderFooterTools || _footerTools.length ? jsx_runtime_1.jsx("div", __assign({
      className: prefixCls + "-detail-footer",
      style: toolStyle
    }, {
      children: _renderFooterTools ? _renderFooterTools(dataSource) : map_1["default"](_footerTools, function (_a, index) {
        var id = _a.id,
            name = _a.name,
            action = _a.action;
        return jsx_runtime_1.jsxs("div", __assign({
          onClick: action,
          className: prefixCls + "-detail-footer-btn"
        }, {
          children: [jsx_runtime_1.jsx("div", __assign({
            className: prefixCls + "-detail-footer-btn-name"
          }, {
            children: name
          }), void 0), _footerTools.length > 1 && index !== _footerTools.length - 1 && jsx_runtime_1.jsx("div", {
            className: prefixCls + "-detail-footer-btn-split-line"
          }, void 0)]
        }), id);
      })
    }), void 0) : null]
  }), void 0);
});

