function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { Button, Popover, Table } from 'antd';
import React, { useEffect, useMemo, useState } from 'react';
import Icon from "../Icon";
import { mergeMetadataTexts } from "../metadata/locale";
import { fetchMetadataObjFormat } from "./fetchArray";
import "./index.less";
var MetadataPopoverList = function MetadataPopoverList(_ref) {
  var type = _ref.type,
    kind = _ref.kind,
    datasource = _ref.datasource,
    placement = _ref.placement,
    textsProp = _ref.texts;
  var t = useMemo(function () {
    return mergeMetadataTexts(textsProp);
  }, [textsProp]);
  var _useState = useState([]),
    _useState2 = _slicedToArray(_useState, 2),
    dataList = _useState2[0],
    setDataList = _useState2[1];
  var columns = useMemo(function () {
    var base = [{
      title: t.key,
      dataIndex: 'key',
      key: 'key',
      render: function render(text) {
        return text || '--';
      },
      width: '40%'
    }, {
      title: t.value,
      dataIndex: 'value',
      key: 'value',
      render: function render(text) {
        return text || '--';
      },
      width: '40%'
    }];
    if (type === 'taints') {
      base.push({
        title: t.taintEffect,
        dataIndex: 'effect',
        key: 'effect',
        render: function render(text) {
          return text || '--';
        },
        width: '20%'
      });
    }
    return base;
  }, [t, type]);
  useEffect(function () {
    setDataList(datasource ? fetchMetadataObjFormat(datasource, type) : []);
  }, [datasource, type]);
  var popoverContent = /*#__PURE__*/React.createElement(Table, {
    columns: columns,
    dataSource: dataList,
    rowKey: function rowKey(record) {
      return record === null || record === void 0 ? void 0 : record.key;
    },
    pagination: false,
    scroll: {
      y: 250
    }
  });
  var trigger = kind === 'count' ? /*#__PURE__*/React.createElement(Button, {
    type: "text",
    size: "small",
    className: "hc-metadata-count",
    onClick: function onClick(e) {
      return e.stopPropagation();
    }
  }, /*#__PURE__*/React.createElement("span", null, dataList.length)) : kind === 'more' ? /*#__PURE__*/React.createElement("div", {
    className: "hc-metadata-more",
    onClick: function onClick(e) {
      return e.stopPropagation();
    }
  }, /*#__PURE__*/React.createElement(Icon, {
    name: "v35_MoreHorizontal"
  })) : '--';
  return /*#__PURE__*/React.createElement(Popover, {
    content: popoverContent,
    trigger: "click",
    overlayClassName: "metadata-table-popover",
    styles: {
      root: {
        width: dataList.length === 0 ? 200 : 'auto'
      }
    },
    placement: placement || 'bottom'
  }, trigger);
};
export default MetadataPopoverList;