local ____lualib = require("lualib_bundle")
local __TS__StringReplace = ____lualib.__TS__StringReplace
local __TS__ParseInt = ____lualib.__TS__ParseInt
local __TS__ArrayFind = ____lualib.__TS__ArrayFind
local __TS__ArrayMap = ____lualib.__TS__ArrayMap
local __TS__ObjectAssign = ____lualib.__TS__ObjectAssign
local ____exports = {}
local createSnapPoints, createSnapPoint, createAssets, createAsset
local ____math = require("math")
local ____math = ____math.default
____exports.createBaseObject = function(properties, ____type)
    return {
        GUID = properties.guid or ____exports.createGuid(),
        Name = ____type,
        Nickname = properties.name,
        Description = properties.description,
        GMNotes = properties.gmNotes,
        Memo = properties.memo,
        Tags = properties.tags,
        Transform = ____exports.createTransform(properties),
        Locked = properties.locked,
        ColorDiffuse = properties.color and ____exports.createColor(properties.color) or nil,
        AltLookAngle = properties.viewAngle,
        Tooltip = properties.tooltip,
        CustomUIAssets = createAssets(properties),
        AttachedSnapPoints = createSnapPoints(properties),
        LuaScript = properties.script,
        XmlUI = properties.ui
    }
end
--- Creates a stated object from the given object data.
-- 
-- @param objects The list of objects to add as states. The order in this list will represent the state numbers.
-- @param startingState The index of `objects` of the object which should be the current object. Defaults to the first entry.
____exports.createStates = function(objects, startingState)
    if startingState == nil then
        startingState = 0
    end
    local baseObject = objects[startingState + 1]
    baseObject.States = {}
    do
        local i = 0
        while i < #objects do
            do
                local __continue4
                repeat
                    if i == startingState then
                        __continue4 = true
                        break
                    end
                    local stateId = i + 1
                    baseObject.States[stateId] = objects[i + 1]
                    __continue4 = true
                until true
                if not __continue4 then
                    break
                end
            end
            i = i + 1
        end
    end
    return baseObject
end
____exports.createGuid = function()
    return "123456"
end
____exports.createTransform = function(properties)
    local position = properties.position or ({x = 0, y = 0, z = 0})
    local rotation = properties.rotation or ({x = 0, y = 0, z = 0})
    local scale = properties.scale or 1
    if type(scale) == "number" then
        scale = {x = scale, y = scale, z = scale}
    end
    return {
        posX = position.x,
        posY = position.y,
        posZ = position.z,
        rotX = rotation.x,
        rotY = rotation.y,
        rotZ = rotation.z,
        scaleX = scale.x,
        scaleY = scale.y,
        scaleZ = scale.z
    }
end
____exports.createColor = function(color)
    if type(color) == "string" then
        color = __TS__StringReplace(color, "#", "")
        local r, g, b = string.sub(color, 1, 2), string.sub(color, 3, 4), string.sub(color, 5, 6)
        local a = #color > 6 and string.sub(color, 7) or nil
        color = {
            r = __TS__ParseInt(r, 16),
            g = __TS__ParseInt(g, 16),
            b = __TS__ParseInt(b, 16),
            a = a and __TS__ParseInt(a, 16) or nil
        }
    end
    if color.r > 1 or color.g > 1 or color.b > 1 then
        local function adjust(value)
            return ____math.round(value / 255, 4)
        end
        return {
            r = adjust(color.r),
            g = adjust(color.g),
            b = adjust(color.b),
            a = color.a and adjust(color.a) or nil
        }
    end
    return color
end
____exports.addAsset = function(object, asset)
    if object.CustomUIAssets == nil then
        object.CustomUIAssets = {}
    end
    local newAsset = createAsset(asset)
    if not __TS__ArrayFind(
        object.CustomUIAssets,
        function(____, a) return a.Name == newAsset.Name or a.URL == newAsset.URL end
    ) then
        local ____object_CustomUIAssets_0 = object.CustomUIAssets
        ____object_CustomUIAssets_0[#____object_CustomUIAssets_0 + 1] = newAsset
    end
end
createSnapPoints = function(properties)
    if not properties.snapPoints then
        return nil
    end
    return __TS__ArrayMap(
        properties.snapPoints,
        function(____, s) return createSnapPoint(s) end
    )
end
createSnapPoint = function(snapPoint)
    if snapPoint.position ~= nil then
        return {
            Position = __TS__ObjectAssign({}, snapPoint.position),
            Rotation = snapPoint.rotation and __TS__ObjectAssign({}, snapPoint.rotation) or nil,
            Tags = snapPoint.tags
        }
    end
    return {Position = snapPoint}
end
createAssets = function(properties)
    local ____opt_1 = properties.assets
    return ____opt_1 and __TS__ArrayMap(
        properties.assets,
        function(____, a) return createAsset(a) end
    )
end
createAsset = function(asset)
    if asset.image ~= nil then
        return {Type = 0, Name = asset.name, URL = asset.image}
    end
    return {Type = 1, Name = asset.name, URL = asset.assetBundle}
end
return ____exports
