import { gameData } from "../pubUtils/data";
import { ShopItemListParam } from '../domain/roleField/shop';
import { UserShopModel } from "../db/UserShop";

export async function getShopListById(shopId: number, roleId: string) {

    let shopItemList = new Array<ShopItemListParam>(); // 返回
    let userShopRecs = await UserShopModel.findMapByShopId(roleId, shopId);
    // console.log(JSON.stringify([...userShopRecs]))
    
    let dicShop = gameData.shop.get(shopId)||[];
    for(let { id, type } of dicShop) {
        let buyCount = userShopRecs.has(id)?userShopRecs.get(id).count: 0;

        let param = new ShopItemListParam(id, 1, type, buyCount, 0);
        shopItemList.push(param);
    }

    shopItemList = shopItemList.sort((a, b) => b.order - a.order);
    return shopItemList;
}

export async function getAllShopList(roleId: string) {
    let shopLists: {shopId: number, shopItemList: ShopItemListParam[]}[] = [];
    for(let [ shopId ] of gameData.shopList) {
        let shopItemList = await getShopListById(shopId, roleId);
        shopLists.push({ shopId, shopItemList });
    }
    return shopLists;
}