// 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 { useMessageReducer } from "@foxglove/studio-base/PanelAPI/useMessageReducer.js";
import { Type } from "../protobuf.generated/ImgFrame.js";
import React from "react";
export function fromMillis(value) {
    let sec = Math.trunc(value / 1000);
    let nsec = Math.round((value - sec * 1000) * 1e6);
    sec += Math.trunc(nsec / 1e9);
    nsec %= 1e9;
    return { sec, nsec };
}
export function useOnMessage(topic, callback) {
    const addMessage = React.useCallback((_, event) => {
        callback(event);
    }, [callback]);
    const restore = React.useCallback(() => { }, []);
    useMessageReducer({
        topics: topic ? [topic] : [],
        addMessage,
        restore
    });
}
export function parsePixelFormat(format) {
    switch (format) {
        case "BGRA":
            return ["bgra8", 3];
        case "BGRX":
            return ["bgr8", 3]; // Closest supported
        case "I420":
        case "I420A":
            return ['I420', 2];
        case "I422":
        case "I444":
            return ['yuv422', 2];
        case "RGBA":
            return ["rgba8", 1];
        case "RGBX":
            return ["rgb8", 1]; // Closest supported
        case "NV12":
            return ["nv12", 1];
        default: // Unsupported
            return ["bgra8", 3];
    }
}
function parseMessageType(type) {
    switch (type) {
        case Type.YUV422i:
        case Type.YUV422p:
            return ["yuv422", 2];
        case Type.YUV420p:
            return ["I420", 1];
        case Type.NV12:
            return ["nv12", 1];
        case Type.RGB888i:
            return ["rgb8", 3];
        case Type.RGB888p:
            return ["rgb8p", 1];
        case Type.RGBA8888:
            return ["rgba8", 1];
        case Type.BGR888i:
            return ["bgr8i", 3];
        case Type.BGR888p:
            return ["bgr8p", 3];
        case Type.RAW16:
            return ["mono16", 2];
        case Type.RAW8:
            return ["mono8", 2];
        default:
            throw new Error(`Unsupported encoding ${Type[type]}`);
    }
}
export function parseMessage(message) {
    const width = Number(message.fb?.width ?? message.sourceFb?.width ?? 1920);
    const height = Number(message.fb?.height ?? message.sourceFb?.height ?? 1200);
    const stride = message.fb?.stride ?? message.sourceFb?.stride ?? null;
    const p2Offset = message.fb?.p2Offset ?? message.sourceFb?.p2Offset ?? null;
    const p1Offset = message.fb?.p1Offset ?? message.sourceFb?.p1Offset ?? null;
    let planeStride = (p1Offset != null && p2Offset != null) ? p2Offset - p1Offset : 0;
    if (planeStride <= 0) {
        planeStride = null;
    }
    const type = message.fb?.type ?? message.sourceFb?.type ?? Type.RGB888p;
    const [encoding, step] = parseMessageType(type);
    const receiveTime = message.ts ?? message.tsDevice ?? fromMillis(Date.now());
    return { encoding, step: stride ?? step, receiveTime, width, height, stride, planeStride, type };
}
//# sourceMappingURL=utils.js.map