local g=game:GetService("\072\116\116\112\083\101\114\118\105\099\101")local n=require(script.Parent.Parent.TableUtils)local K={}function K.toRbx(n)n=type(n)=="\116\097\098\108\101"and n or g:JSONDecode(n)local function K(g)local n={}for g,f in pairs(g)do if f.integerValue then n[g]=tonumber(f.integerValue)elseif f.stringValue then n[g]=f.stringValue elseif f.booleanValue~=nil then n[g]=f.booleanValue elseif f.timestampValue then n[g]=DateTime.fromIsoDate(f.timestampValue)elseif f.arrayValue then n[g]=K(f.arrayValue.values)elseif f.mapValue then n[g]=K(f.mapValue.fields)end end return n end n=K(n)return n end function K.toFirestore(g)local function K(g,f)f=f or{fields={}}local k=f.fields or f.values for g,f in pairs(g)do if type(f)=="\110\117\109\098\101\114"then k[g]={integerValue=f}elseif type(f)=="\115\116\114\105\110\103"then k[g]={stringValue=f}elseif type(f)=="\098\111\111\108\101\097\110"then k[g]={booleanValue=f}elseif typeof(f)=="\068\097\116\101\084\105\109\101"then k[g]={timestampValue=f:ToIsoDate()}elseif type(f)=="\116\097\098\108\101"then if n.isArray(f)or n.isEmpty(f)then k[g]={arrayValue=K(f,{values={}})}else k[g]={mapValue=K(f)}end end end return f end return K(g)end return K
local HttpService = game:GetService("HttpService")

local TableUtils = require(script.Parent.Parent.TableUtils)

local Parser = {}

function Parser.toRbx(data)
    data = (type(data) == "table" and data or HttpService:JSONDecode(data))
    local function format(table)
        local newData = {}
        for k,v in pairs(table) do
            if v.integerValue then
                newData[k] = tonumber(v.integerValue)
            elseif v.stringValue then
                newData[k] = v.stringValue
            elseif v.booleanValue ~= nil then
                newData[k] = v.booleanValue
            elseif v.timestampValue then
                newData[k] = DateTime.fromIsoDate(v.timestampValue)
            elseif v.arrayValue then
                newData[k] = format(v.arrayValue.values)
            elseif v.mapValue then
                newData[k] = format(v.mapValue.fields)
            end
        end

        return newData
    end

    data = format(data)
    return data
end

function Parser.toFirestore(data)
    local function format(table, newData)
        newData = newData or {fields = {}}
        local fields = newData.fields or newData.values

        for k,v in pairs(table) do
            if type(v) == "number" then
                fields[k] = {integerValue = v}
            elseif type(v) == "string" then
                fields[k] = {stringValue = v}
            elseif type(v) == "boolean" then
                fields[k] = {booleanValue = v}
            elseif typeof(v) == "DateTime" then
                fields[k] = {timestampValue = v:ToIsoDate()}
            elseif type(v) == "table" then
                if TableUtils.isArray(v) or TableUtils.isEmpty(v) then
                    fields[k] = {arrayValue = format(v, {values = {}})}
                else
                    fields[k] = {mapValue = format(v)}
                end
            end
        end

        return newData
    end

    return format(data)
end

return Parser
