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

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

/**
 * Gets the currently active actor and token based on how {@link ChatMessage}
 * determines the current speaker.
 * @returns {{actor?: import("../documents/actor/actor").DS4Actor, token?: TokenDocument}} The currently active actor and token.
 */
export function getActiveActorAndToken() {
  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 };
}
