function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
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(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useContext, useState, useRef, useEffect } from 'react';
import { BizGlobalDataContext, IconFont } from '@zgfe/business-lib';
import './index.less';
import { LinkCategoryLogo } from '../category';
import request from '../../utils/ajax';
import apis from '../../constants/api';
import { Select, notification } from 'antd';
import LinkDelConfirm from './delConfirm';
import { CdpContext } from '../../context';
var ListItem = function ListItem(props) {
  var data = props.data;
  var _useContext = useContext(BizGlobalDataContext),
    isDemo = _useContext.isDemo;
  function onEdit() {
    if (props.onEdit) props.onEdit(data);
  }
  function onDel(event) {
    event.stopPropagation();
    event.preventDefault();
    if (props.onDel) props.onDel(data);
  }
  var iconName = data.type.toLowerCase();
  /local|ftp/.test(iconName) && (iconName = 'excel');
  return /*#__PURE__*/React.createElement("div", {
    className: "cdp-link-list-item ".concat(props.active ? 'active' : ''),
    onClick: onEdit
  }, /*#__PURE__*/React.createElement("img", {
    src: LinkCategoryLogo[iconName],
    className: "cdp-link-list-logo"
  }), /*#__PURE__*/React.createElement("span", {
    className: "cdp-link-list-label"
  }, data.name), isDemo ? null : /*#__PURE__*/React.createElement(IconFont, {
    type: "shanchu",
    className: "cdp-link-list-del",
    onClick: onDel
  }));
};
var LinkListPanel = function LinkListPanel(props) {
  var list = props.list;
  var _useState = useState(),
    _useState2 = _slicedToArray(_useState, 2),
    toDel = _useState2[0],
    setToDel = _useState2[1];
  var _useState3 = useState(),
    _useState4 = _slicedToArray(_useState3, 2),
    showType = _useState4[0],
    setShowType = _useState4[1];
  var _useContext2 = useContext(CdpContext),
    categoryList = _useContext2.categoryList;
  var ref = useRef(null);
  var _useState5 = useState(0),
    _useState6 = _slicedToArray(_useState5, 2),
    divHeight = _useState6[0],
    setDivHeight = _useState6[1];
  useEffect(function () {
    setDivHeight(ref && ref.current && ref.current.clientHeight || 0);
  }, [showType]);
  function onDel(data) {
    setToDel(data);
  }
  function onConfirmDel(callback) {
    request(apis.delLink, {
      method: 'get',
      params: {
        id: toDel === null || toDel === void 0 ? void 0 : toDel.id
      }
    }).then(function (res) {
      if (!res) throw new Error();
      notification.success({
        message: '删除成功'
      });
      if (props.onDel) props.onDel(toDel === null || toDel === void 0 ? void 0 : toDel.id);
      setToDel(undefined);
    }).catch(function (err) {
      console.error('删除数据库连接失败', err);
      notification.error({
        message: '删除连接失败'
      });
    }).finally(function () {
      callback();
    });
  }
  function onEdit(data) {
    if (props.onEdit) props.onEdit(data);
  }
  function onChangeType(type) {
    setShowType(type);
  }
  var showList = list.filter(function (item) {
    if (showType !== 'all' && !new RegExp(showType, 'i').test(item.type === 'local' || item.type === 'ftp' ? 'excel' : item.type)) return false;
    return true;
  }).map(function (item) {
    var _props$current;
    return /*#__PURE__*/React.createElement(ListItem, {
      onDel: onDel,
      onEdit: onEdit,
      active: ((_props$current = props.current) === null || _props$current === void 0 ? void 0 : _props$current.id) === item.id,
      key: item.id || item.name,
      data: item
    });
  });
  return /*#__PURE__*/React.createElement("div", {
    className: "cdp-link-list"
  }, Boolean(toDel) && /*#__PURE__*/React.createElement(LinkDelConfirm, {
    linkId: toDel === null || toDel === void 0 ? void 0 : toDel.id,
    onCancel: function onCancel() {
      setToDel(undefined);
    },
    onOk: onConfirmDel
  }), /*#__PURE__*/React.createElement("div", null, "\u6570\u636E\u96C6", /*#__PURE__*/React.createElement(Select, {
    style: {
      marginLeft: 12,
      width: 286
    },
    options: [{
      label: '全部',
      name: 'all'
    }].concat(_toConsumableArray(categoryList || [])),
    fieldNames: {
      label: 'label',
      value: 'name'
    },
    placeholder: "\u8BF7\u9009\u62E9\u7C7B\u578B",
    defaultValue: "all",
    onChange: onChangeType
  })), /*#__PURE__*/React.createElement("div", {
    className: "cdp-link-list-panel",
    ref: ref
  }, /*#__PURE__*/React.createElement("div", {
    style: {
      marginTop: -16,
      flexDirection: 'row',
      flexWrap: 'wrap',
      display: 'flex'
    }
  }, showList.length ? showList : /*#__PURE__*/React.createElement("span", {
    className: "cdp-link-list-empty"
  }, "\u6682\u65E0\u8FDE\u63A5")), divHeight == 128 && /*#__PURE__*/React.createElement("div", {
    className: "cdp-link-list-panel-white"
  })), divHeight == 128 && /*#__PURE__*/React.createElement("div", {
    className: "cdp-link-list-panel-bg"
  }));
};
export default LinkListPanel;