"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CurrentComposition = exports.CURRENT_COMPOSITION_HEIGHT = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const remotion_1 = require("remotion");
const is_composition_still_1 = require("../helpers/is-composition-still");
const open_in_editor_1 = require("../helpers/open-in-editor");
const react_2 = require("../icons/react");
const still_1 = require("../icons/still");
const video_1 = require("../icons/video");
const render_frame_1 = require("../state/render-frame");
const InlineCompositionName_1 = require("./InlineCompositionName");
const InspectorInfoHeader_1 = require("./InspectorInfoHeader");
const InspectorLocationCopy_1 = require("./InspectorLocationCopy");
const InspectorSourceLocation_1 = require("./InspectorSourceLocation");
const NotificationCenter_1 = require("./Notifications/NotificationCenter");
const use_resolved_stack_1 = require("./Timeline/use-resolved-stack");
exports.CURRENT_COMPOSITION_HEIGHT = InspectorInfoHeader_1.INSPECTOR_INFO_HEADER_MIN_HEIGHT;
const sourceLocationIconStyle = {
    flexShrink: 0,
    height: 13,
    width: 13,
};
const renderReactIcon = (color) => {
    return jsx_runtime_1.jsx(react_2.ReactIcon, { color: color, style: sourceLocationIconStyle });
};
const CurrentComposition = () => {
    var _a, _b, _c, _d, _e;
    const video = remotion_1.Internals.useVideo();
    const { compositions } = (0, react_1.useContext)(remotion_1.Internals.CompositionManager);
    const currentComposition = (0, react_1.useMemo)(() => {
        var _a;
        if (!video) {
            return null;
        }
        return ((_a = compositions.find((composition) => composition.id === video.id)) !== null && _a !== void 0 ? _a : null);
    }, [compositions, video]);
    const resolvedCompositionLocation = (0, use_resolved_stack_1.useResolvedStack)((_a = currentComposition === null || currentComposition === void 0 ? void 0 : currentComposition.stack) !== null && _a !== void 0 ? _a : null);
    const validatedLocation = (0, react_1.useMemo)(() => {
        if (!(resolvedCompositionLocation === null || resolvedCompositionLocation === void 0 ? void 0 : resolvedCompositionLocation.source) ||
            resolvedCompositionLocation.line === null) {
            return null;
        }
        return {
            column: resolvedCompositionLocation.column,
            line: resolvedCompositionLocation.line,
            source: resolvedCompositionLocation.source,
        };
    }, [resolvedCompositionLocation]);
    const compositionFile = (_b = validatedLocation === null || validatedLocation === void 0 ? void 0 : validatedLocation.source) !== null && _b !== void 0 ? _b : null;
    const compositionId = (_c = currentComposition === null || currentComposition === void 0 ? void 0 : currentComposition.id) !== null && _c !== void 0 ? _c : null;
    const compositionComponentInfo = (0, open_in_editor_1.useCachedCompositionComponentInfo)({
        compositionFile,
        compositionId,
    });
    const componentLocation = (_d = compositionComponentInfo === null || compositionComponentInfo === void 0 ? void 0 : compositionComponentInfo.location) !== null && _d !== void 0 ? _d : null;
    (0, react_1.useEffect)(() => {
        if (compositionFile === null || compositionId === null) {
            return;
        }
        (0, open_in_editor_1.preloadCompositionComponentInfo)({
            compositionFile,
            compositionId,
        });
    }, [compositionFile, compositionId]);
    const openFileLocation = (0, react_1.useCallback)(() => {
        if (!validatedLocation) {
            return;
        }
        (0, open_in_editor_1.openOriginalPositionInEditor)(validatedLocation).catch((err) => {
            (0, NotificationCenter_1.showNotification)(err.message, 2000);
        });
    }, [validatedLocation]);
    const openComponentLocation = (0, react_1.useCallback)(() => {
        if (!componentLocation) {
            return;
        }
        (0, open_in_editor_1.openOriginalPositionInEditor)(componentLocation).catch((err) => {
            (0, NotificationCenter_1.showNotification)(err.message, 2000);
        });
    }, [componentLocation]);
    const renderCompositionIcon = (0, react_1.useCallback)((color) => {
        if (!video) {
            return null;
        }
        return (0, is_composition_still_1.isCompositionStill)(video) ? (jsx_runtime_1.jsx(still_1.StillIcon, { color: color, style: sourceLocationIconStyle })) : (jsx_runtime_1.jsx(video_1.FilmIcon, { color: color, style: sourceLocationIconStyle }));
    }, [video]);
    return (jsx_runtime_1.jsx(InspectorInfoHeader_1.InspectorInfoHeader, { children: video ? (jsx_runtime_1.jsxs(jsx_runtime_1.Fragment, { children: [
                jsx_runtime_1.jsxs(InspectorLocationCopy_1.InspectorLocationCopy, { location: validatedLocation, name: video.id, children: [
                        jsx_runtime_1.jsx(InlineCompositionName_1.InlineCompositionName, { compositionId: video.id, stack: (_e = currentComposition === null || currentComposition === void 0 ? void 0 : currentComposition.stack) !== null && _e !== void 0 ? _e : null, compositions: compositions }, video.id), jsx_runtime_1.jsx(InspectorSourceLocation_1.InspectorSourceLocation, { location: validatedLocation, canOpen: validatedLocation !== null, onOpen: openFileLocation, renderIcon: renderCompositionIcon }), jsx_runtime_1.jsx(InspectorSourceLocation_1.InspectorSourceLocation, { location: componentLocation, canOpen: componentLocation !== null, onOpen: openComponentLocation, renderIcon: renderReactIcon })
                    ] }), jsx_runtime_1.jsxs(InspectorInfoHeader_1.InspectorInfoSubtitle, { children: [video.width, "x", video.height, (0, is_composition_still_1.isCompositionStill)(video) ? null : `, ${video.fps} FPS`] }), (0, is_composition_still_1.isCompositionStill)(video) ? (jsx_runtime_1.jsx(InspectorInfoHeader_1.InspectorInfoSubtitle, { children: "Still" })) : (jsx_runtime_1.jsxs(InspectorInfoHeader_1.InspectorInfoSubtitle, { children: ["Duration ", (0, render_frame_1.renderFrame)(video.durationInFrames, video.fps)] }))] })) : null }));
};
exports.CurrentComposition = CurrentComposition;
