Ariver
2026-05-18 a5cba8fa3153098d95acb20b6060fac704ecc489
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
import Foundation
import AppKit
 
// MARK: - Data Models
 
struct AppInfo: Identifiable, Hashable {
    var id: URL { path }
    let name: String
    let path: URL
    let tags: [String]
    let bundleIdentifier: String?
    let icon: NSImage  // Pre-loaded during background scan
    var isUncommon: Bool = false
    var note: String? = nil
 
    /// True if this is an Apple pre-installed app.
    var isAppleApp: Bool {
        if let bid = bundleIdentifier, bid.hasPrefix("com.apple.") { return true }
        return AppIndexer.isSystemAppPath(path.path)
    }
 
    func hash(into hasher: inout Hasher) { hasher.combine(path) }
    static func == (lhs: AppInfo, rhs: AppInfo) -> Bool { lhs.path == rhs.path }
}
 
struct TagGroup: Identifiable {
    var id: String { name }
    let name: String
    let apps: [AppInfo]
}
 
// MARK: - Tag Color Mapping
 
enum TagColor {
    static func nsColor(for index: Int) -> NSColor {
        switch index {
        case 0:  return NSColor.systemGray
        case 1:  return NSColor(red: 0.60, green: 0.60, blue: 0.62, alpha: 1.0)
        case 2:  return NSColor(red: 0.38, green: 0.74, blue: 0.35, alpha: 1.0)
        case 3:  return NSColor(red: 0.70, green: 0.45, blue: 0.79, alpha: 1.0)
        case 4:  return NSColor(red: 0.30, green: 0.64, blue: 0.96, alpha: 1.0)
        case 5:  return NSColor(red: 0.97, green: 0.83, blue: 0.29, alpha: 1.0)
        case 6:  return NSColor(red: 0.99, green: 0.38, blue: 0.36, alpha: 1.0)
        case 7:  return NSColor(red: 0.97, green: 0.58, blue: 0.27, alpha: 1.0)
        default: return NSColor.systemGray
        }
    }
    static let allIndices: [Int] = [1, 2, 3, 4, 5, 6, 7]
}
 
// MARK: - App Scanner
 
enum AppIndexer {
 
    static let searchPaths: [URL] = [
        URL(fileURLWithPath: "/Applications"),
        URL(fileURLWithPath: "/System/Applications"),
        URL(fileURLWithPath: "/System/Cryptexes/App/System/Applications"),
        URL(fileURLWithPath: "/System/Volumes/Preboot/Cryptexes/App/System/Applications"),
        FileManager.default.homeDirectoryForCurrentUser
            .appendingPathComponent("Applications")
    ]
 
    private static let systemAppPathPrefixes = [
        "/System/Applications/",
        "/System/Cryptexes/App/System/Applications/",
        "/System/Volumes/Preboot/Cryptexes/App/System/Applications/"
    ]
 
    static func isSystemAppPath(_ path: String) -> Bool {
        systemAppPathPrefixes.contains { path.hasPrefix($0) }
    }
 
    /// Scan all standard locations. Tags are annotated from TagDatabase by the caller.
    static func scan() -> [AppInfo] {
        var seenResolvedPaths = Set<String>()
        var apps: [AppInfo] = []
 
        for baseURL in searchPaths {
            scanDirectChildren(
                in: baseURL,
                apps: &apps,
                seenResolvedPaths: &seenResolvedPaths
            )
 
            guard let enumerator = FileManager.default.enumerator(
                at: baseURL,
                includingPropertiesForKeys: [.isDirectoryKey, .isSymbolicLinkKey],
                options: [.skipsHiddenFiles, .skipsPackageDescendants]
            ) else { continue }
 
            for case let url as URL in enumerator {
                appendAppIfNeeded(
                    at: url,
                    apps: &apps,
                    seenResolvedPaths: &seenResolvedPaths
                )
            }
        }
 
        return deduplicated(apps).sorted {
            $0.name.localizedStandardCompare($1.name) == .orderedAscending
        }
    }
 
    private static func scanDirectChildren(
        in baseURL: URL,
        apps: inout [AppInfo],
        seenResolvedPaths: inout Set<String>
    ) {
        guard let names = try? FileManager.default.contentsOfDirectory(atPath: baseURL.path) else { return }
 
        for name in names where !name.hasPrefix(".") {
            appendAppIfNeeded(
                at: baseURL.appendingPathComponent(name),
                apps: &apps,
                seenResolvedPaths: &seenResolvedPaths
            )
        }
    }
 
    private static func appendAppIfNeeded(
        at url: URL,
        apps: inout [AppInfo],
        seenResolvedPaths: inout Set<String>
    ) {
        guard url.pathExtension.lowercased() == "app" else { return }
 
        let displayURL = url.standardizedFileURL
        guard !isNestedInsideAppBundle(displayURL) else { return }
 
        let resolvedURL = displayURL.resolvingSymlinksInPath().standardizedFileURL
        guard !isNestedInsideAppBundle(resolvedURL) else { return }
        guard seenResolvedPaths.insert(resolvedURL.path).inserted else { return }
 
        let name = displayURL.deletingPathExtension().lastPathComponent
        let icon = NSWorkspace.shared.icon(forFile: displayURL.path)
        icon.size = NSSize(width: 96, height: 96)
 
        let bundleId = Bundle(url: displayURL)?.bundleIdentifier
            ?? Bundle(url: resolvedURL)?.bundleIdentifier
 
        apps.append(AppInfo(
            name: name,
            path: displayURL,
            tags: [],
            bundleIdentifier: bundleId,
            icon: icon
        ))
    }
 
    private static func isNestedInsideAppBundle(_ url: URL) -> Bool {
        let components = url.standardizedFileURL.pathComponents
        guard let lastAppIndex = components.lastIndex(where: { $0.lowercased().hasSuffix(".app") }) else {
            return false
        }
        return components[..<lastAppIndex].contains { $0.lowercased().hasSuffix(".app") }
    }
 
    private static func deduplicated(_ apps: [AppInfo]) -> [AppInfo] {
        apps.reduce(into: [String: AppInfo]()) { result, app in
            let identity = deduplicationIdentity(for: app)
            guard let existing = result[identity] else {
                result[identity] = app
                return
            }
            if appDeduplicationScore(app) > appDeduplicationScore(existing) {
                result[identity] = app
            }
        }
        .values
        .map { $0 }
    }
 
    private static func deduplicationIdentity(for app: AppInfo) -> String {
        if let bundleIdentifier = app.bundleIdentifier?.trimmingCharacters(in: .whitespacesAndNewlines),
           !bundleIdentifier.isEmpty {
            return "bundle:\(bundleIdentifier.lowercased())"
        }
        return "name:\(normalizedAppName(app.name))"
    }
 
    private static func normalizedAppName(_ name: String) -> String {
        name
            .folding(options: [.diacriticInsensitive, .caseInsensitive], locale: .current)
            .lowercased()
            .components(separatedBy: CharacterSet.alphanumerics.inverted)
            .filter { !$0.isEmpty }
            .joined(separator: "-")
    }
 
    private static func appDeduplicationScore(_ app: AppInfo) -> Int {
        let path = app.path.path
        var score = 0
 
        if path.hasPrefix("/Applications/") { score += 500 }
        if path.hasPrefix(FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent("Applications").path + "/") {
            score += 450
        }
        if path.hasPrefix("/System/Applications/") { score += 400 }
        if path.hasPrefix("/System/Cryptexes/") { score += 250 }
        if path.hasPrefix("/System/Volumes/Preboot/") { score += 200 }
        if app.bundleIdentifier?.isEmpty == false { score += 50 }
        if path.split(separator: "/").count <= 3 { score += 25 }
        if isSystemAppPath(path) { score -= 10 }
 
        return score
    }
 
    /// Group apps by their tags (from TagDatabase).
    /// Groups are sorted by tagOrder (user-defined), falling back to alpha.
    static func group(
        apps: [AppInfo],
        nameOverrides: [String: String] = [:],
        defaultGroupName: String = "Other",
        tagOrder: [String] = []
    ) -> [TagGroup] {
        let macCategory = "Mac自带"
        var dict: [String: [AppInfo]] = [:]
        var seenAppIDsByGroup: [String: Set<URL>] = [:]
 
        for app in apps {
            if app.isAppleApp {
                appendGroupedApp(app, to: macCategory, groups: &dict, seenAppIDsByGroup: &seenAppIDsByGroup)
            }
 
            if app.tags.isEmpty {
                if !app.isAppleApp {
                    appendGroupedApp(app, to: defaultGroupName, groups: &dict, seenAppIDsByGroup: &seenAppIDsByGroup)
                }
            } else {
                for tag in uniqueOrdered(app.tags) {
                    let displayName = nameOverrides[tag] ?? tag
                    guard displayName != macCategory else { continue }
                    appendGroupedApp(app, to: displayName, groups: &dict, seenAppIDsByGroup: &seenAppIDsByGroup)
                }
            }
        }
 
        // Build sort index from tagOrder: lower index = appears first
        var orderIndex: [String: Int] = [:]
        for (i, name) in tagOrder.enumerated() {
            orderIndex[name] = i
        }
 
        return dict
            .sorted { lhs, rhs in
                if lhs.key == macCategory { return false }
                if rhs.key == macCategory { return true }
                if lhs.key == defaultGroupName { return false }
                if rhs.key == defaultGroupName { return true }
                // Use custom order if both are in tagOrder
                let li = orderIndex[lhs.key] ?? Int.max
                let ri = orderIndex[rhs.key] ?? Int.max
                if li != ri { return li < ri }
                return lhs.key.localizedStandardCompare(rhs.key) == .orderedAscending
            }
            .map { TagGroup(name: $0.key, apps: $0.value) }
    }
 
    private static func appendGroupedApp(
        _ app: AppInfo,
        to groupName: String,
        groups: inout [String: [AppInfo]],
        seenAppIDsByGroup: inout [String: Set<URL>]
    ) {
        if seenAppIDsByGroup[groupName, default: []].insert(app.id).inserted {
            groups[groupName, default: []].append(app)
        }
    }
 
    private static func uniqueOrdered(_ values: [String]) -> [String] {
        var seen = Set<String>()
        return values.filter { seen.insert($0).inserted }
    }
}
 
enum TagDatabase {
    static let uncommonTagKey = "__system.uncommon"
    static let maxAppNoteLength = 80
    static let autoUncommonOpenThreshold = 100
 
    enum UncommonSource: String, Codable {
        case auto
        case manual
    }
 
    // MARK: Storage types
 
    struct TagDef: Codable, Equatable {
        var color: Int
        var systemCategoryID: SmartCategoryID? = nil
    }
 
    struct Store: Codable {
        var version: Int = 1
        var tags: [String: TagDef] = [:]
        var appTags: [String: [String]] = [:]  // path → tag names
        var tagOrder: [String] = []  // display order; empty → alpha sort
        var uncommonAppPaths: [String] = []  // special marker; does not affect normal groups
        var uncommonSources: [String: UncommonSource] = [:]  // current uncommon source: auto/manual
        var appOpenCounts: [String: Int] = [:]  // launches opened from TagLauncher
        var knownAppPaths: [String] = []  // baseline set to detect newly installed apps
        var appNotes: [String: String] = [:]  // path → user note; retained even if marker is removed
        var disabledSystemCategoryIDs: [SmartCategoryID] = []  // system categories the user deleted
        var smartStart: SmartStartState = SmartStartState()
        var categoryScheme: CategorySchemeState = CategorySchemeState()
 
        enum CodingKeys: String, CodingKey {
            case version
            case tags
            case appTags
            case tagOrder
            case uncommonAppPaths
            case uncommonSources
            case appOpenCounts
            case knownAppPaths
            case appNotes
            case disabledSystemCategoryIDs
            case smartStart
            case categoryScheme
        }
 
        init() {}
 
        init(from decoder: Decoder) throws {
            let container = try decoder.container(keyedBy: CodingKeys.self)
            version = try container.decodeIfPresent(Int.self, forKey: .version) ?? 1
            tags = try container.decodeIfPresent([String: TagDef].self, forKey: .tags) ?? [:]
            appTags = try container.decodeIfPresent([String: [String]].self, forKey: .appTags) ?? [:]
            tagOrder = try container.decodeIfPresent([String].self, forKey: .tagOrder) ?? []
            uncommonAppPaths = try container.decodeIfPresent([String].self, forKey: .uncommonAppPaths) ?? []
            uncommonSources = try container.decodeIfPresent([String: UncommonSource].self, forKey: .uncommonSources) ?? [:]
            appOpenCounts = try container.decodeIfPresent([String: Int].self, forKey: .appOpenCounts) ?? [:]
            knownAppPaths = try container.decodeIfPresent([String].self, forKey: .knownAppPaths) ?? []
            appNotes = try container.decodeIfPresent([String: String].self, forKey: .appNotes) ?? [:]
            disabledSystemCategoryIDs = try container.decodeIfPresent(
                [SmartCategoryID].self,
                forKey: .disabledSystemCategoryIDs
            ) ?? []
            smartStart = try container.decodeIfPresent(SmartStartState.self, forKey: .smartStart) ?? SmartStartState()
            categoryScheme = try container.decodeIfPresent(CategorySchemeState.self, forKey: .categoryScheme) ?? CategorySchemeState()
 
            for path in uncommonAppPaths where uncommonSources[path] == nil {
                uncommonSources[path] = .manual
            }
        }
 
        var hasUserTagAssignments: Bool {
            appTags.values.contains { !$0.isEmpty }
        }
    }
 
    struct SmartStartState: Codable, Equatable {
        var catalogVersion: Int = 0
        var lastRunAt: Date? = nil
        var lastAppliedAt: Date? = nil
        var lastSuggestionAt: Date? = nil
        var lastBackupPath: String? = nil
        var lastMode: String? = nil
        var lastMatchedAppCount: Int = 0
        var lastAssignedTagCount: Int = 0
    }
 
    struct CategorySchemeState: Codable, Equatable {
        var currentName: String? = nil
        var currentCreatedAt: Date? = nil
        var previousName: String? = nil
        var previousCreatedAt: Date? = nil
        var previousBackupPath: String? = nil
        var lastChangedAt: Date? = nil
    }
 
    // MARK: Paths
 
    private static var storeDir: URL {
        let dir = FileManager.default.homeDirectoryForCurrentUser
            .appendingPathComponent("Library/Application Support/Apptag")
        try? FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
        return dir
    }
 
    static var storeURL: URL { storeDir.appendingPathComponent("tags.json") }
 
    static var smartStartBackupDir: URL {
        let dir = storeDir.appendingPathComponent("SmartStartBackups")
        try? FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
        return dir
    }
 
    static var categorySchemeBackupDir: URL {
        let dir = storeDir.appendingPathComponent("CategorySchemeBackups")
        try? FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
        return dir
    }
 
    private static var legacyStoreURL: URL? {
        guard ProcessInfo.processInfo.environment["APP_SANDBOX_CONTAINER_ID"] != nil else {
            return nil
        }
 
        let home = FileManager.default.homeDirectoryForCurrentUser
        let bundleID = Bundle.main.bundleIdentifier ?? "com.apptag.launcher"
        let marker = "/Library/Containers/\(bundleID)/Data"
        guard let range = home.path.range(of: marker) else { return nil }
 
        let realHomePath = String(home.path[..<range.lowerBound])
        return URL(fileURLWithPath: realHomePath)
            .appendingPathComponent("Library/Application Support/Apptag/tags.json")
    }
 
    // MARK: Load / Save
 
    static func load() -> Store {
        migrateLegacyStoreIfNeeded()
        guard let data = try? Data(contentsOf: storeURL),
              let store = try? JSONDecoder().decode(Store.self, from: data)
        else { return Store() }
        return store
    }
 
    static func loadWithEnsuredCategoryScheme() -> Store {
        var store = load()
        if normalizeCategorySchemeMetadata(&store) {
            save(store)
        }
        return store
    }
 
    static func save(_ store: Store) {
        let encoder = JSONEncoder()
        encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
        guard let data = try? encoder.encode(store) else { return }
        try? data.write(to: storeURL, options: .atomic)
    }
 
    static func backup(_ store: Store, reason: String, in directory: URL? = nil) -> URL? {
        let formatter = ISO8601DateFormatter()
        formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
        let safeTimestamp = formatter.string(from: Date())
            .replacingOccurrences(of: ":", with: "-")
        let safeReason = reason
            .replacingOccurrences(of: "/", with: "-")
            .replacingOccurrences(of: " ", with: "-")
        let url = (directory ?? smartStartBackupDir)
            .appendingPathComponent("\(safeTimestamp)-\(safeReason).json")
 
        let encoder = JSONEncoder()
        encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
        guard let data = try? encoder.encode(store) else { return nil }
        do {
            try data.write(to: url, options: .atomic)
            return url
        } catch {
            return nil
        }
    }
 
    // MARK: Category Scheme Auto Snapshots
 
    private struct CategorySchemeFingerprint: Equatable {
        let tags: [String: TagDef]
        let appTags: [String: [String]]
        let tagOrder: [String]
        let uncommonAppPaths: [String]
        let uncommonSources: [String: UncommonSource]
        let disabledSystemCategoryIDs: [SmartCategoryID]
    }
 
    private static let categorySchemeBatchDebounceSeconds: TimeInterval = 90
    private static let categorySchemeRetentionSeconds: TimeInterval = 30 * 24 * 60 * 60
    private static let categorySchemeBatchQueue = DispatchQueue(label: "com.apptag.category-scheme-batch")
    private static var categorySchemeBatchActive = false
    private static var categorySchemeBatchResetWorkItem: DispatchWorkItem?
 
    static func saveUserCategorySchemeMutation(
        _ store: Store,
        previous previousStore: Store,
        reason: String
    ) {
        guard categorySchemeFingerprint(for: store) != categorySchemeFingerprint(for: previousStore) else {
            save(store)
            return
        }
 
        var nextStore = store
        recordAutomaticPreviousCategoryScheme(
            in: &nextStore,
            previous: previousStore,
            reason: reason,
            updateCurrentSchemeName: true
        )
        save(nextStore)
    }
 
    static func attachPreviousCategorySchemeSnapshot(
        to store: inout Store,
        previous previousStore: Store,
        reason: String,
        updateCurrentSchemeName: Bool
    ) {
        guard categorySchemeFingerprint(for: store) != categorySchemeFingerprint(for: previousStore) else { return }
        recordAutomaticPreviousCategoryScheme(
            in: &store,
            previous: previousStore,
            reason: reason,
            updateCurrentSchemeName: updateCurrentSchemeName
        )
    }
 
    static func flushPendingCategorySchemeBackupBatch() {
        categorySchemeBatchQueue.sync {
            categorySchemeBatchActive = false
            categorySchemeBatchResetWorkItem?.cancel()
            categorySchemeBatchResetWorkItem = nil
        }
        pruneOldCategorySchemeBackups()
    }
 
    private static func recordAutomaticPreviousCategoryScheme(
        in store: inout Store,
        previous previousStore: Store,
        reason: String,
        updateCurrentSchemeName: Bool
    ) {
        var normalizedPrevious = previousStore
        _ = normalizeCategorySchemeMetadata(&normalizedPrevious)
        let now = Date()
        let shouldCreatePreviousSnapshot = beginOrExtendCategorySchemeBatch()
 
        if shouldCreatePreviousSnapshot {
            let backupURL = backup(
                normalizedPrevious,
                reason: "auto-\(reason)",
                in: categorySchemeBackupDir
            )
            let previousCreatedAt = normalizedPrevious.categoryScheme.currentCreatedAt
                ?? normalizedPrevious.categoryScheme.lastChangedAt
                ?? now
            store.categoryScheme.previousName = normalizedPrevious.categoryScheme.currentName
                ?? schemeName(prefixKey: "scheme.beforeSmartStart", at: previousCreatedAt)
            store.categoryScheme.previousCreatedAt = previousCreatedAt
            store.categoryScheme.previousBackupPath = backupURL?.path
        }
 
        if updateCurrentSchemeName {
            store.categoryScheme.currentCreatedAt = now
            store.categoryScheme.currentName = schemeName(prefixKey: "scheme.local", at: now)
        }
        store.categoryScheme.lastChangedAt = now
        pruneOldCategorySchemeBackups()
    }
 
    private static func beginOrExtendCategorySchemeBatch() -> Bool {
        categorySchemeBatchQueue.sync {
            let isFirstChangeInBatch = !categorySchemeBatchActive
            categorySchemeBatchActive = true
            categorySchemeBatchResetWorkItem?.cancel()
 
            let workItem = DispatchWorkItem {
                categorySchemeBatchQueue.sync {
                    categorySchemeBatchActive = false
                    categorySchemeBatchResetWorkItem = nil
                }
                pruneOldCategorySchemeBackups()
            }
            categorySchemeBatchResetWorkItem = workItem
            DispatchQueue.main.asyncAfter(
                deadline: .now() + categorySchemeBatchDebounceSeconds,
                execute: workItem
            )
            return isFirstChangeInBatch
        }
    }
 
    private static func categorySchemeFingerprint(for store: Store) -> CategorySchemeFingerprint {
        CategorySchemeFingerprint(
            tags: store.tags,
            appTags: store.appTags.mapValues(uniqueOrdered),
            tagOrder: uniqueOrdered(store.tagOrder),
            uncommonAppPaths: store.uncommonAppPaths.sorted(),
            uncommonSources: store.uncommonSources,
            disabledSystemCategoryIDs: store.disabledSystemCategoryIDs
        )
    }
 
    private static func pruneOldCategorySchemeBackups(now: Date = Date()) {
        let fm = FileManager.default
        let cutoff = now.addingTimeInterval(-categorySchemeRetentionSeconds)
        guard let urls = try? fm.contentsOfDirectory(
            at: categorySchemeBackupDir,
            includingPropertiesForKeys: [.creationDateKey, .contentModificationDateKey],
            options: [.skipsHiddenFiles]
        ) else { return }
 
        for url in urls where url.pathExtension.lowercased() == "json" {
            let values = try? url.resourceValues(forKeys: [.creationDateKey, .contentModificationDateKey])
            let date = values?.creationDate ?? values?.contentModificationDate ?? .distantFuture
            if date < cutoff {
                try? fm.removeItem(at: url)
            }
        }
    }
 
    static func restore(fromBackupAt path: String) -> Bool {
        let url = URL(fileURLWithPath: path)
        guard let data = try? Data(contentsOf: url),
              let store = try? JSONDecoder().decode(Store.self, from: data)
        else { return false }
        save(store)
        return true
    }
 
    /// App Store sandbox builds read Application Support inside the app container.
    /// Older non-sandbox builds stored the same database directly under ~/Library.
    /// On first sandbox launch, migrate the richer legacy database before seeding defaults.
    static func migrateLegacyStoreIfNeeded() {
        let fm = FileManager.default
        guard let legacyURL = legacyStoreURL,
              fm.fileExists(atPath: legacyURL.path)
        else { return }
 
        guard let legacyData = try? Data(contentsOf: legacyURL),
              let legacyStore = try? JSONDecoder().decode(Store.self, from: legacyData)
        else { return }
 
        if let currentData = try? Data(contentsOf: storeURL),
           let currentStore = try? JSONDecoder().decode(Store.self, from: currentData),
           storeScore(currentStore) >= storeScore(legacyStore) {
            return
        }
 
        try? fm.createDirectory(at: storeDir, withIntermediateDirectories: true)
        try? legacyData.write(to: storeURL, options: .atomic)
    }
 
    private static func storeScore(_ store: Store) -> Int {
        store.appTags.count * 100 + store.tagOrder.count * 10 + store.tags.count
    }
 
    // MARK: Localization
 
    @discardableResult
    static func relocalizeSystemTagsForCurrentLanguage() -> Bool {
        var store = load()
        guard relocalizeSystemTags(in: &store) else { return false }
        save(store)
        return true
    }
 
    private static func relocalizeSystemTags(in store: inout Store) -> Bool {
        var renameMap: [String: String] = [:]
 
        for (tagName, tagDef) in store.tags {
            guard let categoryID = tagDef.systemCategoryID else { continue }
            let targetName = categoryID.localizedDisplayName
            guard targetName != tagName else { continue }
            if let existing = store.tags[targetName],
               existing.systemCategoryID != categoryID {
                continue
            }
            renameMap[tagName] = targetName
        }
 
        guard !renameMap.isEmpty else { return false }
        for (oldName, newName) in renameMap {
            renameTagKey(in: &store, from: oldName, to: newName)
        }
        return true
    }
 
    static func systemTagName(for categoryID: SmartCategoryID, in store: Store) -> String? {
        store.tags.first { $0.value.systemCategoryID == categoryID }?.key
    }
 
    @discardableResult
    static func ensureSystemTag(
        for categoryID: SmartCategoryID,
        in store: inout Store
    ) -> (name: String, created: Bool)? {
        if let existingName = systemTagName(for: categoryID, in: store) {
            let targetName = categoryID.localizedDisplayName
            if existingName != targetName {
                if let existing = store.tags[targetName],
                   existing.systemCategoryID != categoryID {
                    return (existingName, false)
                }
                renameTagKey(in: &store, from: existingName, to: targetName)
                return (targetName, false)
            }
            if !store.tagOrder.contains(existingName) {
                store.tagOrder.append(existingName)
            }
            return (existingName, false)
        }
 
        let targetName = categoryID.localizedDisplayName
        if let existing = store.tags[targetName],
           existing.systemCategoryID != categoryID {
            return nil
        }
 
        store.tags[targetName] = TagDef(
            color: categoryID.defaultColorIndex,
            systemCategoryID: categoryID
        )
        if !store.tagOrder.contains(targetName) {
            store.tagOrder.append(targetName)
        }
        return (targetName, true)
    }
 
    private static func renameTagKey(in store: inout Store, from oldName: String, to newName: String) {
        guard oldName != newName, let tagDef = store.tags.removeValue(forKey: oldName) else { return }
        store.tags[newName] = tagDef
        store.tagOrder = uniqueOrdered(store.tagOrder.map { $0 == oldName ? newName : $0 })
 
        for (path, tags) in store.appTags {
            let renamedTags = uniqueOrdered(tags.map { $0 == oldName ? newName : $0 })
            if renamedTags.isEmpty {
                store.appTags.removeValue(forKey: path)
            } else {
                store.appTags[path] = renamedTags
            }
        }
    }
 
    private static func uniqueOrdered(_ values: [String]) -> [String] {
        var seen = Set<String>()
        return values.filter { seen.insert($0).inserted }
    }
 
    // MARK: Export / Import
 
    static func exportTo(_ url: URL) throws {
        migrateLegacyStoreIfNeeded()
        let store = loadWithEnsuredCategoryScheme()
        let data = try JSONEncoder().encode(store)
        try data.write(to: url, options: .atomic)
    }
 
    static func importFrom(_ url: URL) throws -> Store {
        let previousStore = loadWithEnsuredCategoryScheme()
        let data = try Data(contentsOf: url)
        var store = try JSONDecoder().decode(Store.self, from: data)
        applyImportedCategorySchemeMetadata(to: &store, importedFileURL: url)
        attachPreviousCategorySchemeSnapshot(
            to: &store,
            previous: previousStore,
            reason: "manual-import",
            updateCurrentSchemeName: false
        )
        flushPendingCategorySchemeBackupBatch()
        save(store)
        return store
    }
 
    private static func applyImportedCategorySchemeMetadata(to store: inout Store, importedFileURL url: URL) {
        let importedCreatedAt = importedSchemeCreatedAt(from: url)
        store.categoryScheme.currentCreatedAt = importedCreatedAt
        store.categoryScheme.currentName = schemeName(prefixKey: "scheme.local", at: importedCreatedAt)
        store.categoryScheme.lastChangedAt = Date()
    }
 
    private static func importedSchemeCreatedAt(from url: URL) -> Date {
        let filename = url.deletingPathExtension().lastPathComponent
        if let timestampDate = schemeTimestampDate(in: filename) {
            return timestampDate
        }
 
        if let values = try? url.resourceValues(forKeys: [.creationDateKey, .contentModificationDateKey]) {
            return values.creationDate ?? values.contentModificationDate ?? Date()
        }
 
        return Date()
    }
 
    private static func schemeTimestampDate(in text: String) -> Date? {
        let pattern = #"(?<!\d)\d{4}\.\d{4}\.\d{4}(?!\d)"#
        guard let range = text.range(of: pattern, options: .regularExpression) else { return nil }
 
        let formatter = DateFormatter()
        formatter.locale = Locale(identifier: "en_US_POSIX")
        formatter.calendar = Calendar(identifier: .gregorian)
        formatter.timeZone = TimeZone.current
        formatter.dateFormat = "yyyy.MMdd.HHmm"
        formatter.isLenient = false
        return formatter.date(from: String(text[range]))
    }
 
    @discardableResult
    static func normalizeCategorySchemeMetadata(_ store: inout Store) -> Bool {
        var changed = false
        if store.categoryScheme.currentCreatedAt == nil {
            store.categoryScheme.currentCreatedAt = store.categoryScheme.lastChangedAt
                ?? store.smartStart.lastAppliedAt
                ?? store.smartStart.lastRunAt
                ?? Date()
            changed = true
        }
 
        if let createdAt = store.categoryScheme.currentCreatedAt {
            let prefixKey = store.smartStart.lastAppliedAt == nil ? "scheme.local" : "scheme.smartStart"
            let normalizedName = normalizedSchemeName(
                storedName: store.categoryScheme.currentName,
                createdAt: createdAt,
                fallbackPrefixKey: prefixKey
            )
            if store.categoryScheme.currentName != normalizedName {
                store.categoryScheme.currentName = normalizedName
                changed = true
            }
        } else if store.categoryScheme.currentName == nil {
            changed = true
        }
 
        if store.categoryScheme.lastChangedAt == nil {
            store.categoryScheme.lastChangedAt = store.categoryScheme.currentCreatedAt
            changed = true
        }
 
        if store.categoryScheme.previousName == nil,
           store.categoryScheme.previousBackupPath != nil {
            let createdAt = store.categoryScheme.previousCreatedAt
                ?? store.categoryScheme.currentCreatedAt
                ?? Date()
            store.categoryScheme.previousCreatedAt = createdAt
            store.categoryScheme.previousName = normalizedSchemeName(
                storedName: store.categoryScheme.previousName,
                createdAt: createdAt,
                fallbackPrefixKey: "scheme.beforeSmartStart"
            )
            changed = true
        } else if let previousCreatedAt = store.categoryScheme.previousCreatedAt,
                  let previousName = store.categoryScheme.previousName {
            let normalizedName = normalizedSchemeName(
                storedName: previousName,
                createdAt: previousCreatedAt,
                fallbackPrefixKey: "scheme.beforeSmartStart"
            )
            if store.categoryScheme.previousName != normalizedName {
                store.categoryScheme.previousName = normalizedName
                changed = true
            }
        }
 
        return changed
    }
 
    static func schemeName(prefixKey: String, at date: Date) -> String {
        "\(tr(prefixKey)) \(schemeTimestamp(at: date))"
    }
 
    static func schemeTimestamp(at date: Date) -> String {
        let formatter = DateFormatter()
        formatter.locale = Locale(identifier: "en_US_POSIX")
        formatter.calendar = Calendar(identifier: .gregorian)
        formatter.timeZone = TimeZone.current
        formatter.dateFormat = "yyyy.MMdd.HHmm"
        return formatter.string(from: date)
    }
 
    static func exportFileName(for scheme: CategorySchemeState) -> String {
        let fallbackDate = scheme.currentCreatedAt ?? scheme.lastChangedAt ?? Date()
        let baseName = normalizedSchemeName(
            storedName: scheme.currentName,
            createdAt: fallbackDate,
            fallbackPrefixKey: "scheme.local"
        )
        return "\(safeFileName(baseName)).json"
    }
 
    static func normalizedSchemeName(
        storedName: String?,
        createdAt: Date,
        fallbackPrefixKey: String
    ) -> String {
        let prefix = schemePrefix(from: storedName) ?? tr(fallbackPrefixKey)
        return "\(prefix) \(schemeTimestamp(at: createdAt))"
    }
 
    private static func schemePrefix(from storedName: String?) -> String? {
        guard let storedName else { return nil }
        let trimmed = storedName.trimmingCharacters(in: .whitespacesAndNewlines)
        guard !trimmed.isEmpty else { return nil }
 
        let prefixKeys = ["scheme.local", "scheme.smartStart", "scheme.systemSmartStart", "scheme.beforeSmartStart"]
        for key in prefixKeys {
            if trimmed.hasPrefix(tr(key)) {
                return tr(key)
            }
            for language in L10n.supported {
                if let localized = L10n.loadedTranslation(key, for: language.code),
                   !localized.isEmpty,
                   trimmed.hasPrefix(localized) {
                    return tr(key)
                }
            }
        }
 
        if let firstNumber = trimmed.firstIndex(where: { $0.isNumber }) {
            let prefix = String(trimmed[..<firstNumber])
                .trimmingCharacters(in: .whitespacesAndNewlines)
            if !prefix.isEmpty { return prefix }
        }
 
        return trimmed
    }
 
    private static func safeFileName(_ value: String) -> String {
        let invalid = CharacterSet(charactersIn: "/\\?%*|\"<>:\n\r\t")
        let parts = value.components(separatedBy: invalid)
            .map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
            .filter { !$0.isEmpty }
        let joined = parts.joined(separator: "-")
        return joined.isEmpty ? "TagLauncher-tags" : joined
    }
 
    /// Seed starter system tags on first launch. Smart Start adds any additional
    /// system tags it needs after scanning the user's installed apps.
    static func seedDefaultTags() {
        migrateLegacyStoreIfNeeded()
        guard !FileManager.default.fileExists(atPath: storeURL.path) else { return }
 
        let starterCategoryIDs: [SmartCategoryID] = [
            .design,
            .development,
            .writing,
            .game,
            .entertainment,
            .system,
            .productivity
        ]
 
        var store = Store()
        for categoryID in starterCategoryIDs {
            _ = ensureSystemTag(for: categoryID, in: &store)
        }
        save(store)
    }
}
 
// MARK: - Tag Editor (CRUD against local TagDatabase)
 
enum TagEditor {
 
    // MARK: Read
 
    /// Get tag→color map from the database.
    static func tagColors() -> [String: Int] {
        let store = TagDatabase.load()
        return store.tags.mapValues { $0.color }
    }
 
    /// Tag names in display order. Falls back to alpha sort if no custom order.
    static func orderedTagNames() -> [String] {
        let store = TagDatabase.load()
        let ordered = store.tagOrder.filter { store.tags[$0] != nil }
        let remaining = store.tags.keys.filter { !ordered.contains($0) }.sorted()
        return ordered + remaining
    }
 
    /// Persist a new tag display order.
    static func reorderTags(_ names: [String]) {
        var store = TagDatabase.load()
        let previousStore = store
        store.tagOrder = names
        TagDatabase.saveUserCategorySchemeMutation(
            store,
            previous: previousStore,
            reason: "reorder-tags"
        )
    }
 
    /// Create a new tag definition (no app assignments yet).
    static func createTag(_ name: String, color: Int) {
        var store = TagDatabase.load()
        guard store.tags[name] == nil else { return }
        let previousStore = store
        store.tags[name] = TagDatabase.TagDef(color: color, systemCategoryID: nil)
        if !store.tagOrder.contains(name) { store.tagOrder.insert(name, at: 0) }
        TagDatabase.saveUserCategorySchemeMutation(
            store,
            previous: previousStore,
            reason: "create-tag"
        )
    }
 
    /// Annotate scanned apps with tags from the database.
    static func annotate(apps: [AppInfo]) -> [AppInfo] {
        annotate(apps: apps, store: TagDatabase.load())
    }
 
    static func annotate(apps: [AppInfo], store: TagDatabase.Store) -> [AppInfo] {
        let uncommonPaths = Set(store.uncommonAppPaths)
        return apps.map { app in
            let appTags = store.appTags[app.path.path] ?? []
            return AppInfo(
                name: app.name, path: app.path, tags: appTags,
                bundleIdentifier: app.bundleIdentifier, icon: app.icon,
                isUncommon: uncommonPaths.contains(app.path.path),
                note: store.appNotes[app.path.path]
            )
        }
    }
 
    // MARK: Write
 
    /// Assign a tag to multiple apps. Preserves existing tags.
    static func assignTag(_ tag: String, color: Int, to paths: [String]) {
        var store = TagDatabase.load()
        let previousStore = store
        // Ensure tag definition exists
        if store.tags[tag] == nil {
            store.tags[tag] = TagDatabase.TagDef(color: color, systemCategoryID: nil)
            // New tag: append to display order
            if !store.tagOrder.contains(tag) { store.tagOrder.insert(tag, at: 0) }
        }
        for path in paths {
            var current = store.appTags[path] ?? []
            if !current.contains(tag) {
                current.append(tag)
                store.appTags[path] = current
            }
        }
        TagDatabase.saveUserCategorySchemeMutation(
            store,
            previous: previousStore,
            reason: "assign-tag"
        )
    }
 
    /// Replace the full editable tag set for multiple apps.
    static func setTags(_ tags: [String], to paths: [String]) {
        var store = TagDatabase.load()
        let previousStore = store
        let selectedUncommon = tags.contains(TagDatabase.uncommonTagKey)
        let validTags = tags.filter { store.tags[$0] != nil }
        var uncommonPaths = Set(store.uncommonAppPaths)
        for path in paths {
            if validTags.isEmpty {
                store.appTags.removeValue(forKey: path)
            } else {
                store.appTags[path] = validTags
            }
            if selectedUncommon {
                uncommonPaths.insert(path)
                store.uncommonSources[path] = .manual
            } else {
                uncommonPaths.remove(path)
                store.uncommonSources.removeValue(forKey: path)
            }
        }
        store.uncommonAppPaths = uncommonPaths.sorted()
        TagDatabase.saveUserCategorySchemeMutation(
            store,
            previous: previousStore,
            reason: "set-tags"
        )
    }
 
    /// Append tags to multiple apps without disturbing their existing tag sets.
    static func appendTags(_ tags: [String], to paths: [String]) {
        guard !tags.isEmpty, !paths.isEmpty else { return }
 
        var store = TagDatabase.load()
        let previousStore = store
        let selectedUncommon = tags.contains(TagDatabase.uncommonTagKey)
        let validTags = tags.filter { store.tags[$0] != nil }
        var uncommonPaths = Set(store.uncommonAppPaths)
 
        for path in paths {
            var current = store.appTags[path] ?? []
            for tag in validTags where !current.contains(tag) {
                current.append(tag)
            }
            if !current.isEmpty {
                store.appTags[path] = current
            }
 
            if selectedUncommon {
                uncommonPaths.insert(path)
                store.uncommonSources[path] = .manual
            }
        }
 
        store.uncommonAppPaths = uncommonPaths.sorted()
        TagDatabase.saveUserCategorySchemeMutation(
            store,
            previous: previousStore,
            reason: "append-tags"
        )
    }
 
    /// Remove tags from multiple apps while preserving every unrelated tag.
    static func removeTags(_ tags: [String], from paths: [String]) {
        guard !tags.isEmpty, !paths.isEmpty else { return }
 
        var store = TagDatabase.load()
        let previousStore = store
        let selectedUncommon = tags.contains(TagDatabase.uncommonTagKey)
        let validTags = Set(tags.filter { store.tags[$0] != nil })
        var uncommonPaths = Set(store.uncommonAppPaths)
 
        for path in paths {
            var current = store.appTags[path] ?? []
            current.removeAll { validTags.contains($0) }
 
            if current.isEmpty {
                store.appTags.removeValue(forKey: path)
            } else {
                store.appTags[path] = current
            }
 
            if selectedUncommon {
                uncommonPaths.remove(path)
                store.uncommonSources.removeValue(forKey: path)
            }
        }
 
        store.uncommonAppPaths = uncommonPaths.sorted()
        TagDatabase.saveUserCategorySchemeMutation(
            store,
            previous: previousStore,
            reason: "remove-tags"
        )
    }
 
    static func reconcileScannedApps(_ apps: [AppInfo]) -> TagDatabase.Store {
        var store = TagDatabase.load()
        let scannedPaths = Set(apps.map { $0.path.path })
        let knownPaths = Set(store.knownAppPaths)
 
        if store.knownAppPaths.isEmpty {
            let seededDefaultNotes = seedDefaultAppleAppNotes(for: apps, in: &store)
            let markedUncommon = markUnfamiliarAppleAppsAsUncommon(apps, in: &store)
            store.knownAppPaths = scannedPaths.sorted()
            if apps.isEmpty == false || seededDefaultNotes || markedUncommon {
                TagDatabase.save(store)
            }
            return store
        }
 
        let newPaths = scannedPaths.subtracting(knownPaths)
        guard !newPaths.isEmpty else { return store }
 
        var uncommonPaths = Set(store.uncommonAppPaths)
        let appsByPath = Dictionary(uniqueKeysWithValues: apps.map { ($0.path.path, $0) })
        for path in newPaths {
            if let app = appsByPath[path],
               app.isAppleApp,
               AppleDefaultAppNotes.isFamiliarAppleApp(app) {
                continue
            }
            uncommonPaths.insert(path)
            store.uncommonSources[path] = .auto
            if store.appOpenCounts[path] == nil {
                store.appOpenCounts[path] = 0
            }
        }
 
        let newApps = apps.filter { newPaths.contains($0.path.path) }
        _ = seedDefaultAppleAppNotes(for: newApps, in: &store)
        store.uncommonAppPaths = uncommonPaths.sorted()
        store.knownAppPaths = knownPaths.union(newPaths).sorted()
        TagDatabase.save(store)
        return store
    }
 
    private static func seedDefaultAppleAppNotes(
        for apps: [AppInfo],
        in store: inout TagDatabase.Store
    ) -> Bool {
        var changed = false
 
        for app in apps where app.isAppleApp {
            let path = app.path.path
            if store.appNotes[path]?.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty == false {
                continue
            }
 
            guard let note = AppleDefaultAppNotes.note(for: app) else { continue }
            store.appNotes[path] = String(note.prefix(TagDatabase.maxAppNoteLength))
            changed = true
        }
 
        return changed
    }
 
    private static func markUnfamiliarAppleAppsAsUncommon(
        _ apps: [AppInfo],
        in store: inout TagDatabase.Store
    ) -> Bool {
        var uncommonPaths = Set(store.uncommonAppPaths)
        var changed = false
 
        for app in apps where app.isAppleApp && !AppleDefaultAppNotes.isFamiliarAppleApp(app) {
            let path = app.path.path
            if uncommonPaths.insert(path).inserted {
                changed = true
            }
            if store.uncommonSources[path] == nil {
                store.uncommonSources[path] = .auto
                changed = true
            }
        }
 
        if changed {
            store.uncommonAppPaths = uncommonPaths.sorted()
        }
        return changed
    }
 
    static func recordLauncherOpen(for path: String) {
        var store = TagDatabase.load()
        store.appOpenCounts[path] = (store.appOpenCounts[path] ?? 0) + 1
 
        if store.uncommonSources[path] == .auto,
           store.appOpenCounts[path, default: 0] >= TagDatabase.autoUncommonOpenThreshold {
            var uncommonPaths = Set(store.uncommonAppPaths)
            uncommonPaths.remove(path)
            store.uncommonAppPaths = uncommonPaths.sorted()
            store.uncommonSources.removeValue(forKey: path)
        }
 
        TagDatabase.save(store)
    }
 
    static func setAppNote(_ note: String, for path: String) {
        var store = TagDatabase.load()
        let previousStore = store
        let trimmed = note.trimmingCharacters(in: .whitespacesAndNewlines)
        let limited = String(trimmed.prefix(TagDatabase.maxAppNoteLength))
        if limited.isEmpty {
            store.appNotes.removeValue(forKey: path)
        } else {
            store.appNotes[path] = limited
            var uncommonPaths = Set(store.uncommonAppPaths)
            if uncommonPaths.insert(path).inserted {
                store.uncommonAppPaths = uncommonPaths.sorted()
            }
            if store.uncommonSources[path] != .manual {
                store.uncommonSources[path] = .manual
            }
        }
        TagDatabase.saveUserCategorySchemeMutation(
            store,
            previous: previousStore,
            reason: "edit-app-note"
        )
    }
 
    static func moveApp(path: String, from sourceTag: String, to targetTag: String, color: Int, copy: Bool) {
        var store = TagDatabase.load()
        let previousStore = store
        if store.tags[targetTag] == nil {
            store.tags[targetTag] = TagDatabase.TagDef(color: color, systemCategoryID: nil)
            if !store.tagOrder.contains(targetTag) { store.tagOrder.insert(targetTag, at: 0) }
        }
 
        var current = store.appTags[path] ?? []
        if !copy, !sourceTag.isEmpty, sourceTag != "Mac自带", sourceTag != tr("group.appleBuiltIn") {
            current.removeAll { $0 == sourceTag }
        }
        if !current.contains(targetTag) {
            current.append(targetTag)
        }
 
        if current.isEmpty {
            store.appTags.removeValue(forKey: path)
        } else {
            store.appTags[path] = current
        }
        TagDatabase.saveUserCategorySchemeMutation(
            store,
            previous: previousStore,
            reason: "move-app"
        )
    }
 
    /// Rename a tag on all apps that have it.
    static func renameTag(from oldName: String, to newName: String) {
        var store = TagDatabase.load()
        guard oldName != newName, store.tags[newName] == nil else { return }
        let previousStore = store
        // Update tag definition
        if let def = store.tags.removeValue(forKey: oldName) {
            store.tags[newName] = def
        }
        store.tagOrder = store.tagOrder.map { $0 == oldName ? newName : $0 }
        // Update all app assignments
        for (path, var tags) in store.appTags {
            if let idx = tags.firstIndex(of: oldName) {
                tags[idx] = newName
                store.appTags[path] = Array(NSOrderedSet(array: tags).compactMap { $0 as? String })
            }
        }
        TagDatabase.saveUserCategorySchemeMutation(
            store,
            previous: previousStore,
            reason: "rename-tag"
        )
    }
 
    /// Delete a tag completely (from all apps and tag definitions).
    static func deleteTagCompletely(_ tag: String) {
        var store = TagDatabase.load()
        let previousStore = store
        if let removedTag = store.tags.removeValue(forKey: tag),
           let categoryID = removedTag.systemCategoryID,
           !store.disabledSystemCategoryIDs.contains(categoryID) {
            store.disabledSystemCategoryIDs.append(categoryID)
        }
        store.tagOrder.removeAll { $0 == tag }
        for (path, var tags) in store.appTags {
            tags.removeAll { $0 == tag }
            if tags.isEmpty {
                store.appTags.removeValue(forKey: path)
            } else {
                store.appTags[path] = tags
            }
        }
        TagDatabase.saveUserCategorySchemeMutation(
            store,
            previous: previousStore,
            reason: "delete-tag"
        )
    }
 
    /// Set the color for a tag.
    static func setColor(_ color: Int, for tag: String) {
        var store = TagDatabase.load()
        let previousStore = store
        var tagDef = store.tags[tag] ?? TagDatabase.TagDef(color: color, systemCategoryID: nil)
        tagDef.color = color
        store.tags[tag] = tagDef
        TagDatabase.saveUserCategorySchemeMutation(
            store,
            previous: previousStore,
            reason: "set-tag-color"
        )
    }
}