"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 _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

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

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

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

var _renderUi = require("@pie-lib/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: {
    // this style is applied only on small screens and for touch devices when dragging, for drag-in-the-blank.
    // It is styled to be identical to the drag-in-the-blank chip
    backgroundColor: _renderUi.color.white(),
    border: "1px solid ".concat(_renderUi.color.text()),
    color: _renderUi.color.text(),
    alignItems: 'center',
    display: 'inline-flex',
    height: 'initial',
    minHeight: '32px',
    fontSize: 'inherit',
    whiteSpace: 'pre-wrap',
    maxWidth: '374px',
    borderRadius: '3px',
    padding: '12px'
  },
  ica: {
    backgroundColor: _renderUi.color.background(),
    border: "1px solid ".concat(_renderUi.color.borderDark()),
    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, touchPosition, style) {
  var transform = "translate(".concat(touchPosition.x, "px, ").concat(touchPosition.y, "px)");
  var top = (style === null || style === void 0 ? void 0 : style.top) || 0;
  var left = (style === null || style === void 0 ? void 0 : style.left) || 0;
  var position = (style === null || style === void 0 ? void 0 : style.position) || 'fixed';
  return _objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread({
    position: position,
    top: top,
    left: left,
    transform: transform
  }, 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 : {});
};

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 _useState = (0, _react.useState)({
    x: 0,
    y: 0
  }),
      _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
      touchPosition = _useState2[0],
      setTouchPosition = _useState2[1];

  var _useState3 = (0, _react.useState)(1),
      _useState4 = (0, _slicedToArray2["default"])(_useState3, 2),
      zoomLevel = _useState4[0],
      setZoomLevel = _useState4[1];

  var handleTouchMove = (0, _react.useCallback)(function (event) {
    if (event.touches.length > 0) {
      var touch = event.touches[0];
      var touchOffset = 1;
      setTouchPosition({
        x: (touch.clientX + touchOffset) / zoomLevel,
        y: (touch.clientY + touchOffset) / zoomLevel
      });
    }
  }, [zoomLevel]);
  var root = (0, _react.useRef)(null);
  (0, _react.useEffect)(function () {
    if (display && root.current) {
      (0, _mathRendering.renderMath)(root.current); // Adjusted for precise zoom level calculation in Online Testing, targeting the specific class pattern .asmt-zoomable.asmt-zoom-NR .asmt-question .padding

      var zoomAffectedElement = document.querySelector('.padding') || document.body;
      setZoomLevel(parseFloat(getComputedStyle(zoomAffectedElement).zoom) || 1);
    }
  }, [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]);
  (0, _react.useEffect)(function () {
    var touchMoveListener = function touchMoveListener(event) {
      return handleTouchMove(event);
    };

    if (display) {
      window.addEventListener('touchmove', touchMoveListener);
    }

    return function () {
      window.removeEventListener('touchmove', touchMoveListener);
    };
  }, [display, handleTouchMove]);

  if (!display) {
    return null;
  }

  var customStyle = getCustomStyle(itemType, item, touchPosition, 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