"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _typeof = require("@babel/runtime/helpers/typeof");

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

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

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

var _reactDndMultiBackend = require("react-dnd-multi-backend");

var _renderUi = require("../render-ui");

var _mathRendering = require("@pie-lib/math-rendering");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

var styles = {
  maskBlank: {
    border: '1px solid black',
    color: 'black',
    minWidth: '90px',
    minHeight: '32px',
    height: 'auto',
    maxWidth: '374px',
    display: 'flex',
    padding: '4px',
    alignItems: 'center',
    justifyContent: 'center',
    borderRadius: '16px'
  },
  ica: {
    backgroundColor: _renderUi.color.background(),
    border: "1px solid ".concat(_renderUi.color.primary()),
    display: 'flex',
    alignItems: 'center',
    justifyContent: 'center',
    minHeight: '28px',
    padding: '0 3px',
    marginLeft: 2,
    marginTop: 2,
    width: 'fit-content'
  },
  categorize: {
    color: _renderUi.color.text(),
    backgroundColor: _renderUi.color.background(),
    padding: '16px',
    borderRadius: '4px',
    border: '1px solid'
  },
  matchList: {
    color: _renderUi.color.text(),
    backgroundColor: _renderUi.color.background(),
    padding: '10px',
    boxSizing: 'border-box',
    border: '1px solid #D1D1D1'
  },
  placementOrdering: {
    padding: '10px',
    boxSizing: 'border-box',
    border: '1px solid #D1D1D1',
    backgroundColor: _renderUi.color.background()
  }
};

var getPrompt = function getPrompt(itemType, item) {
  var _item$choice;

  switch (itemType) {
    // DRAG-IN-THE-BLANK
    case 'MaskBlank':
      return item === null || item === void 0 ? void 0 : (_item$choice = item.choice) === null || _item$choice === void 0 ? void 0 : _item$choice.value;
    // IMAGE-CLOZE-ASSOCIATION

    case 'react-dnd-response':
      return item === null || item === void 0 ? void 0 : item.value;
    // MATCH-LIST

    case 'Answer':
      return item === null || item === void 0 ? void 0 : item.value;
    // PLACEMENT-ORDERING

    case 'Tile':
      return item === null || item === void 0 ? void 0 : item.value;

    default:
      return (item === null || item === void 0 ? void 0 : item.itemType) === 'categorize' ? item === null || item === void 0 ? void 0 : item.value : undefined;
  }
};

var getCustomStyle = function getCustomStyle(itemType, item, style) {
  var baseStyle = _objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread({}, style), itemType === 'MaskBlank' ? styles.maskBlank : {}), (item === null || item === void 0 ? void 0 : item.itemType) === 'categorize' ? styles.categorize : {}), itemType === 'Answer' ? styles.matchList : {}), itemType === 'Tile' ? styles.placementOrdering : {}), itemType === 'react-dnd-response' ? styles.ica : {});

  return baseStyle;
};

var PreviewComponent = function PreviewComponent() {
  var _item$choice2;

  var preview = (0, _reactDndMultiBackend.usePreview)();
  var itemType = preview.itemType,
      item = preview.item,
      style = preview.style,
      display = preview.display;
  var root = (0, _react.useRef)(null);
  (0, _react.useEffect)(function () {
    if (display && root.current) {
      (0, _mathRendering.renderMath)(root.current);
    }
  }, [display, item === null || item === void 0 ? void 0 : (_item$choice2 = item.choice) === null || _item$choice2 === void 0 ? void 0 : _item$choice2.value, item === null || item === void 0 ? void 0 : item.value, itemType, item]);

  if (!display) {
    return null;
  }

  var customStyle = getCustomStyle(itemType, item, style);
  var prompt = getPrompt(itemType, item);
  return /*#__PURE__*/_react["default"].createElement("div", {
    ref: root,
    style: customStyle
  }, /*#__PURE__*/_react["default"].createElement(_renderUi.PreviewPrompt, {
    className: "label",
    prompt: prompt,
    tagName: "span"
  }));
};

var _default = PreviewComponent;
exports["default"] = _default;
//# sourceMappingURL=preview-component.js.map