"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CardsForDeckbuildingService = void 0;
const deckstrings_1 = require("@firestone-hs/deckstrings");
const public_api_1 = require("../public-api");
class CardsForDeckbuildingService {
    constructor() {
        this.duplicatesForDeckbuilding = null;
    }
    init(allCards) {
        var _a, _b, _c;
        const allSetsDuplicates = {};
        for (const card of allCards.getCards()) {
            if (!!((_a = card.counterpartCards) === null || _a === void 0 ? void 0 : _a.length) || card.deckDuplicateDbfId) {
                const rootId = (_c = (_b = card.counterpartCards) === null || _b === void 0 ? void 0 : _b[0]) !== null && _c !== void 0 ? _c : card.deckDuplicateDbfId;
                if (!allSetsDuplicates[rootId]) {
                    allSetsDuplicates[rootId] = [];
                }
                allSetsDuplicates[rootId].push(card);
            }
            else {
                if (!allSetsDuplicates[card.dbfId]) {
                    allSetsDuplicates[card.dbfId] = [];
                }
                allSetsDuplicates[card.dbfId].push(card);
            }
        }
        for (const dbfId of Object.keys(allSetsDuplicates)) {
            allSetsDuplicates[dbfId] = allSetsDuplicates[dbfId].sort((a, b) => a.dbfId - b.dbfId);
        }
        const formats = [
            'standard',
            'wild',
            'twist',
            'duels',
        ];
        const result = {};
        for (const format of formats) {
            result[format] = {};
            const allowedSets = this.getAllowedSets(format);
            for (const dbfId of Object.keys(allSetsDuplicates)) {
                const cards = allSetsDuplicates[dbfId];
                const card = cards.find((card) => { var _a; return allowedSets.includes((_a = card.set) === null || _a === void 0 ? void 0 : _a.toLowerCase()); });
                if (card) {
                    result[format][dbfId] = card;
                }
            }
        }
        this.duplicatesForDeckbuilding = result;
    }
    normalizeDeckList(decklist, allCards) {
        var _a, _b;
        const decoded = deckstrings_1.decode(decklist);
        const cards = decoded.cards.map((pair) => [
            this.getBaseCardDbfIdForDeckbuilding(pair[0], decoded.format, allCards),
            pair[1],
        ]);
        const sideboards = (_a = decoded.sideboards) === null || _a === void 0 ? void 0 : _a.map((sideboard) => this.normalizeSideboard(sideboard, decoded.format, allCards));
        const heroes = (_b = decoded.heroes) === null || _b === void 0 ? void 0 : _b.map((heroDbfId) => { var _a; return (_a = public_api_1.normalizeDeckHeroDbfId(heroDbfId, allCards)) !== null && _a !== void 0 ? _a : 7; });
        const updated = {
            ...decoded,
            heroes: heroes,
            cards: cards,
            sideboards: sideboards,
        };
        return deckstrings_1.encode(updated);
    }
    normalizeSideboard(sideboard, format, allCards) {
        const cards = sideboard.cards.map((pair) => [
            this.getBaseCardDbfIdForDeckbuilding(pair[0], format, allCards),
            pair[1],
        ]);
        return {
            ...sideboard,
            cards: cards,
        };
    }
    getBaseCardDbfIdForDeckbuilding(cardIdOrDbfId, format, allCards) {
        var _a, _b, _c, _d, _e;
        const refCard = allCards.getCard(cardIdOrDbfId);
        const deckDbfId = (_c = (_b = (_a = refCard.counterpartCards) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : refCard.deckDuplicateDbfId) !== null && _c !== void 0 ? _c : refCard.dbfId;
        return ((_e = (_d = this.duplicatesForDeckbuilding[public_api_1.formatFormat(format)][deckDbfId]) === null || _d === void 0 ? void 0 : _d.dbfId) !== null && _e !== void 0 ? _e : allCards.getCard(deckDbfId).dbfId);
    }
    getBaseCardIdForDeckbuilding(cardIdOrDbfId, format, allCards) {
        var _a, _b, _c, _d, _e;
        const refCard = allCards.getCard(cardIdOrDbfId);
        const deckDbfId = (_c = (_b = (_a = refCard.counterpartCards) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : refCard.deckDuplicateDbfId) !== null && _c !== void 0 ? _c : refCard.dbfId;
        return (_e = (_d = this.duplicatesForDeckbuilding[public_api_1.formatFormat(format)][deckDbfId]) === null || _d === void 0 ? void 0 : _d.id) !== null && _e !== void 0 ? _e : allCards.getCard(deckDbfId).id;
    }
    getAllowedSets(format) {
        switch (format) {
            case 'standard':
                return public_api_1.standardSets;
            case 'classic':
                return ['classic'];
            case 'twist':
                return public_api_1.twistSets;
            case 'duels':
                return public_api_1.duelsSets;
            case 'wild':
            default:
                return public_api_1.wildSets;
        }
    }
}
exports.CardsForDeckbuildingService = CardsForDeckbuildingService;
//# sourceMappingURL=cards-for-deckbuilding.service.js.map