Flatten a list inside a nested loop

I have the following variable

  api_access = {
    "test" = {
      role_names = [
        "Role1"
        "Role2
      ]
    }
  }

I need to iterate trough each element of the list, where I perform a lookup for the string to the the appropriate id. So I do this

locals {
  role_ids = {
    for app, ids in var.api_access : app => {
      for roleid in ids.role_names : "role_ids" => [
        //getroleid() which results in e.g. "00000000-0000-0000-0000-000000000001"
      ]...
    }
  }
}

So far the above gives me

+ role_ids = {
      + "test" = {
          + "role_ids" = [
              + [
                  + "00000000-0000-0000-0000-000000000001",
                ],
              + [
                  + "00000000-0000-0000-0000-000000000002",
                ],
            ]
        }
    }

I would get this:

+ role_ids = {
      + "test" = {
          + "role_ids" = [
              + [
                  + "00000000-0000-0000-0000-000000000001",
                  + "00000000-0000-0000-0000-000000000000",
                ],
            ]
        }
    }

I tried to flatten the role_ids as follows

      for roleid in ids.role_names : "role_ids" =>flatten([
        roleid
      ]...)

Which fails with

Call to function “flatten” failed: can only flatten lists, sets and tuples.

Ok ultimately I got the result I wanted, when I use this construct:

locals {
  role_ids = {
    for app, ids in var.api_access : app => {
      id = data.azuread_application.apps[app].client_id
      roleids = flatten([
        for roleid in ids.role_ids : [
          for el in data.azuread_application.apps[app].app_roles : el.id if el.display_name == roleid
        ]
      ])
    }
  }
}

Which will give me the following local

+ role_ids = {
      + "test" = {
          + id      = "00000000-0000-0000-0000-000000000001",
          + roleids = [
              + "00000000-0000-0000-0000-000000000001",
              + "00000000-0000-0000-0000-000000000002",
            ]
        }
    }