"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateSelectedTimelineEasings = exports.getTimelineEasingValueForSelection = exports.getEasingSelections = exports.makeEasingDragOverride = exports.getSelectedEasingUpdates = exports.getSelectedEasingUpdate = exports.canEditEasingForInterpolationFunction = void 0;
const studio_shared_1 = require("@remotion/studio-shared");
const studio_shared_2 = require("@remotion/studio-shared");
Object.defineProperty(exports, "canEditEasingForInterpolationFunction", { enumerable: true, get: function () { return studio_shared_2.canEditEasingForInterpolationFunction; } });
const remotion_1 = require("remotion");
const call_update_keyframe_settings_1 = require("./call-update-keyframe-settings");
const find_track_for_node_path_info_1 = require("./find-track-for-node-path-info");
const parse_keyframe_field_from_node_path_1 = require("./parse-keyframe-field-from-node-path");
const isEasingSelection = (selection) => selection.type === 'easing';
const getSelectedEasingUpdate = ({ selection, sequences, overrideIdsToNodePaths, propStatuses, }) => {
    var _a;
    var _b, _c, _d;
    const field = (0, parse_keyframe_field_from_node_path_1.parseKeyframeFieldFromNodePath)(selection.nodePathInfo.auxiliaryKeys);
    if (field === null) {
        return null;
    }
    const track = (0, find_track_for_node_path_info_1.findTrackForNodePathInfo)({
        sequences,
        overrideIdsToNodePaths,
        nodePathInfo: selection.nodePathInfo,
    });
    const sequence = (_b = track === null || track === void 0 ? void 0 : track.sequence) !== null && _b !== void 0 ? _b : null;
    if (!sequence) {
        return null;
    }
    const nodePath = selection.nodePathInfo.sequenceSubscriptionKey;
    const fileName = nodePath.absolutePath;
    if (field.type === 'sequence') {
        if (!sequence.controls) {
            return null;
        }
        const sequencePropStatus = (_a = remotion_1.Internals.getPropStatusesCtx(propStatuses, nodePath)) === null || _a === void 0 ? void 0 : _a[field.fieldKey];
        if ((sequencePropStatus === null || sequencePropStatus === void 0 ? void 0 : sequencePropStatus.status) !== 'keyframed' ||
            !(0, studio_shared_1.canEditEasingForInterpolationFunction)(sequencePropStatus.interpolationFunction)) {
            return null;
        }
        return {
            type: 'sequence',
            fileName,
            nodePath,
            fieldKey: field.fieldKey,
            schema: sequence.controls.schema,
            segmentIndex: selection.segmentIndex,
            currentEasing: (_c = sequencePropStatus.easing[selection.segmentIndex]) !== null && _c !== void 0 ? _c : studio_shared_1.LINEAR_KEYFRAME_EASING,
            propStatus: sequencePropStatus,
        };
    }
    const effect = sequence.effects[field.effectIndex];
    if (!effect) {
        return null;
    }
    const effectStatus = remotion_1.Internals.getEffectPropStatusesCtx({
        propStatuses,
        nodePath,
        effectIndex: field.effectIndex,
    });
    const effectPropStatus = effectStatus.type === 'can-update-effect'
        ? effectStatus.props[field.fieldKey]
        : null;
    if ((effectPropStatus === null || effectPropStatus === void 0 ? void 0 : effectPropStatus.status) !== 'keyframed' ||
        !(0, studio_shared_1.canEditEasingForInterpolationFunction)(effectPropStatus.interpolationFunction)) {
        return null;
    }
    return {
        type: 'effect',
        fileName,
        nodePath,
        effectIndex: field.effectIndex,
        fieldKey: field.fieldKey,
        schema: effect.schema,
        segmentIndex: selection.segmentIndex,
        currentEasing: (_d = effectPropStatus.easing[selection.segmentIndex]) !== null && _d !== void 0 ? _d : studio_shared_1.LINEAR_KEYFRAME_EASING,
        propStatus: effectPropStatus,
    };
};
exports.getSelectedEasingUpdate = getSelectedEasingUpdate;
const getSelectedEasingUpdates = ({ selections, sequences, overrideIdsToNodePaths, propStatuses, }) => {
    return (0, exports.getEasingSelections)(selections)
        .map((selection) => (0, exports.getSelectedEasingUpdate)({
        selection,
        sequences,
        overrideIdsToNodePaths,
        propStatuses,
    }))
        .filter((update) => update !== null);
};
exports.getSelectedEasingUpdates = getSelectedEasingUpdates;
const makeEasingDragOverride = ({ status, segmentIndex, easing, }) => {
    const nextEasing = [...status.easing];
    while (nextEasing.length < status.keyframes.length - 1) {
        nextEasing.push(studio_shared_1.LINEAR_KEYFRAME_EASING);
    }
    if (nextEasing.length > status.keyframes.length - 1) {
        nextEasing.length = status.keyframes.length - 1;
    }
    nextEasing[segmentIndex] = easing;
    return {
        type: 'keyframed',
        status: {
            ...status,
            easing: nextEasing,
        },
    };
};
exports.makeEasingDragOverride = makeEasingDragOverride;
const getEasingSelections = (selections) => selections.filter(isEasingSelection);
exports.getEasingSelections = getEasingSelections;
const getTimelineEasingValueForSelection = ({ selection, sequences, overrideIdsToNodePaths, propStatuses, }) => {
    var _a;
    var _b;
    return ((_b = (_a = (0, exports.getSelectedEasingUpdate)({
        selection,
        sequences,
        overrideIdsToNodePaths,
        propStatuses,
    })) === null || _a === void 0 ? void 0 : _a.currentEasing) !== null && _b !== void 0 ? _b : null);
};
exports.getTimelineEasingValueForSelection = getTimelineEasingValueForSelection;
const updateSelectedTimelineEasings = ({ selections, sequences, overrideIdsToNodePaths, propStatuses, setPropStatuses, clientId, easing, }) => {
    const easingSelections = (0, exports.getEasingSelections)(selections);
    if (easingSelections.length === 0) {
        return null;
    }
    const updates = (0, exports.getSelectedEasingUpdates)({
        selections: easingSelections,
        sequences,
        overrideIdsToNodePaths,
        propStatuses,
    });
    if (updates.length === 0) {
        return null;
    }
    return Promise.all(updates.map((update) => {
        const settings = {
            type: 'easing',
            segmentIndex: update.segmentIndex,
            easing,
        };
        if (update.type === 'sequence') {
            return (0, call_update_keyframe_settings_1.callUpdateSequenceKeyframeSettings)({
                fileName: update.fileName,
                nodePath: update.nodePath,
                fieldKey: update.fieldKey,
                settings,
                schema: update.schema,
                setPropStatuses,
                clientId,
            });
        }
        return (0, call_update_keyframe_settings_1.callUpdateEffectKeyframeSettings)({
            fileName: update.fileName,
            nodePath: update.nodePath,
            effectIndex: update.effectIndex,
            fieldKey: update.fieldKey,
            settings,
            schema: update.schema,
            setPropStatuses,
            clientId,
        });
    })).then(() => undefined);
};
exports.updateSelectedTimelineEasings = updateSelectedTimelineEasings;
