// This Source Code Form is subject to the terms of the Mozilla Public
// License, v2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/
import { PointsAnnotationType } from "@foxglove/schemas";
import { estimateObjectSize } from "@foxglove/studio-base/players/messageMemoryEstimation.js";
import { DEFAULT_BACKGROUND_COLOR, DEBUG_COLOR, DEFAULT_TEXT_COLOR, DETECTION_STYLE, DEFAULT_STYLE } from "./constants.js";
import { DEFAULT_HEIGHT, DEFAULT_WIDTH, getBorderThickness, getTextSize } from "./utils.js";
import { parseMessage } from "../../utils.js";
export function deserializeImgDetections({ topic, message, callback, topicSizes }) {
    const { receiveTime } = parseMessage(message);
    const foxgloveMessage = {
        points: [],
        texts: [],
        circles: [],
    };
    if (message.detections.length > 0) {
        for (const detection of message.detections) {
            const detectionWidth = detection.xmax - detection.xmin;
            const { width, height } = (topicSizes.values().next().value ?? {
                width: DEFAULT_WIDTH, height: DEFAULT_HEIGHT
            });
            const ratio = width / height;
            const highligtLength = 0.035;
            const highlightSizeX = Math.min(highligtLength, detectionWidth);
            const highlightSizeY = Math.min(highligtLength * ratio, detection.ymax - detection.ymin);
            const style = DETECTION_STYLE[detection.label] ?? DEFAULT_STYLE;
            if (detection.labelName) {
                style.label = detection.labelName;
            }
            foxgloveMessage.points.push({
                timestamp: receiveTime,
                points: [
                    { x: detection.xmin, y: detection.ymin },
                    { x: detection.xmin, y: detection.ymax },
                    { x: detection.xmax, y: detection.ymax },
                    { x: detection.xmax, y: detection.ymin },
                ],
                type: PointsAnnotationType.LINE_LOOP,
                outline_colors: [],
                thickness: 0,
                outline_color: DEBUG_COLOR,
                fill_color: style.fill,
            });
            const baseHighlightConfig = {
                timestamp: receiveTime,
                type: PointsAnnotationType.LINE_LIST,
                outline_colors: [],
                thickness: getBorderThickness(width, height),
                outline_color: style.outline,
                fill_color: DEBUG_COLOR,
            };
            foxgloveMessage.points.push({
                ...baseHighlightConfig,
                points: [
                    { x: detection.xmin, y: detection.ymin },
                    { x: detection.xmin + highlightSizeX, y: detection.ymin },
                ],
            });
            foxgloveMessage.points.push({
                ...baseHighlightConfig,
                points: [
                    { x: detection.xmin, y: detection.ymin },
                    { x: detection.xmin, y: detection.ymin + highlightSizeY },
                ],
            });
            foxgloveMessage.points.push({
                ...baseHighlightConfig,
                points: [
                    { x: detection.xmax, y: detection.ymin },
                    { x: detection.xmax - highlightSizeX, y: detection.ymin },
                ],
            });
            foxgloveMessage.points.push({
                ...baseHighlightConfig,
                points: [
                    { x: detection.xmax, y: detection.ymin },
                    { x: detection.xmax, y: detection.ymin + highlightSizeY },
                ],
            });
            foxgloveMessage.points.push({
                ...baseHighlightConfig,
                points: [
                    { x: detection.xmax, y: detection.ymax },
                    { x: detection.xmax - highlightSizeX, y: detection.ymax },
                ],
            });
            foxgloveMessage.points.push({
                ...baseHighlightConfig,
                points: [
                    { x: detection.xmax, y: detection.ymax },
                    { x: detection.xmax, y: detection.ymax - highlightSizeY },
                ],
            });
            foxgloveMessage.points.push({
                ...baseHighlightConfig,
                points: [
                    { x: detection.xmin, y: detection.ymax },
                    { x: detection.xmin + highlightSizeX, y: detection.ymax },
                ],
            });
            foxgloveMessage.points.push({
                ...baseHighlightConfig,
                points: [
                    { x: detection.xmin, y: detection.ymax },
                    { x: detection.xmin, y: detection.ymax - highlightSizeY },
                ],
            });
            const label = `${style.label ?? `Unknown #${detection.label}`} ${Math.round(detection.confidence * 100)}%`;
            foxgloveMessage.texts.push({
                timestamp: receiveTime,
                position: { x: detection.xmin + highligtLength / 2, y: detection.ymin + highlightSizeY },
                text: label,
                font_size: getTextSize(width, height),
                text_color: DEFAULT_TEXT_COLOR,
                background_color: DEFAULT_BACKGROUND_COLOR
            });
        }
    }
    callback({
        topic,
        receiveTime,
        message: foxgloveMessage,
        sizeInBytes: estimateObjectSize(foxgloveMessage),
        schemaName: "foxglove.ImageAnnotations"
    });
}
//# sourceMappingURL=detection.js.map