#include "ui_defs.lua"
#include "ui/helpers/platform.lua"

function Ui.resolveMVVM(name, namespace, vmArgs)
    local viewName
    local vmName 
    -- consider name as namespace if it's a table
    if type(name) == "table" then
        viewName = "View"
        vmName = "ViewModel"
        namespace = name
    else
        viewName = name .. "View"
        vmName = name .. "ViewModel"
    end

    local view = platformResolve(viewName, namespace)
    local vm = platformResolve(vmName, namespace)

    local viewObj
    local vmObj
    if type(view) == "function" then
        viewObj = view{}
    elseif type(view) == "table" then
        viewObj = view
    end

    if type(vm) == "function" then
        vmObj = vm{}
    elseif type(vm) == "table" then
        vmObj = vm
    end

    viewObj:setViewModel(vmObj)

    if vmArgs ~= nil then
        for k,v in pairs(vmArgs) do
            vmObj[k] = v
        end
    end

    return viewObj
end