2023-07-15 18:44:20 +00:00
|
|
|
import { Region } from '../../constants/regions';
|
2024-03-12 23:36:56 -05:00
|
|
|
import { ClanList } from '../../types/clanList';
|
2023-10-20 10:50:54 -05:00
|
|
|
import fetchBlitz from './fetchBlitz';
|
2023-05-16 02:02:50 +00:00
|
|
|
|
2023-10-14 11:15:42 -05:00
|
|
|
export default async function searchClansAcrossRegions(
|
|
|
|
|
search: string,
|
2023-11-01 11:45:19 -05:00
|
|
|
limit = 25,
|
2023-10-14 11:15:42 -05:00
|
|
|
) {
|
2023-05-20 01:22:20 +00:00
|
|
|
const trimmed = search.trim();
|
|
|
|
|
if (trimmed.length < 3 && trimmed.length > 100) return [];
|
2023-11-01 11:45:19 -05:00
|
|
|
const normalizedLimit = Math.floor(limit / 3);
|
|
|
|
|
|
|
|
|
|
// TODO: remove splice once wargaming fixes the functionality of the limit felid
|
2023-05-16 02:02:50 +00:00
|
|
|
|
2023-05-20 01:22:20 +00:00
|
|
|
return (
|
|
|
|
|
await Promise.all([
|
2024-04-15 13:19:25 -05:00
|
|
|
fetchBlitz<ClanList>('com', 'clans/list', {
|
|
|
|
|
search,
|
|
|
|
|
limit: normalizedLimit,
|
|
|
|
|
}).then((value) =>
|
2023-11-01 11:45:19 -05:00
|
|
|
value
|
|
|
|
|
.map((account) => ({
|
2023-05-16 02:02:50 +00:00
|
|
|
...account,
|
2023-09-07 03:24:07 +00:00
|
|
|
region: 'com' as Region,
|
2023-11-01 11:45:19 -05:00
|
|
|
}))
|
|
|
|
|
.splice(0, normalizedLimit),
|
2023-05-20 01:22:20 +00:00
|
|
|
),
|
2024-04-15 13:19:25 -05:00
|
|
|
fetchBlitz<ClanList>('eu', 'clans/list', {
|
|
|
|
|
search,
|
|
|
|
|
limit: normalizedLimit,
|
|
|
|
|
}).then((value) =>
|
2023-11-01 11:45:19 -05:00
|
|
|
value
|
|
|
|
|
.map((account) => ({
|
2023-05-16 02:02:50 +00:00
|
|
|
...account,
|
2023-09-07 03:24:07 +00:00
|
|
|
region: 'eu' as Region,
|
2023-11-01 11:45:19 -05:00
|
|
|
}))
|
|
|
|
|
.splice(0, normalizedLimit),
|
2023-05-20 01:22:20 +00:00
|
|
|
),
|
2024-04-15 13:19:25 -05:00
|
|
|
fetchBlitz<ClanList>('asia', 'clans/list', {
|
|
|
|
|
search,
|
|
|
|
|
limit: normalizedLimit,
|
|
|
|
|
}).then((value) =>
|
2023-11-01 11:45:19 -05:00
|
|
|
value
|
|
|
|
|
.map((account) => ({
|
2023-05-16 02:02:50 +00:00
|
|
|
...account,
|
2023-09-07 03:24:07 +00:00
|
|
|
region: 'asia' as Region,
|
2023-11-01 11:45:19 -05:00
|
|
|
}))
|
|
|
|
|
.splice(0, normalizedLimit),
|
2023-05-20 01:22:20 +00:00
|
|
|
),
|
|
|
|
|
])
|
|
|
|
|
)
|
|
|
|
|
.filter(Boolean)
|
|
|
|
|
.flat();
|
2023-05-16 02:02:50 +00:00
|
|
|
}
|