"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SequenceSelectionInspector = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const client_id_1 = require("../../helpers/client-id");
const InspectorSequenceSection_1 = require("../InspectorSequenceSection");
const is_menu_item_1 = require("../Menu/is-menu-item");
const TimelineSelection_1 = require("../Timeline/TimelineSelection");
const AlignmentControls_1 = require("./AlignmentControls");
const common_1 = require("./common");
const SequenceInspectorHeader_1 = require("./SequenceInspectorHeader");
const styles_1 = require("./styles");
const use_track_for_selection_1 = require("./use-track-for-selection");
const SequenceExpandedInspector = ({ track }) => {
    const { previewServerState } = (0, react_1.useContext)(client_id_1.StudioServerConnectionCtx);
    const { selectedItems, selectItems } = (0, TimelineSelection_1.useTimelineSelection)();
    const sourceLocation = (0, SequenceInspectorHeader_1.useSequenceInspectorSourceLocation)(track.sequence);
    const { validatedLocation } = sourceLocation;
    const sequenceSelection = (0, react_1.useMemo)(() => {
        if (!track.nodePathInfo) {
            return null;
        }
        return {
            type: 'sequence',
            nodePathInfo: track.nodePathInfo,
        };
    }, [track.nodePathInfo]);
    const sequenceSelected = (0, react_1.useMemo)(() => {
        if (sequenceSelection === null || selectedItems.length !== 1) {
            return false;
        }
        return ((0, TimelineSelection_1.getTimelineSelectionKey)(selectedItems[0]) ===
            (0, TimelineSelection_1.getTimelineSelectionKey)(sequenceSelection));
    }, [selectedItems, sequenceSelection]);
    const selectSequenceOnInspectorPointerDown = (0, react_1.useCallback)((event) => {
        if (event.button !== 0 || sequenceSelection === null) {
            return;
        }
        event.stopPropagation();
        if (sequenceSelected) {
            return;
        }
        selectItems([sequenceSelection], { reveal: true });
    }, [selectItems, sequenceSelected, sequenceSelection]);
    if (previewServerState.type !== 'connected') {
        return jsx_runtime_1.jsx(common_1.InspectorMessage, { children: "Studio server disconnected" });
    }
    if (!track.nodePathInfo ||
        !(0, InspectorSequenceSection_1.hasSequenceControls)(track.sequence) ||
        !validatedLocation) {
        return jsx_runtime_1.jsx(common_1.InspectorMessage, { children: "Sequence inspector unavailable" });
    }
    return (jsx_runtime_1.jsxs("div", { style: styles_1.selectedContainer, className: is_menu_item_1.VERTICAL_SCROLLBAR_CLASSNAME, onPointerDown: selectSequenceOnInspectorPointerDown, children: [
            jsx_runtime_1.jsx(SequenceInspectorHeader_1.SequenceInspectorHeader, { sourceLocation: sourceLocation, track: track }), jsx_runtime_1.jsx(InspectorSequenceSection_1.InspectorSequenceSection, { sequence: track.sequence, validatedLocation: validatedLocation, nodePathInfo: track.nodePathInfo, keyframeDisplayOffset: track.keyframeDisplayOffset, renderSectionHeader: (children) => (jsx_runtime_1.jsx(common_1.InspectorSectionHeader, { children: children })), renderTransformControls: () => jsx_runtime_1.jsx(AlignmentControls_1.AlignmentControls, { track: track }) })] }));
};
const SequenceSelectionInspector = ({ selection }) => {
    const track = (0, use_track_for_selection_1.useTrackForSelection)(selection);
    if (!track) {
        return jsx_runtime_1.jsx(common_1.InspectorMessage, { children: "Sequence inspector unavailable" });
    }
    const stackKey = track.sequence.getStack();
    return (jsx_runtime_1.jsx(SequenceExpandedInspector, { track: track }, stackKey !== null && stackKey !== void 0 ? stackKey : track.sequence.id));
};
exports.SequenceSelectionInspector = SequenceSelectionInspector;
