import { ygopro } from "@/api";
import { fetchCard, getCardStr } from "@/api/cards";
import { matStore, messageStore } from "@/stores";

export const fetchCheckCardMeta = async (
  {
    code,
    location,
    level1,
    level2,
    response,
    effectDescCode,
  }: {
    code: number;
    location: ygopro.CardLocation;
    level1?: number;
    level2?: number;
    response: number;
    effectDescCode?: number;
  },
  selected?: boolean,
  mustSelect?: boolean
) => {
  const controller = location.controler;
  const newID =
    code != 0
      ? code
      : matStore.in(location.location).of(controller)[location.sequence]
          ?.occupant?.id || 0;
  const meta = await fetchCard(code);

  const effectDesc = effectDescCode
    ? getCardStr(meta, effectDescCode & 0xf)
    : undefined;
  const newOption = {
    code: newID,
    location: location.toObject(),
    level1,
    level2,
    effectDesc,
    response,
  };

  if (selected) {
    messageStore.selectCardActions.selecteds.push(newOption);
  } else if (mustSelect) {
    messageStore.selectCardActions.mustSelects.push(newOption);
  } else {
    messageStore.selectCardActions.selectables.push(newOption);
  }
};
