local ____lualib = require("lualib_bundle")
local Map = ____lualib.Map
local __TS__New = ____lualib.__TS__New
local __TS__ObjectAssign = ____lualib.__TS__ObjectAssign
local __TS__ArrayMap = ____lualib.__TS__ArrayMap
local __TS__ArrayReverse = ____lualib.__TS__ArrayReverse
local __TS__Unpack = ____lualib.__TS__Unpack
local __TS__StringPadStart = ____lualib.__TS__StringPadStart
local __TS__Number = ____lualib.__TS__Number
local ____exports = {}
local getDeckId, getCardId
local ____baseObject = require("forge.baseObject")
local createBaseObject = ____baseObject.createBaseObject
local currentDeckId = 1337
local createdDeckIds = __TS__New(Map)
--- Sets the starting deck id that will be used.
-- This is useful if multiple processes create content for the same TTS save, since otherwise their deck ids would clash.
____exports.setCurrentDeckId = function(id)
    currentDeckId = id
    return currentDeckId
end
--- Creates a new deck with the given properties.
-- 
-- By default the `BackIsHidden` property is set to `true`.
____exports.createDeck = function(properties)
    local deckId = getDeckId(properties)
    local ____temp_1 = properties.type or 0
    local ____properties_front_2 = properties.front
    local ____properties_back_3 = properties.back
    local ____properties_width_4 = properties.width
    local ____properties_height_5 = properties.height
    local ____properties_uniqueBack_0 = properties.uniqueBack
    if ____properties_uniqueBack_0 == nil then
        ____properties_uniqueBack_0 = false
    end
    local customDeck = {[deckId] = {
        Type = ____temp_1,
        FaceURL = ____properties_front_2,
        BackURL = ____properties_back_3,
        NumWidth = ____properties_width_4,
        NumHeight = ____properties_height_5,
        BackIsHidden = true,
        UniqueBack = ____properties_uniqueBack_0
    }}
    local function createCard(card)
        return __TS__ObjectAssign(
            {},
            createBaseObject(card, "Card"),
            {
                CardID = getCardId(deckId, card),
                CustomDeck = customDeck
            }
        )
    end
    local cards = __TS__ArrayReverse(__TS__ArrayMap(
        properties.cards,
        function(____, c) return createCard(c) end
    ))
    local cardIds = __TS__ArrayMap(
        cards,
        function(____, c) return c.CardID end
    )
    return __TS__ObjectAssign(
        {},
        createBaseObject(properties, "DeckCustom"),
        {DeckIDs = cardIds, CustomDeck = customDeck, ContainedObjects = cards}
    )
end
--- Combines the given decks into a single deck. The result deck has the properties (name, tags, etc.) of the first deck in the list.
-- Only the cards of the other decks will be added to the first deck.
-- 
-- *NOTE*: This operation mutates the first deck!
____exports.combineDecks = function(decks)
    local baseDeck = decks[1]
    do
        local i = 1
        while i < #decks do
            local deck = decks[i + 1]
            local ____baseDeck_ContainedObjects_6 = baseDeck.ContainedObjects
            ____baseDeck_ContainedObjects_6[#____baseDeck_ContainedObjects_6 + 1] = __TS__Unpack(deck.ContainedObjects)
            local ____baseDeck_DeckIDs_7 = baseDeck.DeckIDs
            ____baseDeck_DeckIDs_7[#____baseDeck_DeckIDs_7 + 1] = __TS__Unpack(deck.DeckIDs)
            baseDeck.CustomDeck = __TS__ObjectAssign({}, baseDeck.CustomDeck, deck.CustomDeck)
            i = i + 1
        end
    end
    return baseDeck
end
getDeckId = function(deck)
    local existing = createdDeckIds:get(deck.front)
    if existing then
        return existing
    end
    createdDeckIds:set(deck.front, currentDeckId)
    currentDeckId = currentDeckId + 1
    return currentDeckId - 1
end
getCardId = function(deckId, card)
    local index = __TS__StringPadStart(
        tostring(card.index),
        2,
        "0"
    )
    return __TS__Number(tostring(deckId) .. index)
end
return ____exports
