[manifest]
version = "1.0.0"
dump_lua = true
priority = -10

### Back API

# Back:init()
[[patches]]
[patches.pattern]
target = 'back.lua'
pattern = "if not selected_back then selected_back = G.P_CENTERS.b_red end"
position = 'after'
match_indent = true
payload = "self.atlas = selected_back.unlocked and selected_back.atlas or nil"

# Back:change_to()
[[patches]]
[patches.pattern]
target = 'back.lua'
pattern = "if not new_back then new_back = G.P_CENTERS.b_red end"
position = 'after'
match_indent = true
payload = "self.atlas = new_back.unlocked and new_back.atlas or nil"

# G.FUNCS.change_viewed_back
[[patches]]
[patches.pattern]
target = 'functions/button_callbacks.lua'
pattern = "G.PROFILES[G.SETTINGS.profile].MEMORY.deck = args.to_val"
position = 'after'
match_indent = true
payload = '''
for key, val in pairs(G.sticker_card.area.cards) do
	val.children.back = false
	val:set_ability(val.config.center, true)
end'''

# Back:apply_to_run()
[[patches]]
[patches.pattern]
target = 'back.lua'
pattern = "function Back:apply_to_run()"
position = 'after'
match_indent = true
payload = '''
	local obj = self.effect.center
	if obj.apply and type(obj.apply) == 'function' then
		obj:apply()
	end'''

# Back:trigger_effect(args)
[[patches]]
[patches.pattern]
target = 'back.lua'
pattern = "if not args then return end"
position = 'after'
match_indent = true
payload = '''
	local obj = self.effect.center
	if obj.trigger_effect and type(obj.trigger_effect) == 'function' then
		local o = {obj:trigger_effect(args)}
		if o then return unpack(o) end
	end'''

## Additonal trigger_effect contexts
## calculate_joker
# card.lua
[[patches]]
[patches.pattern]
target = 'card.lua'
pattern = '''G.jokers.cards[i]:calculate_joker({remove_playing_cards = true, removed = destroyed_cards})
        end'''
position = 'after'
match_indent = true
payload = '''G.GAME.selected_back:trigger_effect({context = 'remove_playing_cards', removed = destroyed_cards})'''

[[patches]]
[patches.pattern]
target = 'card.lua'
pattern = '''G.jokers.cards[i]:calculate_joker({open_booster = true, card = self})
            end'''
position = 'after'
match_indent = true
payload = '''G.GAME.selected_back:trigger_effect({context = 'open_booster', card = self})'''

[[patches]]
[patches.pattern]
target = 'card.lua'
pattern = '''G.jokers.cards[i]:calculate_joker({buying_card = true, card = self})
        end'''
position = 'after'
match_indent = true
payload = '''G.GAME.selected_back:trigger_effect({context = 'buying_card', card = self})'''

# game.lua
[[patches]]
[patches.pattern]
target = 'game.lua'
pattern = '''G.jokers.cards[i]:calculate_joker({first_hand_drawn = true})
                    end'''
position = 'after'
match_indent = true
payload = '''G.GAME.selected_back:trigger_effect({context = 'first_hand_drawn'})'''

# functions/button_callbacks.lua
[[patches]]
[patches.pattern]
target = 'functions/button_callbacks.lua'
pattern = '''G.jokers.cards[i]:calculate_joker({using_consumeable = true, consumeable = card})
      end'''
position = 'after'
match_indent = true
payload = '''G.GAME.selected_back:trigger_effect({context = 'using_consumeable', consumeable = card})'''

[[patches]]
[patches.pattern]
target = 'functions/button_callbacks.lua'
pattern = '''G.jokers.cards[i]:calculate_joker({selling_card = true, card = card})
      end
    end'''
position = 'after'
match_indent = true
payload = '''G.GAME.selected_back:trigger_effect({context = 'selling_card', card = card})'''

[[patches]]
[patches.pattern]
target = 'functions/button_callbacks.lua'
pattern = '''G.jokers.cards[i]:calculate_joker({buying_card = true, card = c1})
          end'''
position = 'after'
match_indent = true
payload = '''G.GAME.selected_back:trigger_effect({context = 'buying_card', card = c1})'''

[[patches]]
[patches.pattern]
target = 'functions/button_callbacks.lua'
pattern = '''G.jokers.cards[i]:calculate_joker({ending_shop = true})
      end'''
position = 'after'
match_indent = true
payload = '''G.GAME.selected_back:trigger_effect({context = 'ending_shop'})'''

[[patches]]
[patches.pattern]
target = 'functions/button_callbacks.lua'
pattern = '''G.jokers.cards[i]:calculate_joker({skipping_booster = true})
    end'''
position = 'after'
match_indent = true
payload = '''G.GAME.selected_back:trigger_effect({context = 'skipping_booster'})'''

[[patches]]
[patches.pattern]
target = 'functions/button_callbacks.lua'
pattern = '''G.jokers.cards[i]:calculate_joker({skip_blind = true})
          end'''
position = 'after'
match_indent = true
payload = '''G.GAME.selected_back:trigger_effect({context = 'skip_blind'})'''

[[patches]]
[patches.pattern]
target = 'functions/button_callbacks.lua'
pattern = '''G.jokers.cards[i]:calculate_joker({reroll_shop = true})
            end'''
position = 'after'
match_indent = true
payload = '''G.GAME.selected_back:trigger_effect({context = 'reroll_shop'})'''

# functions/state_events.lua
[[patches]]
[patches.pattern]
target = 'functions/state_events.lua'
pattern = '''G.jokers.cards[i]:calculate_perishable()
            end'''
position = 'after'
match_indent = true
payload = '''G.GAME.selected_back:trigger_effect({context = 'end_of_round', game_over = game_over})'''

[[patches]]
[patches.pattern]
target = 'functions/state_events.lua'
pattern = '''G.jokers.cards[i]:calculate_joker({setting_blind = true, blind = G.GAME.round_resets.blind})
            end'''
position = 'after'
match_indent = true
payload = '''G.GAME.selected_back:trigger_effect({context = 'setting_blind', blind = G.GAME.round_resets.blind})'''

[[patches]]
[patches.pattern]
target = 'functions/state_events.lua'
pattern = '''card_eval_status_text(G.jokers.cards[j], 'jokers', nil, 1, nil, eval)
                end
            end'''
position = 'after'
match_indent = true
payload = '''G.GAME.selected_back:trigger_effect({context = 'discard', other_card =  G.hand.highlighted[i], full_hand = G.hand.highlighted})'''

## eval_card
# functions/state_events.lua
[[patches]]
[patches.pattern]
target = 'functions/state_events.lua'
pattern = '''level_up_hand(G.jokers.cards[i], text)
                end
            end
        end'''
position = 'after'
match_indent = true
payload = '''G.GAME.selected_back:trigger_effect({context = 'before_hand', full_hand = G.play.cards, scoring_hand = scoring_hand, scoring_name = text, poker_hands = poker_hands})'''

[[patches]]
[patches.pattern]
target = 'functions/state_events.lua'
pattern = '''local effects = eval_card(_card, {cardarea = G.jokers, full_hand = G.play.cards, scoring_hand = scoring_hand, scoring_name = text, poker_hands = poker_hands, joker_main = true})'''
position = 'after'
match_indent = true
payload = '''G.GAME.selected_back:trigger_effect({context = 'joker_main', joker =  _card, full_hand = G.play.cards, scoring_hand = scoring_hand, scoring_name = text, poker_hands = poker_hands})'''

[[patches]]
[patches.pattern]
target = 'functions/state_events.lua'
pattern = '''eval_card(G.jokers.cards[j], {cardarea = G.jokers, remove_playing_cards = true, removed = cards_destroyed})
        end'''
position = 'after'
match_indent = true
payload = '''G.GAME.selected_back:trigger_effect({context = 'remove_playing_cards', removed = cards_destroyed})'''

[[patches]]
[patches.pattern]
target = 'functions/state_events.lua'
pattern = '''end
    end

    }) end
'''
position = 'after'
match_indent = true
payload = '''G.GAME.selected_back:trigger_effect({context = 'after', full_hand = G.play.cards, scoring_hand = scoring_hand, scoring_name = text, poker_hands = poker_hands})'''

## Back:generate_UI

# Localization with `unlock` field in loc_txt, same as for Jokers
[[patches]]
[patches.pattern]
target = 'back.lua'
pattern = 'if not back_config.unlock_condition then'
position = 'at'
payload = '''
local localized_by_smods
local key_override
if back_config.locked_loc_vars and type(back_config.locked_loc_vars) == 'function' then
	local res = back_config:locked_loc_vars() or {}
	loc_args = res.vars or {}
	key_override = res.key
end
if G.localization.descriptions.Back[key_override or back_config.key].unlock_parsed then
	localize{type = 'unlocks', key = key_override or back_config.key, set = 'Back', nodes = loc_nodes, vars = loc_args}
	localized_by_smods = true
end
if not back_config.unlock_condition then'''
match_indent = true
[[patches]]
[patches.pattern]
target = 'back.lua'
pattern = '''localize{type = 'descriptions', key = 'demo_locked', set = "Other", nodes = loc_nodes, vars = loc_args}'''
position = 'at'
payload = '''
if not localized_by_smods then
	localize{type = 'descriptions', key = 'demo_locked', set = "Other", nodes = loc_nodes, vars = loc_args}
end'''
match_indent = true

[[patches]]
[patches.pattern]
target = 'back.lua'
pattern = 'loc_args = {other_name}'
position = 'at'
payload = 'loc_args = loc_args or {other_name}'
match_indent = true
[[patches]]
[patches.pattern]
target = 'back.lua'
pattern = 'loc_args = {tostring(back_config.unlock_condition.amount)}'
position = 'at'
payload = 'loc_args = loc_args or {tostring(back_config.unlock_condition.amount)}'
match_indent = true
[[patches]]
[patches.pattern]
target = 'back.lua'
pattern = 'loc_args = {other_name, colours = {get_stake_col(back_config.unlock_condition.stake)}}'
position = 'at'
payload = 'loc_args = loc_args or {other_name, colours = {get_stake_col(back_config.unlock_condition.stake)}}'
match_indent = true

[[patches]]
[patches.pattern]
target = 'back.lua'
pattern = "if name_to_check == 'Blue Deck'*"
position = 'at'
match_indent = true
payload = '''
local key_override
if back_config.loc_vars and type(back_config.loc_vars) == 'function' then
	local res = back_config:loc_vars() or {}
	loc_args = res.vars or {}
	key_override = res.key
elseif name_to_check == 'Blue Deck' then loc_args = {effect_config.hands}'''

[[patches]]
[patches.regex]
target = 'back.lua'
pattern = "key = back_config\\.key"
position = 'at'
payload = "key = key_override or back_config.key"
