Mirror of https://github.com/roostorg/coop
github.com/roostorg/coop
1import { type ReadonlyDeep } from 'type-fest';
2
3import { type LocationArea } from '../types/locationArea.js';
4
5export enum MatchingValueType {
6 STRING = 'STRING',
7 TEXT_BANK = 'TEXT_BANK',
8 LOCATION = 'LOCATION',
9 LOCATION_BANK = 'LOCATION_BANK',
10 IMAGE_BANK = 'IMAGE_BANK',
11}
12
13// TODO: Eventually, we probably don't wanna allow undefined or null for each of
14// these keys, but these types reflect the reality of what we've historically
15// saved in the db.
16export type MatchingValues = {
17 strings?: readonly string[] | null;
18 textBankIds?: readonly string[] | null;
19 locations?: readonly ReadonlyDeep<LocationArea>[] | null;
20 locationBankIds?: readonly string[] | null;
21 imageBankIds?: readonly string[] | null;
22};
23
24export function getMatchingValuesType(matchingValues?: MatchingValues) {
25 if (!matchingValues) {
26 return undefined;
27 }
28 if (matchingValues.strings?.length) {
29 return MatchingValueType.STRING;
30 }
31 if (matchingValues.textBankIds?.length) {
32 return MatchingValueType.TEXT_BANK;
33 }
34 if (matchingValues.locations?.length) {
35 return MatchingValueType.LOCATION;
36 }
37 if (matchingValues.locationBankIds?.length) {
38 return MatchingValueType.LOCATION_BANK;
39 }
40 if (matchingValues.imageBankIds?.length) {
41 return MatchingValueType.IMAGE_BANK;
42 }
43 return undefined;
44}
45
46export function isLocationArea(
47 it: string | null | LocationArea,
48): it is LocationArea {
49 return typeof it === 'object' && it != null && 'geometry' in it;
50}