// SPDX-FileCopyrightText: 2021 Johannes Loher
//
// SPDX-License-Identifier: MIT

import { DS4Actor } from "../actor/actor";
import { getCanvas, getGame } from "../helpers";

/**
 * Gets the currently active actor based on how {@link ChatMessage} determines
 * the current speaker.
 * @returns The currently active {@link DS4Actor} if any, and `undefined` otherwise.
 */
export function getActiveActor(): DS4Actor | undefined {
    const speaker = ChatMessage.getSpeaker();

    const speakerToken = speaker.token ? getCanvas().tokens?.get(speaker.token) : undefined;
    if (speakerToken) {
        return speakerToken.actor ?? undefined;
    }

    const speakerActor = speaker.actor ? getGame().actors?.get(speaker.actor) : undefined;
    if (speakerActor) {
        return speakerActor;
    }
}