dofile("../../support/aseutilities.lua")

local sprite <const> = app.site.sprite
if not sprite then return end
local activeLayer <const> = app.site.layer
if not activeLayer then return end

if activeLayer.isReference then
    app.alert {
        title = "Error",
        text = "Layer is already a reference."
    }
    return
end

-- Cache methods used in for loop.
local colorCopy <const> = AseUtilities.aseColorCopy
local tileMapToImage <const> = AseUtilities.tileMapToImage

-- Unpack relevant sprite properties.
local frObjs <const> = sprite.frames
local colorMode <const> = sprite.colorMode

-- Flatten a potential group.
local activeIsGroup <const> = activeLayer.isGroup
local srcLayer = activeLayer
if activeIsGroup then
    -- Not much can be done about the transactions
    -- generated by this function here, because
    -- doesn't seem like a good idea to reassign
    -- srcLayer within a transaction.
    -- srcLayer = AseUtilities.flattenGroup(
    --     sprite, activeLayer, frObjs,
    --     true, false, true, true)
    app.alert {
        title = "Error",
        text = "Group layers are not supported."
    }
    return
end

-- Unpack source layer properties.
local srcBlendMode <const> = srcLayer.blendMode
    or BlendMode.NORMAL
local srcColor <const> = colorCopy(srcLayer.color, "")
local srcIsTileMap <const> = srcLayer.isTilemap
local srcOpacity <const> = srcLayer.opacity or 255
local srcTileSet <const> = srcLayer.tileset

app.transaction("To Reference", function()
    app.command.NewLayer {
        reference = true,
    }
    local refLayer <const> = app.site.layer
    if refLayer and refLayer.isReference then
        local lenFrObjs <const> = #frObjs
        local i = 0
        while i < lenFrObjs do
            i = i + 1
            local frObj <const> = frObjs[i]
            local srcCel <const> = srcLayer:cel(frObj)
            if srcCel then
                local srcImg = srcCel.image
                if srcIsTileMap and srcTileSet then
                    srcImg = tileMapToImage(
                        srcImg, srcTileSet, colorMode)
                end

                local refCel <const> = sprite:newCel(
                    refLayer, frObj, srcImg,
                    srcCel.position)
                refCel.color = colorCopy(srcCel.color, "")
                refCel.data = srcCel.data
                refCel.opacity = srcCel.opacity
                refCel.zIndex = srcCel.zIndex
            end
        end

        refLayer.name = "Reference"
        refLayer.blendMode = srcBlendMode
        refLayer.color = srcColor
        refLayer.data = srcLayer.data
        refLayer.isContinuous = srcLayer.isContinuous
        refLayer.isEditable = srcLayer.isEditable
        refLayer.isVisible = srcLayer.isVisible
        refLayer.opacity = srcOpacity
        refLayer.parent = srcLayer.parent
        refLayer.stackIndex = srcLayer.stackIndex

        app.layer = refLayer
        sprite:deleteLayer(srcLayer)
        if activeIsGroup then
            sprite:deleteLayer(activeLayer)
        end
    end
end)

app.refresh()