import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import React, { useMemo } from "react";
import Annotator from "react-image-annotate";
import useEventCallback from "use-event-callback";
import { convertFromRIARegionFmt, convertToRIAKeyframes } from "../../utils/ria-format.js";
var regionTypeToTool = {
  "bounding-box": "create-box",
  polygon: "create-polygon",
  "full-segmentation": "create-polygon",
  point: "create-point"
};
var emptyObj = {};
export default (function (_ref) {
  var iface = _ref.interface,
      sampleIndex = _ref.sampleIndex,
      sample = _ref.sample,
      _ref$containerProps = _ref.containerProps,
      containerProps = _ref$containerProps === void 0 ? emptyObj : _ref$containerProps,
      onModifySample = _ref.onModifySample;
  var _iface$regionTypesAll = iface.regionTypesAllowed,
      regionTypesAllowed = _iface$regionTypesAll === void 0 ? ["bounding-box"] : _iface$regionTypesAll;
  var isClassification = !Boolean(iface === null || iface === void 0 ? void 0 : iface.multipleRegionLabels);
  var labelProps = useMemo(function () {
    return isClassification ? {
      regionClsList: (iface.labels || []).map(function (l) {
        return typeof l === "string" ? l : l.id;
      })
    } : {
      regionTagList: (iface.labels || []).map(function (l) {
        return typeof l === "string" ? l : l.id;
      })
    };
  }, [isClassification, iface.labels]);

  var getUDTKeyFrames = function getUDTKeyFrames(riaOutput) {
    var newKeyframes = {};

    for (var key in riaOutput.keyframes) {
      newKeyframes[key] = {
        regions: riaOutput.keyframes[key].regions.map(convertFromRIARegionFmt)
      };
    }

    return newKeyframes;
  };

  var onExit = useEventCallback(function (output) {
    var annotation = {
      keyframes: getUDTKeyFrames(output)
    };
    onModifySample(_objectSpread(_objectSpread({}, sample), {}, {
      annotation: annotation
    }));
    if (containerProps.onExit) containerProps.onExit();
  });
  var onNext = useEventCallback(function (output) {
    var annotation = {
      keyframes: getUDTKeyFrames(output)
    };
    onModifySample(_objectSpread(_objectSpread({}, sample), {}, {
      annotation: annotation
    }));

    if (containerProps.onExit) {
      containerProps.onExit("go-to-next");
    }
  });
  var onPrev = useEventCallback(function (output) {
    var annotation = {
      keyframes: getUDTKeyFrames(output)
    };
    onModifySample(_objectSpread(_objectSpread({}, sample), {}, {
      annotation: annotation
    }));

    if (containerProps.onExit) {
      containerProps.onExit("go-to-previous");
    }
  });
  var enabledTools = useMemo(function () {
    return ["select"].concat(regionTypesAllowed.map(function (rt) {
      return regionTypeToTool[rt];
    }).filter(Boolean));
  }, [regionTypesAllowed]);
  if (!sample) throw new Error("No sample data provided selected");
  if (!sample.videoUrl) throw new Error("Sample must have videoUrl");
  var annotation = sample.annotation || {};
  return /*#__PURE__*/React.createElement("div", {
    style: {
      height: containerProps.height || "calc(100vh - 70px)",
      width: "100%",
      minHeight: 600
    }
  }, /*#__PURE__*/React.createElement(Annotator, Object.assign({
    key: sampleIndex,
    taskDescription: iface.description
  }, labelProps, {
    enabledTools: enabledTools,
    keyframes: convertToRIAKeyframes((annotation === null || annotation === void 0 ? void 0 : annotation.keyframes) || {}),
    onNextImage: onNext,
    onPrevImage: onPrev,
    videoName: sample.customId || "",
    videoTime: 0,
    videoSrc: sample.videoUrl,
    onExit: onExit
  })));
});