import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
// RIA = React Image Annotate
export function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}
export function getRandomColor() {
  var h = getRandomInt(0, 360);
  var s = 100;
  var l = 50;
  return "hsl(".concat(h.toString(), ",").concat(s.toString(), "%,").concat(l.toString(), "%)");
}
export var rid = function rid() {
  return Math.random().toString().split(".")[1];
};
export var convertToRIARegionFmt = function convertToRIARegionFmt(region) {
  switch (region.regionType) {
    case "bounding-box":
      {
        return {
          id: region.id || rid(),
          cls: region.classification,
          tags: region.labels,
          color: region.color || getRandomColor(),
          type: "box",
          x: region.centerX - region.width / 2,
          y: region.centerY - region.height / 2,
          w: region.width,
          h: region.height
        };
      }

    case "point":
      {
        return {
          id: region.id || rid(),
          type: "point",
          tags: region.labels,
          cls: region.classification,
          color: region.color || getRandomColor(),
          x: region.x,
          y: region.y
        };
      }

    case "polygon":
      {
        return {
          id: region.id || rid(),
          type: "polygon",
          tags: region.labels,
          cls: region.classification,
          color: region.color || getRandomColor(),
          open: false,
          points: region.points.map(function (p) {
            return [p.x, p.y];
          })
        };
      }

    case "line":
    case "pixel-mask":
      {
        throw new Error("Unsupported region \"".concat(JSON.stringify(region), "\""));
      }

    case "keypoints":
      {
        return {
          id: region.id || rid(),
          type: "keypoints",
          keypointsDefinitionId: region.keypointsDefinitionId,
          points: region.points
        };
      }

    default:
      return null;
  }
};
export var convertFromRIARegionFmt = function convertFromRIARegionFmt(riaRegion) {
  switch (riaRegion.type) {
    case "point":
      {
        return {
          regionType: "point",
          id: riaRegion.id,
          x: riaRegion.x,
          y: riaRegion.y,
          classification: riaRegion.cls,
          labels: riaRegion.tags,
          color: riaRegion.color
        };
      }

    case "box":
      {
        return {
          regionType: "bounding-box",
          id: riaRegion.id,
          centerX: riaRegion.x + riaRegion.w / 2,
          centerY: riaRegion.y + riaRegion.h / 2,
          width: riaRegion.w,
          height: riaRegion.h,
          classification: riaRegion.cls,
          labels: riaRegion.tags,
          color: riaRegion.color
        };
      }

    case "polygon":
      {
        return {
          regionType: "polygon",
          id: riaRegion.id,
          classification: riaRegion.cls,
          labels: riaRegion.tags,
          color: riaRegion.color,
          points: riaRegion.points.map(function (_ref) {
            var _ref2 = _slicedToArray(_ref, 2),
                x = _ref2[0],
                y = _ref2[1];

            return {
              x: x,
              y: y
            };
          })
        };
      }

    case "keypoints":
      {
        return {
          id: riaRegion.id || rid(),
          keypointsDefinitionId: riaRegion.keypointsDefinitionId,
          regionType: "keypoints",
          points: riaRegion.points
        };
      }

    default:
      throw new Error("Unsupported riaRegion \"".concat(JSON.stringify(riaRegion), "\""));
  }
};
export var convertToRIAImageFmt = function convertToRIAImageFmt(_ref3) {
  var title = _ref3.title,
      td = _ref3.taskDatum,
      i = _ref3.index,
      output = _ref3.output;
  td = td || {};
  var regions = !output ? undefined : Array.isArray(output) ? output.map(convertToRIARegionFmt) : [convertToRIARegionFmt(output)];

  if (td.imageUrl) {
    return {
      src: td.imageUrl,
      name: title || "Sample ".concat(i),
      regions: regions
    };
  } else if (td.videoUrl && td.videoFrameAt !== undefined) {
    return {
      src: td.videoUrl,
      frameTime: td.videoFrameAt,
      name: title || "Sample ".concat(i),
      regions: regions
    };
  }

  throw new Error("Task Datum not supported \"".concat(JSON.stringify(td), "\""));
};
export var convertToRIAKeyframes = function convertToRIAKeyframes(keyframes) {
  var newKeyframes = {};

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

  return newKeyframes;
};