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

import { getCanvas, getGame } from "../utils/utils";

import type { DS4Actor } from "../documents/actor/actor";

/**
 * Gets the currently active actor and token based on how {@link ChatMessage}
 * determines the current speaker.
 * @returns The currently active {@link DS4Actor} and {@link TokenDocument}.
 */
export function getActiveActorAndToken(): { actor?: DS4Actor; token?: TokenDocument } {
    const speaker = ChatMessage.getSpeaker();

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

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