import { storageUtil } from 'press-ui/common/utils/storage';

import { AWARD_GET_STATUS } from './config';

interface PrizeInfo {
  status: number;
}


const MATCHING_LOCAL_STORAGE_KEY = 'matching_result_show_';


const getMatchingLocalStorageKey = ({
  siteId,
  parentId,
  childId,
}: {
  siteId: string | number;
  parentId: string | number;
  childId: string | number;
}) => `${MATCHING_LOCAL_STORAGE_KEY}${siteId}_${parentId}_${childId}`;


export function getCanClaimPrizeList({
  prizeList,
}: {
  prizeList: Array<PrizeInfo>;
}) {
  const canClaimPrizeList = prizeList.filter(item => item.status == AWARD_GET_STATUS.CAN_CLAIM);

  // TODO: to delete
  // const canClaimPrizeList = prizeList;


  return canClaimPrizeList;
}


export function checkMatchingResult({
  siteId,
  parentId,
  childId,
  prizeList,
  matchingScoreInfo,
  isJoined,
  isEnd,
}: {
  siteId: string | number;
  parentId: string | number;
  childId: string | number;
  prizeList: Array<PrizeInfo>;
  matchingScoreInfo: { rank?: number };
  isJoined?: boolean;
  isEnd?: boolean;
}) {
  console.log('checkMatchingResult', {
    siteId,
    parentId,
    childId,
    prizeList,
    matchingScoreInfo,
  });
  if (!isJoined || !isEnd) {
    return {
      showDialog: false,
    };
  }
  const { rank } = matchingScoreInfo || {};


  const localStorageKey = getMatchingLocalStorageKey({
    siteId,
    parentId,
    childId,
  });

  const showed = storageUtil.get(localStorageKey) === '1';
  if (showed) {
    return {
      showDialog: false,
    };
  }
  const canClaimPrizeList = getCanClaimPrizeList({
    prizeList,
  });

  // TODO: to release
  storageUtil.set(localStorageKey, '1');

  if (!canClaimPrizeList?.length) {
    return {
      showDialog: false,
    };
  }
  return {
    showDialog: true,
    rank,
    canClaimPrizeList,
  };
}
