// 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 as FoxglovePointsAnnotationType } from "@foxglove/schemas";
import { estimateObjectSize } from "@foxglove/studio-base/players/messageMemoryEstimation.js";
import { DEFAULT_BACKGROUND_COLOR, DEFAULT_STYLE, DEFAULT_TEXT_COLOR } from "./constants.js";
import { DEFAULT_HEIGHT, DEFAULT_WIDTH, getTextSize } from "./utils.js";
import { PointsAnnotationType } from "../../../protobuf.generated/ImageAnnotations.js";
import { parseMessage } from "../../utils.js";
export function deserializeImgDetectionsCustom({ topic, message, callback, topicSizes }) {
    const { receiveTime } = parseMessage(message);
    const { width, height } = (topicSizes.values().next().value ?? {
        width: DEFAULT_WIDTH, height: DEFAULT_HEIGHT
    });
    const xMultiplier = width >= height ? 1 : height / width;
    const yMultiplier = height >= width ? 1 : width / height;
    const points = [];
    const texts = [];
    const circles = [];
    for (const annotations of message.annotations) {
        points.push(...annotations.points.map(annotation => ({
            timestamp: receiveTime,
            type: annotation.type === PointsAnnotationType.UNRECOGNIZED
                ? FoxglovePointsAnnotationType.UNKNOWN
                : Number(annotation.type),
            points: annotation.points,
            outline_color: annotation.outlineColor ?? DEFAULT_STYLE.outline,
            outline_colors: [],
            fill_color: annotation.fillColor ?? DEFAULT_STYLE.fill,
            thickness: annotation.thickness
        })));
        texts.push(...annotations.texts.map(annotation => ({
            timestamp: receiveTime,
            position: annotation.position ?? { x: 0, y: 0 },
            text: annotation.text,
            font_size: annotation.fontSize ?? getTextSize(width, height),
            text_color: annotation.textColor ?? DEFAULT_TEXT_COLOR,
            background_color: annotation.backgroundColor ?? DEFAULT_BACKGROUND_COLOR
        })));
        circles.push(...annotations.circles.map(annotation => ({
            timestamp: receiveTime,
            xMultiplier,
            yMultiplier,
            position: annotation.position ?? { x: 0, y: 0 },
            diameter: annotation.diameter,
            thickness: annotation.thickness,
            fill_color: annotation.fillColor ?? DEFAULT_STYLE.fill,
            outline_color: annotation.outlineColor ?? DEFAULT_STYLE.outline,
        })));
    }
    const foxgloveMessage = { points, texts, circles };
    callback({
        topic,
        receiveTime,
        message: foxgloveMessage,
        sizeInBytes: estimateObjectSize(foxgloveMessage),
        schemaName: "foxglove.ImageAnnotations"
    });
}
//# sourceMappingURL=custom.js.map