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

### Tag API
# Tag:apply_to_run()
[[patches]]
[patches.pattern]
target = "tag.lua"
pattern = "function Tag:apply_to_run(_context)"
position = 'after'
match_indent = true
payload = '''
    if self.triggered then return end
    local obj = SMODS.Tags[self.key]
    local res
    if obj and obj.apply and type(obj.apply) == 'function' then
        res = obj:apply(self, _context)
    end
    if res then return res end
'''

# Tag:set_ability()
[[patches]]
[patches.pattern]
target = "tag.lua"
pattern = "function Tag:set_ability()"
position = 'after'
match_indent = true
payload = '''
    local obj = SMODS.Tags[self.key]
    local res
    if obj and obj.set_ability and type(obj.set_ability) == 'function' then
        obj:set_ability(self)
    end
'''

# create_UIBox_your_collection_tags()
[[patches]]
[patches.regex]
target = "functions/UI_definitions.lua"
pattern = "(?<indent>[\t ]*)local tag_matrix = \\{(\n.*){6}"
position = 'at'
line_prepend = '$indent'
payload = '''
local tag_matrix = {}
local counter = 0
local tag_tab = {}
local tag_pool = {}
if G.ACTIVE_MOD_UI then
    for k, v in pairs(G.P_TAGS) do
        if v.mod and G.ACTIVE_MOD_UI.id == v.mod.id then tag_pool[k] = v end
    end
else
    tag_pool = G.P_TAGS
end
for k, v in pairs(tag_pool) do
    counter = counter + 1
    tag_tab[#tag_tab+1] = v
end
for i = 1, math.ceil(counter / 6) do
    table.insert(tag_matrix, {})
end'''

[[patches]]
[patches.regex]
target = "functions/UI_definitions.lua"
pattern = '''(?<indent>[\t ]*)v\.children\.alert\.states\.collide\.can = false\n[\s\S]{8}end\n[\s\S]{8}return true\n[\s\S]{4}end\)\n[\s\S]{2}\}\)\)\n{3}'''
position = 'after'
line_prepend = '$indent'
payload = '''
local table_nodes = {}
for i = 1, math.ceil(counter / 6) do
    table.insert(table_nodes, {n=G.UIT.R, config={align = "cm"}, nodes=tag_matrix[i]})
end'''

[[patches]]
[patches.regex]
target = "functions/UI_definitions.lua"
pattern = '''(?<indent>[\t ]*)\{\n[\s\S]{10}\{n=G\.UIT\.R, config=\{align = "cm"\}, nodes=tag_matrix\[1\]},[\s\S]*tag_matrix\[4\]\},\n[\s\S]{8}\}'''
position = 'at'
line_prepend = '$indent'
payload = '''table_nodes'''

# Tag:generate_UI()
[[patches]]
[patches.regex]
target = "tag.lua"
pattern = 'G.ASSET_ATLAS\["tags"\]'
position = 'at'
payload = 'G.ASSET_ATLAS[(not self.hide_ability) and G.P_TAGS[self.key].atlas or "tags"]'

# Tag:get_uibox_table()
[[patches]]
[patches.pattern]
target = "tag.lua"
pattern = '''tag_sprite.ability_UIBox_table = generate_card_ui(G.P_TAGS[self.key], nil, loc_vars, (self.hide_ability) and 'Undiscovered' or 'Tag', nil, (self.hide_ability))'''
position = 'at'
match_indent = true
payload = '''tag_sprite.ability_UIBox_table = generate_card_ui(G.P_TAGS[self.key], nil, loc_vars, (self.hide_ability) and 'Undiscovered' or 'Tag', nil, (self.hide_ability), nil, nil, self)'''
