1import Foundation
2
3/// A reusable answer to a `tokens`, `people` or `location` question: an
4/// activity, a person or a place. Responses refer to tokens by ID, so editing
5/// a token's text renames it throughout history, as Reporter did.
6struct Token: Hashable, Sendable {
7 /// Imported tokens keep Reporter's `uniqueIdentifier`, which Reporter
8 /// shared between every answer using the token. New tokens use UUIDs.
9 var id: String
10 /// The question whose answers the token belongs to.
11 var questionID: String
12 /// The name shown for the token. For a place picked from a directory and
13 /// never renamed, this is the directory's name at the time.
14 var text: String
15 /// Set for tokens of `location` questions.
16 var place: PlaceDetails?
17 /// How the token reads in a narrative, when that differs from its text:
18 /// an English form of a name written in kanji, or 'the park' for a place.
19 /// It's used as written, in place of the rules.
20 var narrativeForm: String?
21 /// When set, the token was merged into the token with this ID, and
22 /// answers referring to it should be read as referring to that token.
23 var mergedInto: String?
24 var archivedAt: Date?
25 var modifiedAt: Date
26}
27
28extension Token {
29 /// Whether the token is a place whose text is still a name a place
30 /// directory gave it. Imported Foursquare venues and some MapKit places
31 /// have no directory names, so their text is the directory's.
32 var isNamedByDirectory: Bool {
33 guard let place, place.externalID != nil else { return false }
34 return place.names.isEmpty || place.names.contains { $0.text == text }
35 }
36}
37
38struct PlaceDetails: Codable, Hashable, Sendable {
39 /// A representative coordinate for the place: the directory's coordinate,
40 /// or the centre of the answers given for it.
41 var coordinate: Coordinate?
42 /// Names from the directory, by language. The token's `text` is what the
43 /// user chose; these let the place be shown in the local language.
44 var names: [LocalisedName] = []
45 var externalID: ExternalPlaceID?
46}
47
48struct LocalisedName: Codable, Hashable, Sendable {
49 /// BCP 47 language tag, for example `ja` or `en`.
50 var language: String
51 var text: String
52}
53
54/// An identifier for a place in an external directory. Stored alongside the
55/// place's names and coordinate, never instead of them, so the record keeps
56/// its meaning if the directory goes away.
57struct ExternalPlaceID: Codable, Hashable, Sendable {
58 enum Source: String, Codable, Sendable {
59 /// Foursquare venue IDs, from Reporter.
60 case foursquare
61 /// `MKMapItem.Identifier` raw values.
62 case mapKit
63 }
64
65 var source: Source
66 var value: String
67}