"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _react = _interopRequireWildcard(require("react"));

var _icons = require("@ant-design/icons");

var _classnames = _interopRequireDefault(require("classnames"));

var _antd = require("antd");

var _indexModule = _interopRequireDefault(require("./index.module.less"));

var _UIApiContext = _interopRequireDefault(require("../UIApiContext"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var _default = function _default(props) {
  var type = props.type,
      matchedResources = props.matchedResources,
      current = props.current,
      setActiveResource = props.setActiveResource,
      updateUrlQuery = props.updateUrlQuery,
      blocks = props.blocks,
      selectedTag = props.selectedTag,
      setSelectedTag = props.setSelectedTag,
      loading = props.loading;

  var _useContext = (0, _react.useContext)(_UIApiContext.default),
      api = _useContext.api;

  var _api$_ = api._,
      uniq = _api$_.uniq,
      flatten = _api$_.flatten;
  var tags = (0, _react.useMemo)(function () {
    return uniq(flatten(blocks.map(function (item) {
      return item.category ? [item.category] : item.tags;
    })));
  }, [blocks]); // const sortTagMap = {
  //   表格: 10,
  //   表单: 9,
  //   通用: 8,
  //   列表: 7,
  //   用户: 6,
  //   数据录入: 5,
  //   数据展示: 4,
  //   基本: 3,
  //   高级: 2,
  // };
  //
  // const sortTag = (a, b) => {
  //   if (sortTagMap[a]) {
  //     return -sortTagMap[a];
  //   }
  //   if (sortTagMap[b]) {
  //     return -sortTagMap[b];
  //   }
  //   return 0;
  // };

  function renderCats() {
    if (loading) {
      return /*#__PURE__*/_react.default.createElement("div", {
        className: "".concat(_indexModule.default.cats, " ").concat(loading ? _indexModule.default.catsLoading : '')
      }, /*#__PURE__*/_react.default.createElement(_antd.Spin, {
        size: "large"
      }));
    }

    var getTagCls = function getTagCls(selected) {
      return (0, _classnames.default)(_indexModule.default.cat, _defineProperty({}, _indexModule.default.current, selectedTag === selected));
    };

    return /*#__PURE__*/_react.default.createElement("div", {
      className: _indexModule.default.cats
    }, /*#__PURE__*/_react.default.createElement("div", {
      key: "\u5168\u90E8",
      className: getTagCls(''),
      onClick: function onClick() {
        return setSelectedTag('');
      }
    }, "\u5168\u90E8"), tags // .sort(sortTag)
    .filter(function (tag) {
      return tag !== '废弃';
    }).map(function (tag) {
      return /*#__PURE__*/_react.default.createElement("div", {
        key: tag,
        className: getTagCls(tag),
        onClick: function onClick() {
          return setSelectedTag(tag);
        }
      }, tag);
    }));
  }

  function renderResources() {
    function resourceSwitchHandler(r) {
      if (r.id === current.id) {
        setActiveResource({
          id: null
        });
        return;
      }

      setActiveResource(r);
      updateUrlQuery({
        type: type,
        resource: r.id
      });
    }

    return matchedResources.map(function (r) {
      var isCurrent = current.id === r.id;
      var resourceCls = (0, _classnames.default)(_indexModule.default.resource, _defineProperty({}, _indexModule.default.current, !!isCurrent));
      return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, {
        key: r.id
      }, /*#__PURE__*/_react.default.createElement("div", {
        className: resourceCls,
        onClick: resourceSwitchHandler.bind(null, r)
      }, /*#__PURE__*/_react.default.createElement("div", {
        className: _indexModule.default.icon
      }, /*#__PURE__*/_react.default.createElement("img", {
        src: r.icon,
        style: {
          width: '32px',
          height: '32px'
        }
      })), /*#__PURE__*/_react.default.createElement("div", {
        className: _indexModule.default.titleAndDescription
      }, /*#__PURE__*/_react.default.createElement("div", {
        className: _indexModule.default.title
      }, r.name), /*#__PURE__*/_react.default.createElement("div", {
        className: _indexModule.default.description
      }, r.description)), /*#__PURE__*/_react.default.createElement("div", {
        className: _indexModule.default.switcher
      }, isCurrent ? /*#__PURE__*/_react.default.createElement(_icons.UpOutlined, {
        style: {
          fontSize: '12px'
        }
      }) : /*#__PURE__*/_react.default.createElement(_icons.DownOutlined, {
        style: {
          fontSize: '12px'
        }
      }))), isCurrent ? renderCats() : null);
    });
  }

  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, renderResources());
};

exports.default = _default;