"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 = exports.getPathFromFilename = exports.Meats = void 0;

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

var _antd = require("antd");

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

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

var _HighlightedText = _interopRequireDefault(require("./HighlightedText"));

var _getInsertPosition = _interopRequireDefault(require("./getInsertPosition"));

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

var _data = require("../../../data.d");

var _ImageLoad = _interopRequireDefault(require("./ImageLoad"));

var _ImagePreview = _interopRequireDefault(require("./ImagePreview"));

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 _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

/**
 * 子区块 的 tag
 * @param
 */
var Meats = function Meats(_ref) {
  var item = _ref.item,
      keyword = _ref.keyword;
  return /*#__PURE__*/_react.default.createElement("div", {
    className: _indexModule.default.meats
  }, /*#__PURE__*/_react.default.createElement("span", {
    className: _indexModule.default.tags
  }, item.tags && item.tags.map(function (tag) {
    return /*#__PURE__*/_react.default.createElement("span", {
      key: tag,
      className: _indexModule.default.tagInCard
    }, /*#__PURE__*/_react.default.createElement(_HighlightedText.default, {
      text: tag,
      highlight: keyword
    }));
  })));
};

exports.Meats = Meats;

/**
 * 将绝对路径转化为相对路径
 * @param api
 * @param filenamePath
 */
var getPathFromFilename = /*#__PURE__*/function () {
  var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(api, filenamePath) {
    var _ref3, data;

    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _context.next = 2;
            return api.callRemote({
              type: 'org.umi.block.getRelativePagesPath',
              payload: {
                path: filenamePath
              }
            });

          case 2:
            _ref3 = _context.sent;
            data = _ref3.data;
            return _context.abrupt("return", data.replace(/(index)?((\.js?)|(\.tsx?)|(\.jsx?))$/, ''));

          case 5:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));

  return function getPathFromFilename(_x, _x2) {
    return _ref2.apply(this, arguments);
  };
}();
/**
 * 打开 modal 框之前的一些处理
 * @param api  umi ui 的 api 全局对象
 * @param param 参数
 */


exports.getPathFromFilename = getPathFromFilename;

var onBeforeOpenModal = /*#__PURE__*/function () {
  var _ref5 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(api, _ref4) {
    var item, type, onShowModal, position, targetPath, _option;

    return regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            item = _ref4.item, type = _ref4.type, onShowModal = _ref4.onShowModal;
            _context2.prev = 1;
            _context2.next = 4;
            return api.callRemote({
              type: 'org.umi.block.checkIfCanAdd',
              payload: {
                item: item,
                type: type
              }
            });

          case 4:
            _context2.next = 10;
            break;

          case 6:
            _context2.prev = 6;
            _context2.t0 = _context2["catch"](1);

            _antd.message.error(_context2.t0.message);

            return _context2.abrupt("return");

          case 10:
            if (!(api.isMini() && type === 'block')) {
              _context2.next = 20;
              break;
            }

            _context2.next = 13;
            return (0, _getInsertPosition.default)(api).catch(function (e) {
              _antd.message.error(e.message);
            });

          case 13:
            position = _context2.sent;
            _context2.next = 16;
            return getPathFromFilename(api, position.filename);

          case 16:
            targetPath = _context2.sent;
            _option = {
              path: targetPath,
              index: position.index,
              blockTarget: targetPath
            };
            onShowModal(item, _option);
            return _context2.abrupt("return");

          case 20:
            onShowModal(item, {});

          case 21:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2, null, [[1, 6]]);
  }));

  return function onBeforeOpenModal(_x3, _x4) {
    return _ref5.apply(this, arguments);
  };
}();

var ToolTipAddButton = function ToolTipAddButton(_ref6) {
  var disabledTitle = _ref6.disabledTitle,
      disabled = _ref6.disabled,
      children = _ref6.children,
      reset = _objectWithoutProperties(_ref6, ["disabledTitle", "disabled", "children"]);

  if (disabled) {
    return /*#__PURE__*/_react.default.createElement(_antd.Tooltip, {
      title: disabledTitle
    }, /*#__PURE__*/_react.default.createElement(_antd.Button, _extends({
      className: _indexModule.default.addBtn,
      type: "primary",
      disabled: disabled
    }, reset), children));
  }

  return /*#__PURE__*/_react.default.createElement(_antd.Button, _extends({
    className: _indexModule.default.addBtn,
    type: "primary",
    disabled: disabled
  }, reset), children);
};

var BlockItem = function BlockItem(_ref7) {
  var type = _ref7.type,
      item = _ref7.item,
      _ref7$loading = _ref7.loading,
      loading = _ref7$loading === void 0 ? false : _ref7$loading,
      disabled = _ref7.disabled,
      onShowModal = _ref7.onShowModal,
      keyword = _ref7.keyword,
      selectedTag = _ref7.selectedTag;

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

  var intl = api.intl;
  var isMini = api.isMini();
  return /*#__PURE__*/_react.default.createElement(_antd.Col, {
    className: _indexModule.default.col,
    key: item.url
  }, /*#__PURE__*/_react.default.createElement("div", {
    id: item.url,
    className: _indexModule.default.templateCard,
    onClick: function onClick() {
      if (loading) {
        onShowModal(item, {});
      }
    }
  }, /*#__PURE__*/_react.default.createElement(_antd.Spin, {
    spinning: loading,
    tip: "\u6DFB\u52A0\u4E2D..."
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: _indexModule.default.demo
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: _indexModule.default.addProject
  }, /*#__PURE__*/_react.default.createElement(ToolTipAddButton, {
    type: "primary",
    disabled: disabled,
    disabledTitle: intl({
      id: 'org.umi.ui.blocks.adder.disabledTitle'
    }),
    onClick: function onClick() {
      return onBeforeOpenModal(api, {
        type: type,
        item: item,
        onShowModal: onShowModal
      });
    }
  }, loading ? intl({
    id: 'org.umi.ui.blocks.list.viewlog'
  }) : intl({
    id: 'org.umi.ui.blocks.list.add'
  })), /*#__PURE__*/_react.default.createElement("div", {
    className: "".concat(_indexModule.default.btnGroup, " ").concat(item.previewUrl ? _indexModule.default.hasPreview : '')
  }, /*#__PURE__*/_react.default.createElement(_ImagePreview.default, {
    img: item.img,
    cls: _indexModule.default.previewBtn
  }), /*#__PURE__*/_react.default.createElement("div", {
    className: _indexModule.default.btnSep
  }), item.previewUrl && /*#__PURE__*/_react.default.createElement(_antd.Tooltip, {
    title: intl({
      id: 'org.umi.ui.blocks.list.preview.demo'
    }),
    placement: "bottom"
  }, /*#__PURE__*/_react.default.createElement(_antd.Button, {
    className: _indexModule.default.previewBtn,
    target: "_blank",
    href: item.previewUrl
  }, /*#__PURE__*/_react.default.createElement(_icons.ExportOutlined, null))))), /*#__PURE__*/_react.default.createElement(_ImageLoad.default, {
    src: item.img
  }))), /*#__PURE__*/_react.default.createElement("div", {
    className: _indexModule.default.content
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: _indexModule.default.title
  }, /*#__PURE__*/_react.default.createElement(_HighlightedText.default, {
    text: item.name,
    highlight: keyword
  })), item.description && !isMini && /*#__PURE__*/_react.default.createElement(_antd.Typography.Paragraph, {
    className: _indexModule.default.description,
    ellipsis: {
      rows: 2,
      expandable: false
    }
  }, /*#__PURE__*/_react.default.createElement(_HighlightedText.default, {
    text: item.description,
    highlight: keyword
  })), !isMini && selectedTag === '' && /*#__PURE__*/_react.default.createElement(Meats, {
    item: item,
    keyword: keyword
  }))));
};

var _default = BlockItem;
exports.default = _default;