"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CurrentAsset = exports.AssetInfo = exports.getCurrentAssetMediaDetailLines = exports.getCurrentAssetMetadataSource = exports.CURRENT_ASSET_HEIGHT = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const studio_shared_1 = require("@remotion/studio-shared");
const react_1 = require("react");
const remotion_1 = require("remotion");
const client_id_1 = require("../helpers/client-id");
const format_media_duration_1 = require("../helpers/format-media-duration");
const get_preview_file_type_1 = require("../helpers/get-preview-file-type");
const render_codec_label_1 = require("../helpers/render-codec-label");
const use_media_metadata_1 = require("../helpers/use-media-metadata");
const InlineEditableTitle_1 = require("./InlineEditableTitle");
const InspectorInfoHeader_1 = require("./InspectorInfoHeader");
const use_rename_static_file_1 = require("./NewComposition/use-rename-static-file");
const use_static_files_1 = require("./use-static-files");
exports.CURRENT_ASSET_HEIGHT = InspectorInfoHeader_1.INSPECTOR_INFO_HEADER_MIN_HEIGHT;
const getCurrentAssetMetadataSource = (assetName) => {
    if (!assetName) {
        return null;
    }
    const fileType = (0, get_preview_file_type_1.getPreviewFileType)(assetName);
    return fileType === 'audio' || fileType === 'video'
        ? (0, remotion_1.staticFile)(assetName)
        : null;
};
exports.getCurrentAssetMetadataSource = getCurrentAssetMetadataSource;
const formatFps = (fps) => `${fps.toFixed(2)} FPS`;
const getCurrentAssetMediaDetailLines = (mediaMetadata) => {
    const detailLines = [];
    if (mediaMetadata.hasVideoTrack === true) {
        const videoParts = [
            (0, render_codec_label_1.renderHumanReadableVideoCodec)(mediaMetadata.videoCodec),
        ];
        if (mediaMetadata.fps !== null) {
            videoParts.push(formatFps(mediaMetadata.fps));
        }
        if (mediaMetadata.isHdr !== null) {
            videoParts.push(`HDR: ${mediaMetadata.isHdr ? 'Yes' : 'No'}`);
        }
        detailLines.push(`Video: ${videoParts.join(' · ')}`);
    }
    if (mediaMetadata.hasAudioTrack === true) {
        const audioParts = [
            (0, render_codec_label_1.renderHumanReadableAudioCodec)(mediaMetadata.audioCodec),
        ];
        if (mediaMetadata.sampleRate !== null) {
            audioParts.push(`${mediaMetadata.sampleRate} Hz`);
        }
        detailLines.push(`Audio: ${audioParts.join(' · ')}`);
    }
    else if (mediaMetadata.hasAudioTrack === false) {
        detailLines.push('Audio: No audio');
    }
    return detailLines;
};
exports.getCurrentAssetMediaDetailLines = getCurrentAssetMediaDetailLines;
const AssetInfo = ({ assetName, contentSized = false, onAssetClick, readOnlyStudio }) => {
    var _a;
    const connectionStatus = (0, react_1.useContext)(client_id_1.StudioServerConnectionCtx)
        .previewServerState.type;
    const staticFiles = (0, use_static_files_1.useStaticFiles)();
    const renameFile = (0, use_rename_static_file_1.useRenameStaticFile)({
        relativePath: assetName !== null && assetName !== void 0 ? assetName : '',
        staticFiles,
    });
    const sizeInBytes = (0, react_1.useMemo)(() => {
        var _a;
        if (!assetName) {
            return null;
        }
        const file = staticFiles.find((f) => f.name === assetName);
        return (_a = file === null || file === void 0 ? void 0 : file.sizeInBytes) !== null && _a !== void 0 ? _a : null;
    }, [assetName, staticFiles]);
    const src = (0, exports.getCurrentAssetMetadataSource)(assetName);
    const mediaMetadata = (0, use_media_metadata_1.useMediaMetadata)(src);
    const canRename = onAssetClick === undefined &&
        connectionStatus === 'connected' &&
        !readOnlyStudio;
    const onRename = (0, react_1.useCallback)((newName) => {
        renameFile(newName).catch(() => undefined);
    }, [renameFile]);
    if (!assetName) {
        return jsx_runtime_1.jsx(InspectorInfoHeader_1.InspectorInfoHeader, { contentSized: contentSized });
    }
    const fileName = (_a = assetName.split('/').pop()) !== null && _a !== void 0 ? _a : assetName;
    const subtitleParts = [];
    if (sizeInBytes !== null) {
        subtitleParts.push((0, studio_shared_1.formatBytes)(sizeInBytes));
    }
    if (mediaMetadata) {
        if (mediaMetadata.format) {
            subtitleParts.push(mediaMetadata.format);
        }
        if (mediaMetadata.width !== null && mediaMetadata.height !== null) {
            subtitleParts.push(`${mediaMetadata.width}x${mediaMetadata.height}`);
        }
    }
    const mediaDetailLines = mediaMetadata
        ? (0, exports.getCurrentAssetMediaDetailLines)(mediaMetadata)
        : [];
    return (jsx_runtime_1.jsxs(InspectorInfoHeader_1.InspectorInfoHeader, { contentSized: contentSized, children: [
            jsx_runtime_1.jsx(InlineEditableTitle_1.InlineEditableTitle, { value: fileName, canRename: canRename, getInitialSelection: use_rename_static_file_1.getStaticFileRenameSelection, onClick: onAssetClick, onCommit: onRename, title: assetName }), subtitleParts.length > 0 ? (jsx_runtime_1.jsx(InspectorInfoHeader_1.InspectorInfoSubtitle, { children: subtitleParts.join(' · ') })) : null, mediaMetadata ? (jsx_runtime_1.jsx(InspectorInfoHeader_1.InspectorInfoSubtitle, { children: (0, format_media_duration_1.formatMediaDuration)(mediaMetadata.duration) })) : null, mediaDetailLines.map((line) => {
                return jsx_runtime_1.jsx(InspectorInfoHeader_1.InspectorInfoSubtitle, { children: line }, line);
            })] }));
};
exports.AssetInfo = AssetInfo;
const CurrentAsset = ({ readOnlyStudio }) => {
    const { canvasContent } = (0, react_1.useContext)(remotion_1.Internals.CompositionManager);
    const assetName = (canvasContent === null || canvasContent === void 0 ? void 0 : canvasContent.type) === 'asset' ? canvasContent.asset : null;
    return jsx_runtime_1.jsx(exports.AssetInfo, { assetName: assetName, readOnlyStudio: readOnlyStudio });
};
exports.CurrentAsset = CurrentAsset;
