Query for merge documents

I have below documents
1.
client::c1
{
“type”: “client”,
“versions”: [
“1.0”,
“1.1”,
“1.2”
]
}

client::c2
{
“type”: “client”,
“versions”: [
“1.0”
]
}

3.{
“type”: “version”,
“versions”: [
{
“major”: “1.0”,
“minor”: [
“1.0.1”,
“1.0.2”
]
},
{
“major”: “1.1”,
“minor”: [
“1.1.1”,
“1.1.2”
]
},
{
“major”: “1.3”,
“minor”: []
}
]
}

Expected output as below

[
{
“clientName”: “c1”,
“versions”: [
{
“type”: “major”,
“versionName”: “1.0”
},
{
“type”: “minor”,
“versionName”: “1.0.1”
},
{
“type”: “minor”,
“versionName”: “1.0.2”
},
{
“type”: “major”,
“versionName”: “1.1”
},
{
“type”: “minor”,
“versionName”: “1.1.1”
},
{
“type”: “major”,
“versionName”: “1.2”
}
]
},
{
“clientName”: “c2”,
“versions”: [
{
“type”: “major”,
“versionName”: “1.0”
}
]
}
]

INSERT INTO default VALUES ("client::c1",{ "type": "client", "versions": [ "1.0", "1.1", "1.2" ] });
INSERT INTO default VALUES ("client::c2", { "type": "client", "versions": [ "1.0" ] } );
INSERT INTO default VALUES ("verssions::c1", { "type": "version", "versions": [ { "major": "1.0", "minor": [ "1.0.1", "1.0.2" ] }, { "major": "1.1", "minor": [ "1.1.1", "1.1.2" ] }, { "major": "1.3", "minor": [] } ] });
SELECT
SUBSTR(META(d1).id,8) AS clientName,
ARRAY_FLATTEN(ARRAY (ARRAY ARRAY_CONCAT([{"type": "major","versionName":v}],
                                        ARRAY {"type": "minor","versionName":vm}
                                        FOR vm IN v1.minor END)
                    FOR v1 IN vers WHEN v1.major = v END)
               FOR v IN  d1.versions END,3) AS versions
FROM default AS d1
LET vers = (SELECT RAW d.versions FROM default AS d  WHERE d.type = "version")[0]
WHERE d1.type = "client";