# SPDX-FileCopyrightText: Stone Tickle <lattis@mochiro.moe>
# SPDX-License-Identifier: GPL-3.0-only

global = []

func identity(t int) -> int
    return t
endfunc

func append_global(t any)
    global += t
endfunc

func get_global() -> list[any]
    return global
endfunc

func basic_scope() -> int
    assert(not is_variable('a'))
    a = 1
    return a
endfunc

func factory(v any) -> function
    func f() -> any
        return v
    endfunc

    return f
endfunc

func incrementer() -> function
    i = 0
    func f() -> int
        r = i
        i += 1
        return r
    endfunc

    return f
endfunc

func custom_target_wrap(base_kwargs dict[any]) -> function
    func f(name str, kwargs dict[any]) -> custom_tgt
        kwargs = base_kwargs + kwargs
        return custom_target(name, kwargs: kwargs)
    endfunc

    return f
endfunc

func test_wrap(exe exe, base_kwargs dict[any]) -> function
    func f(name str, args listify[any], kwargs dict[any]: {})
        kwargs = base_kwargs + kwargs + {'args': args}
        test(name, exe, kwargs: kwargs)
    endfunc

    return f
endfunc

func access_globals() -> str
    return meson.project_name()
endfunc

func array_kwarg_default(bar list[any]: []) -> list[any]
    baz = []
    foreach b : bar
        baz += b
    endforeach
    return baz
endfunc

func get_type(a any) -> str
    return typeof(a)
endfunc

func listify_arg(a listify[any]) -> list[any]
    return a
endfunc

func glob_arg(a glob[any]) -> list[any]
    return a
endfunc

func typed_list(a list[str]) -> list[int]
    l = []
    foreach e : a
        l += e.to_int()
    endforeach

    return l
endfunc

func multi_return(k str) -> int
    d = {'a': 1, 'b': 2}
    if not d.has_key(k)
        return 0
    endif

    return d[k]
endfunc

func returns_string() -> str
    return '10'
endfunc

# this should return void and muon should not raise an error stating that
# it expected a return of 'str' due to the internal call to `returns_string`
func returns_void()
    val_str = returns_string()
    message(val_str)
endfunc

func project() -> dict[any]
    return meson.project()
endfunc

return {
    'identity': identity,
    'append_global': append_global,
    'get_global': get_global,
    'basic_scope': basic_scope,
    'factory': factory,
    'incrementer': incrementer,
    'custom_target_wrap': custom_target_wrap,
    'test_wrap': test_wrap,
    'access_globals': access_globals,
    'array_kwarg_default': array_kwarg_default,
    'get_type': get_type,
    'listify_arg': listify_arg,
    'glob_arg': glob_arg,
    'typed_list': typed_list,
    'multi_return': multi_return,
    'returns_void': returns_void,
    'project': project,
}
