File size: 46,238 Bytes
04ec17f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { afterSleep, afterTick, srOnlyStyles, attachRef, } from "svelte-toolbelt";
import { Context, watch } from "runed";
import { findNextSibling, findPreviousSibling } from "./utils.js";
import { kbd } from "../../internal/kbd.js";
import { createBitsAttrs, boolToStr, boolToEmptyStrOrUndef } from "../../internal/attrs.js";
import { getFirstNonCommentChild } from "../../internal/dom.js";
import { computeCommandScore } from "./index.js";
import { cssEscape } from "../../internal/css-escape.js";
const COMMAND_VALUE_ATTR = "data-value";
const commandAttrs = createBitsAttrs({
    component: "command",
    parts: [
        "root",
        "list",
        "input",
        "separator",
        "loading",
        "empty",
        "group",
        "group-items",
        "group-heading",
        "item",
        "viewport",
        "input-label",
    ],
});
// selectors
const COMMAND_GROUP_SELECTOR = commandAttrs.selector("group");
const COMMAND_GROUP_ITEMS_SELECTOR = commandAttrs.selector("group-items");
const COMMAND_GROUP_HEADING_SELECTOR = commandAttrs.selector("group-heading");
const COMMAND_ITEM_SELECTOR = commandAttrs.selector("item");
const COMMAND_VALID_ITEM_SELECTOR = `${commandAttrs.selector("item")}:not([aria-disabled="true"])`;
const CommandRootContext = new Context("Command.Root");
const CommandListContext = new Context("Command.List");
const CommandGroupContainerContext = new Context("Command.Group");
const defaultState = {
    /** Value of the search query */
    search: "",
    /** Currently selected item value */
    value: "",
    filtered: {
        /** The count of all visible items. */
        count: 0,
        /** Map from visible item id to its search store. */
        items: new Map(),
        /** Set of groups with at least one visible item. */
        groups: new Set(),
    },
};
export class CommandRootState {
    static create(opts) {
        return CommandRootContext.set(new CommandRootState(opts));
    }
    opts;
    attachment;
    #updateScheduled = false;
    #isInitialMount = true;
    sortAfterTick = false;
    sortAndFilterAfterTick = false;
    allItems = new Set();
    allGroups = new Map();
    allIds = new Map();
    // attempt to prevent the harsh delay when user is typing fast
    key = $state(0);
    viewportNode = $state(null);
    inputNode = $state(null);
    labelNode = $state(null);
    // published state that the components and other things can react to
    commandState = $state.raw(defaultState);
    // internal state that we mutate in batches and publish to the `state` at once
    _commandState = $state(defaultState);
    #snapshot() {
        return $state.snapshot(this._commandState);
    }
    #scheduleUpdate() {
        if (this.#updateScheduled)
            return;
        this.#updateScheduled = true;
        afterTick(() => {
            this.#updateScheduled = false;
            const currentState = this.#snapshot();
            const hasStateChanged = !Object.is(this.commandState, currentState);
            if (hasStateChanged) {
                this.commandState = currentState;
                this.opts.onStateChange?.current?.(currentState);
            }
        });
    }
    setState(key, value, preventScroll) {
        if (Object.is(this._commandState[key], value))
            return;
        this._commandState[key] = value;
        if (key === "search") {
            // Filter synchronously before emitting back to children
            this.#filterItems();
            this.#sort();
        }
        else if (key === "value") {
            if (!preventScroll)
                this.#scrollSelectedIntoView();
        }
        this.#scheduleUpdate();
    }
    constructor(opts) {
        this.opts = opts;
        this.attachment = attachRef(this.opts.ref);
        const defaults = { ...this._commandState, value: this.opts.value.current ?? "" };
        this._commandState = defaults;
        this.commandState = defaults;
        this.onkeydown = this.onkeydown.bind(this);
    }
    /**
     * Calculates score for an item based on search text and keywords.
     * Higher score = better match.
     *
     * @param value - Item's display text
     * @param keywords - Optional keywords to boost scoring
     * @returns Score from 0-1, where 0 = no match
     */
    #score(value, keywords) {
        const filter = this.opts.filter.current ?? computeCommandScore;
        const score = value ? filter(value, this._commandState.search, keywords) : 0;
        return score;
    }
    /**
     * Sorts items and groups based on search scores.
     * Groups are sorted by their highest scoring item.
     * When no search active, selects first item.
     */
    #sort() {
        if (!this._commandState.search || this.opts.shouldFilter.current === false) {
            // if no search and no initial value set or when clearing search,
            // we select the first item.
            if (!this._commandState.value || !this.#isInitialMount) {
                this.#selectFirstItem();
            }
            else if (this.#isInitialMount && this._commandState.value) {
                // scroll the initial value into view if it exists
                this.#scrollInitialValue();
            }
            return;
        }
        const scores = this._commandState.filtered.items;
        // sort the groups
        const groups = [];
        for (const value of this._commandState.filtered.groups) {
            const items = this.allGroups.get(value);
            let max = 0;
            if (!items) {
                groups.push([value, max]);
                continue;
            }
            // get the max score of the group's items
            for (const item of items) {
                const score = scores.get(item);
                max = Math.max(score ?? 0, max);
            }
            groups.push([value, max]);
        }
        // Sort items within groups to bottom
        // Sort items outside of groups
        // Sort groups to bottom (pushes all non-grouped items to the top)
        const listInsertionElement = this.viewportNode;
        const sorted = this.getValidItems().sort((a, b) => {
            const valueA = a.getAttribute("data-value");
            const valueB = b.getAttribute("data-value");
            const scoresA = scores.get(valueA) ?? 0;
            const scoresB = scores.get(valueB) ?? 0;
            return scoresB - scoresA;
        });
        for (const item of sorted) {
            const group = item.closest(COMMAND_GROUP_ITEMS_SELECTOR);
            if (group) {
                const itemToAppend = item.parentElement === group
                    ? item
                    : item.closest(`${COMMAND_GROUP_ITEMS_SELECTOR} > *`);
                if (itemToAppend) {
                    group.appendChild(itemToAppend);
                }
            }
            else {
                const itemToAppend = item.parentElement === listInsertionElement
                    ? item
                    : item.closest(`${COMMAND_GROUP_ITEMS_SELECTOR} > *`);
                if (itemToAppend) {
                    listInsertionElement?.appendChild(itemToAppend);
                }
            }
        }
        const sortedGroups = groups.sort((a, b) => b[1] - a[1]);
        for (const group of sortedGroups) {
            const element = listInsertionElement?.querySelector(`${COMMAND_GROUP_SELECTOR}[${COMMAND_VALUE_ATTR}="${cssEscape(group[0])}"]`);
            element?.parentElement?.appendChild(element);
        }
        this.#selectFirstItem();
    }
    /**
     * Sets current value and triggers re-render if cleared.
     *
     * @param value - New value to set
     */
    setValue(value, opts) {
        if (value !== this.opts.value.current && value === "") {
            afterTick(() => {
                this.key++;
            });
        }
        this.setState("value", value, opts);
        this.opts.value.current = value;
    }
    /**
     * Selects first non-disabled item on next tick.
     */
    #selectFirstItem() {
        afterTick(() => {
            const item = this.getValidItems().find((item) => item.getAttribute("aria-disabled") !== "true");
            const value = item?.getAttribute(COMMAND_VALUE_ATTR);
            const shouldPreventScroll = this.#isInitialMount && this.opts.disableInitialScroll.current;
            this.setValue(value ?? "", shouldPreventScroll);
            this.#isInitialMount = false;
        });
    }
    /**
     * Scrolls the initial value into view if it exists and is not the first item.
     * Called during initial mount when a value is provided.
     */
    #scrollInitialValue() {
        afterTick(() => {
            const shouldPreventScroll = this.opts.disableInitialScroll.current;
            if (!shouldPreventScroll) {
                this.#scrollSelectedIntoView();
            }
            this.#isInitialMount = false;
        });
    }
    /**
     * Updates filtered items/groups based on search.
     * Recalculates scores and filtered count.
     */
    #filterItems() {
        if (!this._commandState.search || this.opts.shouldFilter.current === false) {
            this._commandState.filtered.count = this.allItems.size;
            return;
        }
        // reset the groups
        this._commandState.filtered.groups = new Set();
        let itemCount = 0;
        // Check which items should be included
        for (const id of this.allItems) {
            const value = this.allIds.get(id)?.value ?? "";
            const keywords = this.allIds.get(id)?.keywords ?? [];
            const rank = this.#score(value, keywords);
            this._commandState.filtered.items.set(id, rank);
            if (rank > 0)
                itemCount++;
        }
        // Check which groups have at least 1 item shown
        for (const [groupId, group] of this.allGroups) {
            for (const itemId of group) {
                const currItem = this._commandState.filtered.items.get(itemId);
                if (currItem && currItem > 0) {
                    this._commandState.filtered.groups.add(groupId);
                    break;
                }
            }
        }
        this._commandState.filtered.count = itemCount;
    }
    /**
     * Gets all non-disabled, visible command items.
     *
     * @returns Array of valid item elements
     * @remarks Exposed for direct item access and bound checking
     */
    getValidItems() {
        const node = this.opts.ref.current;
        if (!node)
            return [];
        const validItems = Array.from(node.querySelectorAll(COMMAND_VALID_ITEM_SELECTOR)).filter((el) => !!el);
        return validItems;
    }
    /**
     * Gets all visible command items.
     *
     * @returns Array of valid item elements
     * @remarks Exposed for direct item access and bound checking
     */
    getVisibleItems() {
        const node = this.opts.ref.current;
        if (!node)
            return [];
        const visibleItems = Array.from(node.querySelectorAll(COMMAND_ITEM_SELECTOR)).filter((el) => !!el);
        return visibleItems;
    }
    /** Returns all visible items in a matrix structure
     *
     * @remarks Returns empty if the command isn't configured as a grid
     *
     * @returns
     */
    get itemsGrid() {
        if (!this.isGrid)
            return [];
        const columns = this.opts.columns.current ?? 1;
        const items = this.getVisibleItems();
        const grid = [[]];
        let currentGroup = items[0]?.getAttribute("data-group");
        let column = 0;
        let row = 0;
        for (let i = 0; i < items.length; i++) {
            const item = items[i];
            const itemGroup = item?.getAttribute("data-group");
            if (currentGroup !== itemGroup) {
                currentGroup = itemGroup;
                column = 1;
                row++;
                grid.push([{ index: i, firstRowOfGroup: true, ref: item }]);
            }
            else {
                column++;
                if (column > columns) {
                    row++;
                    column = 1;
                    grid.push([]);
                }
                grid[row]?.push({
                    index: i,
                    firstRowOfGroup: grid[row]?.[0]?.firstRowOfGroup ?? i === 0,
                    ref: item,
                });
            }
        }
        return grid;
    }
    /**
     * Gets currently selected command item.
     *
     * @returns Selected element or undefined
     */
    #getSelectedItem() {
        const node = this.opts.ref.current;
        if (!node)
            return;
        const selectedNode = node.querySelector(`${COMMAND_VALID_ITEM_SELECTOR}[data-selected]`);
        if (!selectedNode)
            return;
        return selectedNode;
    }
    /**
     * Scrolls selected item into view.
     * Special handling for first items in groups.
     */
    #scrollSelectedIntoView() {
        afterTick(() => {
            const item = this.#getSelectedItem();
            if (!item)
                return;
            const grandparent = item.parentElement?.parentElement;
            if (!grandparent)
                return;
            if (this.isGrid) {
                const isFirstRowOfGroup = this.#itemIsFirstRowOfGroup(item);
                // ensure item is visible
                item.scrollIntoView({ block: "nearest" });
                if (isFirstRowOfGroup) {
                    const closestGroupHeader = item
                        ?.closest(COMMAND_GROUP_SELECTOR)
                        ?.querySelector(COMMAND_GROUP_HEADING_SELECTOR);
                    closestGroupHeader?.scrollIntoView({ block: "nearest" });
                    return;
                }
            }
            else {
                const firstChildOfParent = getFirstNonCommentChild(grandparent);
                if (firstChildOfParent &&
                    firstChildOfParent.dataset?.value === item.dataset?.value) {
                    const closestGroupHeader = item
                        ?.closest(COMMAND_GROUP_SELECTOR)
                        ?.querySelector(COMMAND_GROUP_HEADING_SELECTOR);
                    closestGroupHeader?.scrollIntoView({ block: "nearest" });
                    return;
                }
            }
            item.scrollIntoView({ block: "nearest" });
        });
    }
    #itemIsFirstRowOfGroup(item) {
        const grid = this.itemsGrid;
        if (grid.length === 0)
            return false;
        for (let r = 0; r < grid.length; r++) {
            const row = grid[r];
            if (row === undefined)
                continue;
            for (let c = 0; c < row.length; c++) {
                const column = row[c];
                if (column === undefined || column.ref !== item)
                    continue;
                return column.firstRowOfGroup;
            }
        }
        return false;
    }
    /**
     * Sets selection to item at specified index in valid items array.
     * If index is out of bounds, does nothing.
     *
     * @param index - Zero-based index of item to select
     * @remarks
     * Uses `getValidItems()` to get selectable items, filtering out disabled/hidden ones.
     * Access valid items directly via `getValidItems()` to check bounds before calling.
     *
     * @example
     * // get valid items length for bounds check
     * const items = getValidItems()
     * if (index < items.length) {
     *   updateSelectedToIndex(index)
     * }
     */
    updateSelectedToIndex(index) {
        const item = this.getValidItems()[index];
        if (!item)
            return;
        this.setValue(item.getAttribute(COMMAND_VALUE_ATTR) ?? "");
    }
    /**
     * Updates selected item by moving up/down relative to current selection.
     * Handles wrapping when loop option is enabled.
     *
     * @param change - Direction to move: 1 for next item, -1 for previous item
     * @remarks
     * The loop behavior wraps:
     * - From last item to first when moving next
     * - From first item to last when moving previous
     *
     * Uses `getValidItems()` to get all selectable items, which filters out disabled/hidden items.
     * You can call `getValidItems()` directly to get the current valid items array.
     *
     * @example
     * // select next item
     * updateSelectedByItem(1)
     *
     * // get all valid items
     * const items = getValidItems()
     */
    updateSelectedByItem(change) {
        const selected = this.#getSelectedItem();
        const items = this.getValidItems();
        const index = items.findIndex((item) => item === selected);
        // Get item at this index
        let newSelected = items[index + change];
        if (this.opts.loop.current) {
            newSelected =
                index + change < 0
                    ? items[items.length - 1]
                    : index + change === items.length
                        ? items[0]
                        : items[index + change];
        }
        if (newSelected) {
            this.setValue(newSelected.getAttribute(COMMAND_VALUE_ATTR) ?? "");
        }
    }
    /**
     * Moves selection to the first valid item in the next/previous group.
     * If no group is found, falls back to selecting the next/previous item globally.
     *
     * @param change - Direction to move: 1 for next group, -1 for previous group
     * @example
     * // move to first item in next group
     * updateSelectedByGroup(1)
     *
     * // move to first item in previous group
     * updateSelectedByGroup(-1)
     */
    updateSelectedByGroup(change) {
        const selected = this.#getSelectedItem();
        let group = selected?.closest(COMMAND_GROUP_SELECTOR);
        let item;
        while (group && !item) {
            group =
                change > 0
                    ? findNextSibling(group, COMMAND_GROUP_SELECTOR)
                    : findPreviousSibling(group, COMMAND_GROUP_SELECTOR);
            item = group?.querySelector(COMMAND_VALID_ITEM_SELECTOR);
        }
        if (item) {
            this.setValue(item.getAttribute(COMMAND_VALUE_ATTR) ?? "");
        }
        else {
            this.updateSelectedByItem(change);
        }
    }
    /**
     * Maps item id to display value and search keywords.
     * Returns cleanup function to remove mapping.
     *
     * @param id - Unique item identifier
     * @param value - Display text
     * @param keywords - Optional search boost terms
     * @returns Cleanup function
     */
    registerValue(value, keywords) {
        if (!(value && value === this.allIds.get(value)?.value)) {
            this.allIds.set(value, { value, keywords });
        }
        this._commandState.filtered.items.set(value, this.#score(value, keywords));
        // Schedule sorting to run after this tick when all items are added not each time an item is added
        if (!this.sortAfterTick) {
            this.sortAfterTick = true;
            afterTick(() => {
                this.#sort();
                this.sortAfterTick = false;
            });
        }
        return () => {
            this.allIds.delete(value);
        };
    }
    /**
     * Registers item in command list and its group.
     * Handles filtering, sorting and selection updates.
     *
     * @param id - Item identifier
     * @param groupId - Optional group to add item to
     * @returns Cleanup function that handles selection
     */
    registerItem(id, groupId) {
        this.allItems.add(id);
        // Track this item within the group
        if (groupId) {
            if (!this.allGroups.has(groupId)) {
                this.allGroups.set(groupId, new Set([id]));
            }
            else {
                this.allGroups.get(groupId).add(id);
            }
        }
        // Schedule sorting and filtering to run after this tick when all items are added not each time an item is added
        if (!this.sortAndFilterAfterTick) {
            this.sortAndFilterAfterTick = true;
            afterTick(() => {
                this.#filterItems();
                this.#sort();
                this.sortAndFilterAfterTick = false;
            });
        }
        this.#scheduleUpdate();
        return () => {
            const selectedItem = this.#getSelectedItem();
            this.allItems.delete(id);
            this.commandState.filtered.items.delete(id);
            this.#filterItems();
            // The item removed have been the selected one,
            // so selection should be moved to the first
            if (selectedItem?.getAttribute("id") === id) {
                this.#selectFirstItem();
            }
            this.#scheduleUpdate();
        };
    }
    /**
     * Creates empty group if not exists.
     *
     * @param id - Group identifier
     * @returns Cleanup function
     */
    registerGroup(id) {
        if (!this.allGroups.has(id)) {
            this.allGroups.set(id, new Set());
        }
        return () => {
            this.allIds.delete(id);
            this.allGroups.delete(id);
        };
    }
    get isGrid() {
        return this.opts.columns.current !== null;
    }
    /**
     * Selects last valid item.
     */
    #last() {
        return this.updateSelectedToIndex(this.getValidItems().length - 1);
    }
    /**
     * Handles next item selection:
     * - Meta: Jump to last
     * - Alt: Next group
     * - Default: Next item
     *
     * @param e - Keyboard event
     */
    #next(e) {
        e.preventDefault();
        if (e.metaKey) {
            this.#last();
        }
        else if (e.altKey) {
            this.updateSelectedByGroup(1);
        }
        else {
            this.updateSelectedByItem(1);
        }
    }
    #down(e) {
        if (this.opts.columns.current === null)
            return;
        e.preventDefault();
        if (e.metaKey) {
            this.updateSelectedByGroup(1);
        }
        else {
            this.updateSelectedByItem(this.#nextRowColumnOffset(e));
        }
    }
    #getColumn(item, grid) {
        if (grid.length === 0)
            return null;
        for (let r = 0; r < grid.length; r++) {
            const row = grid[r];
            if (row === undefined)
                continue;
            for (let c = 0; c < row.length; c++) {
                const column = row[c];
                if (column === undefined || column.ref !== item)
                    continue;
                return { columnIndex: c, rowIndex: r };
            }
        }
        return null;
    }
    #nextRowColumnOffset(e) {
        const grid = this.itemsGrid;
        const selected = this.#getSelectedItem();
        if (!selected)
            return 0;
        const column = this.#getColumn(selected, grid);
        if (!column)
            return 0;
        let newItem = null;
        const skipRows = e.altKey ? 1 : 0;
        // if this is the second to last row then we need to go to the last row when skipping and not in loop mode
        if (e.altKey && column.rowIndex === grid.length - 2 && !this.opts.loop.current) {
            newItem = this.#findNextNonDisabledItem({
                start: grid.length - 1,
                end: grid.length,
                expectedColumnIndex: column.columnIndex,
                grid,
            });
        }
        else if (column.rowIndex === grid.length - 1) {
            // if this is the last row we apply the loop logic
            if (!this.opts.loop.current)
                return 0;
            newItem = this.#findNextNonDisabledItem({
                start: 0 + skipRows,
                end: column.rowIndex,
                expectedColumnIndex: column.columnIndex,
                grid,
            });
        }
        else {
            newItem = this.#findNextNonDisabledItem({
                start: column.rowIndex + 1 + skipRows,
                end: grid.length,
                expectedColumnIndex: column.columnIndex,
                grid,
            });
            // this happens if there were no non-disabled columns below the current column
            // we can now try starting from the beginning to find the right column
            if (newItem === null && this.opts.loop.current) {
                newItem = this.#findNextNonDisabledItem({
                    start: 0,
                    end: column.rowIndex,
                    expectedColumnIndex: column.columnIndex,
                    grid,
                });
            }
        }
        return this.#calculateOffset(selected, newItem);
    }
    /** Attempts to find the next non-disabled column that matches the expected column.
     *
     * @remarks
     * - Skips over disabled columns
     * - When a row is shorter than the expected column it defaults to the last item in the row
     *
     * @param param0
     * @returns
     */
    #findNextNonDisabledItem({ start, end, grid, expectedColumnIndex, }) {
        let newItem = null;
        for (let r = start; r < end; r++) {
            const row = grid[r];
            // try to get the next column
            newItem = row[expectedColumnIndex]?.ref ?? null;
            // skip over disabled items
            if (newItem !== null && itemIsDisabled(newItem)) {
                newItem = null;
                continue;
            }
            // if that column doesn't exist default to the next highest column
            if (newItem === null) {
                // try and find the next highest non-disabled item in the row
                // if there aren't any non-disabled items we just give up and return null
                for (let i = row.length - 1; i >= 0; i--) {
                    const item = row[row.length - 1];
                    if (item === undefined || itemIsDisabled(item.ref))
                        continue;
                    newItem = item.ref;
                    break;
                }
            }
            break;
        }
        return newItem;
    }
    #calculateOffset(selected, newSelected) {
        if (newSelected === null)
            return 0;
        const items = this.getValidItems();
        const ogIndex = items.findIndex((item) => item === selected);
        const newIndex = items.findIndex((item) => item === newSelected);
        return newIndex - ogIndex;
    }
    #up(e) {
        if (this.opts.columns.current === null)
            return;
        e.preventDefault();
        if (e.metaKey) {
            this.updateSelectedByGroup(-1);
        }
        else {
            this.updateSelectedByItem(this.#previousRowColumnOffset(e));
        }
    }
    #previousRowColumnOffset(e) {
        const grid = this.itemsGrid;
        const selected = this.#getSelectedItem();
        if (selected === undefined)
            return 0;
        const column = this.#getColumn(selected, grid);
        if (column === null)
            return 0;
        let newItem = null;
        const skipRows = e.altKey ? 1 : 0;
        // if this is the second row then we need to go to the top when skipping and not in loop mode
        if (e.altKey && column.rowIndex === 1 && this.opts.loop.current === false) {
            newItem = this.#findNextNonDisabledItemDesc({
                start: 0,
                end: 0,
                expectedColumnIndex: column.columnIndex,
                grid,
            });
        }
        else if (column.rowIndex === 0) {
            // if this is the last row we apply the loop logic
            if (this.opts.loop.current === false)
                return 0;
            newItem = this.#findNextNonDisabledItemDesc({
                start: grid.length - 1 - skipRows,
                end: column.rowIndex + 1,
                expectedColumnIndex: column.columnIndex,
                grid,
            });
        }
        else {
            newItem = this.#findNextNonDisabledItemDesc({
                start: column.rowIndex - 1 - skipRows,
                end: 0,
                expectedColumnIndex: column.columnIndex,
                grid,
            });
            // this happens if there were no non-disabled columns below the current column
            // we can now try starting from the beginning to find the right column
            if (newItem === null && this.opts.loop.current) {
                newItem = this.#findNextNonDisabledItemDesc({
                    start: grid.length - 1,
                    end: column.rowIndex + 1,
                    expectedColumnIndex: column.columnIndex,
                    grid,
                });
            }
        }
        return this.#calculateOffset(selected, newItem);
    }
    /**
     * Attempts to find the next non-disabled column that matches the expected column.
     *
     * @remarks
     * - Skips over disabled columns
     * - When a row is shorter than the expected column it defaults to the last item in the row
     */
    #findNextNonDisabledItemDesc({ start, end, grid, expectedColumnIndex, }) {
        let newItem = null;
        for (let r = start; r >= end; r--) {
            const row = grid[r];
            if (row === undefined)
                continue;
            // try to get the next column
            newItem = row[expectedColumnIndex]?.ref ?? null;
            // skip over disabled items
            if (newItem !== null && itemIsDisabled(newItem)) {
                newItem = null;
                continue;
            }
            // if that column doesn't exist default to the next highest column
            if (newItem === null) {
                // try and find the next highest non-disabled item in the row
                // if there aren't any non-disabled items we just give up and return null
                for (let i = row.length - 1; i >= 0; i--) {
                    const item = row[row.length - 1];
                    if (item === undefined || itemIsDisabled(item.ref))
                        continue;
                    newItem = item.ref;
                    break;
                }
            }
            break;
        }
        return newItem;
    }
    /**
     * Handles previous item selection:
     * - Meta: Jump to first
     * - Alt: Previous group
     * - Default: Previous item
     *
     * @param e - Keyboard event
     */
    #prev(e) {
        e.preventDefault();
        if (e.metaKey) {
            // First item
            this.updateSelectedToIndex(0);
        }
        else if (e.altKey) {
            // Previous group
            this.updateSelectedByGroup(-1);
        }
        else {
            // Previous item
            this.updateSelectedByItem(-1);
        }
    }
    onkeydown(e) {
        const isVim = this.opts.vimBindings.current && e.ctrlKey;
        switch (e.key) {
            case kbd.n:
            case kbd.j: {
                // vim down
                if (isVim) {
                    if (this.isGrid) {
                        this.#down(e);
                    }
                    else {
                        this.#next(e);
                    }
                }
                break;
            }
            case kbd.l: {
                // vim right
                if (isVim) {
                    if (this.isGrid) {
                        this.#next(e);
                    }
                }
                break;
            }
            case kbd.ARROW_DOWN:
                if (this.isGrid) {
                    this.#down(e);
                }
                else {
                    this.#next(e);
                }
                break;
            case kbd.ARROW_RIGHT:
                if (!this.isGrid)
                    break;
                this.#next(e);
                break;
            case kbd.p:
            case kbd.k: {
                // vim up
                if (isVim) {
                    if (this.isGrid) {
                        this.#up(e);
                    }
                    else {
                        this.#prev(e);
                    }
                }
                break;
            }
            case kbd.h: {
                // vim left
                if (isVim && this.isGrid) {
                    this.#prev(e);
                }
                break;
            }
            case kbd.ARROW_UP:
                if (this.isGrid) {
                    this.#up(e);
                }
                else {
                    this.#prev(e);
                }
                break;
            case kbd.ARROW_LEFT:
                if (!this.isGrid)
                    break;
                this.#prev(e);
                break;
            case kbd.HOME:
                // first item
                e.preventDefault();
                this.updateSelectedToIndex(0);
                break;
            case kbd.END:
                // last item
                e.preventDefault();
                this.#last();
                break;
            case kbd.ENTER: {
                /**
                 * Check if IME composition is finished before triggering the select event.
                 * This prevents unwanted triggering while user is still inputting text with IME.
                 * e.keyCode === 229 is for the Japanese IME && Safari as `isComposing` does not
                 * work with Japanese IME and Safari in combination.
                 */
                if (!e.isComposing && e.keyCode !== 229) {
                    e.preventDefault();
                    const item = this.#getSelectedItem();
                    if (item) {
                        item?.click();
                    }
                }
            }
        }
    }
    props = $derived.by(() => ({
        id: this.opts.id.current,
        role: "application",
        [commandAttrs.root]: "",
        tabindex: -1,
        onkeydown: this.onkeydown,
        ...this.attachment,
    }));
}
function itemIsDisabled(item) {
    return item.getAttribute("aria-disabled") === "true";
}
export class CommandEmptyState {
    static create(opts) {
        return new CommandEmptyState(opts, CommandRootContext.get());
    }
    opts;
    root;
    attachment;
    shouldRender = $derived.by(() => {
        return ((this.root._commandState.filtered.count === 0 && this.#isInitialRender === false) ||
            this.opts.forceMount.current);
    });
    #isInitialRender = true;
    constructor(opts, root) {
        this.opts = opts;
        this.root = root;
        this.attachment = attachRef(this.opts.ref);
        $effect.pre(() => {
            this.#isInitialRender = false;
        });
    }
    props = $derived.by(() => ({
        id: this.opts.id.current,
        role: "presentation",
        [commandAttrs.empty]: "",
        ...this.attachment,
    }));
}
export class CommandGroupContainerState {
    static create(opts) {
        return CommandGroupContainerContext.set(new CommandGroupContainerState(opts, CommandRootContext.get()));
    }
    opts;
    root;
    attachment;
    shouldRender = $derived.by(() => {
        if (this.opts.forceMount.current)
            return true;
        if (this.root.opts.shouldFilter.current === false)
            return true;
        if (!this.root.commandState.search)
            return true;
        return this.root._commandState.filtered.groups.has(this.trueValue);
    });
    headingNode = $state(null);
    trueValue = $state("");
    constructor(opts, root) {
        this.opts = opts;
        this.root = root;
        this.attachment = attachRef(this.opts.ref);
        this.trueValue = opts.value.current ?? opts.id.current;
        watch(() => this.trueValue, () => {
            return this.root.registerGroup(this.trueValue);
        });
        $effect(() => {
            if (this.opts.value.current) {
                this.trueValue = this.opts.value.current;
                return this.root.registerValue(this.opts.value.current);
            }
            else if (this.headingNode && this.headingNode.textContent) {
                this.trueValue = this.headingNode.textContent.trim().toLowerCase();
                return this.root.registerValue(this.trueValue);
            }
            else {
                this.trueValue = `-----${this.opts.id.current}`;
                return this.root.registerValue(this.trueValue);
            }
        });
    }
    props = $derived.by(() => ({
        id: this.opts.id.current,
        role: "presentation",
        hidden: this.shouldRender ? undefined : true,
        "data-value": this.trueValue,
        [commandAttrs.group]: "",
        ...this.attachment,
    }));
}
export class CommandGroupHeadingState {
    static create(opts) {
        return new CommandGroupHeadingState(opts, CommandGroupContainerContext.get());
    }
    opts;
    group;
    attachment;
    constructor(opts, group) {
        this.opts = opts;
        this.group = group;
        this.attachment = attachRef(this.opts.ref, (v) => (this.group.headingNode = v));
    }
    props = $derived.by(() => ({
        id: this.opts.id.current,
        [commandAttrs["group-heading"]]: "",
        ...this.attachment,
    }));
}
export class CommandGroupItemsState {
    static create(opts) {
        return new CommandGroupItemsState(opts, CommandGroupContainerContext.get());
    }
    opts;
    group;
    attachment;
    constructor(opts, group) {
        this.opts = opts;
        this.group = group;
        this.attachment = attachRef(this.opts.ref);
    }
    props = $derived.by(() => ({
        id: this.opts.id.current,
        role: "group",
        [commandAttrs["group-items"]]: "",
        "aria-labelledby": this.group.headingNode?.id ?? undefined,
        ...this.attachment,
    }));
}
export class CommandInputState {
    static create(opts) {
        return new CommandInputState(opts, CommandRootContext.get());
    }
    opts;
    root;
    attachment;
    #selectedItemId = $derived.by(() => {
        const item = this.root.viewportNode?.querySelector(`${COMMAND_ITEM_SELECTOR}[${COMMAND_VALUE_ATTR}="${cssEscape(this.root.opts.value.current)}"]`);
        if (item === undefined || item === null)
            return;
        return item.getAttribute("id") ?? undefined;
    });
    constructor(opts, root) {
        this.opts = opts;
        this.root = root;
        this.attachment = attachRef(this.opts.ref, (v) => (this.root.inputNode = v));
        watch(() => this.opts.ref.current, () => {
            const node = this.opts.ref.current;
            if (node && this.opts.autofocus.current) {
                afterSleep(10, () => node.focus());
            }
        });
        watch(() => this.opts.value.current, () => {
            if (this.root.commandState.search !== this.opts.value.current) {
                this.root.setState("search", this.opts.value.current);
            }
        });
    }
    props = $derived.by(() => ({
        id: this.opts.id.current,
        type: "text",
        [commandAttrs.input]: "",
        autocomplete: "off",
        autocorrect: "off",
        spellcheck: false,
        "aria-autocomplete": "list",
        role: "combobox",
        "aria-expanded": boolToStr(true),
        "aria-controls": this.root.viewportNode?.id ?? undefined,
        "aria-labelledby": this.root.labelNode?.id ?? undefined,
        "aria-activedescendant": this.#selectedItemId,
        ...this.attachment,
    }));
}
export class CommandItemState {
    static create(opts) {
        const group = CommandGroupContainerContext.getOr(null);
        return new CommandItemState({ ...opts, group }, CommandRootContext.get());
    }
    opts;
    root;
    attachment;
    #group = null;
    #trueForceMount = $derived.by(() => {
        return this.opts.forceMount.current || this.#group?.opts.forceMount.current === true;
    });
    shouldRender = $derived.by(() => {
        this.opts.ref.current;
        if (this.#trueForceMount ||
            this.root.opts.shouldFilter.current === false ||
            !this.root.commandState.search) {
            return true;
        }
        const currentScore = this.root.commandState.filtered.items.get(this.trueValue);
        if (currentScore === undefined)
            return false;
        return currentScore > 0;
    });
    isSelected = $derived.by(() => this.root.opts.value.current === this.trueValue && this.trueValue !== "");
    trueValue = $state("");
    constructor(opts, root) {
        this.opts = opts;
        this.root = root;
        this.#group = CommandGroupContainerContext.getOr(null);
        this.trueValue = opts.value.current;
        this.attachment = attachRef(this.opts.ref);
        watch([
            () => this.trueValue,
            () => this.#group?.trueValue,
            () => this.opts.forceMount.current,
        ], () => {
            if (this.opts.forceMount.current || !this.trueValue)
                return;
            return this.root.registerItem(this.trueValue, this.#group?.trueValue);
        });
        watch([() => this.opts.value.current, () => this.opts.ref.current], () => {
            if (this.opts.value.current) {
                this.trueValue = this.opts.value.current;
            }
            else if (this.opts.ref.current?.textContent) {
                this.trueValue = this.opts.ref.current.textContent.trim();
            }
            if (this.trueValue) {
                this.root.registerValue(this.trueValue, opts.keywords.current.map((kw) => kw.trim()));
                this.opts.ref.current?.setAttribute(COMMAND_VALUE_ATTR, this.trueValue);
            }
        });
        // bindings
        this.onclick = this.onclick.bind(this);
        this.onpointermove = this.onpointermove.bind(this);
    }
    #onSelect() {
        if (this.opts.disabled.current)
            return;
        this.#select();
        this.opts.onSelect?.current();
    }
    #select() {
        if (this.opts.disabled.current)
            return;
        this.root.setValue(this.trueValue, true);
    }
    onpointermove(_) {
        if (this.opts.disabled.current || this.root.opts.disablePointerSelection.current)
            return;
        this.#select();
    }
    onclick(_) {
        if (this.opts.disabled.current)
            return;
        this.#onSelect();
    }
    props = $derived.by(() => ({
        id: this.opts.id.current,
        "aria-disabled": boolToStr(this.opts.disabled.current),
        "aria-selected": boolToStr(this.isSelected),
        "data-disabled": boolToEmptyStrOrUndef(this.opts.disabled.current),
        "data-selected": boolToEmptyStrOrUndef(this.isSelected),
        "data-value": this.trueValue,
        "data-group": this.#group?.trueValue,
        [commandAttrs.item]: "",
        role: "option",
        onpointermove: this.onpointermove,
        onclick: this.onclick,
        ...this.attachment,
    }));
}
export class CommandLoadingState {
    static create(opts) {
        return new CommandLoadingState(opts);
    }
    opts;
    attachment;
    constructor(opts) {
        this.opts = opts;
        this.attachment = attachRef(this.opts.ref);
    }
    props = $derived.by(() => ({
        id: this.opts.id.current,
        role: "progressbar",
        "aria-valuenow": this.opts.progress.current,
        "aria-valuemin": 0,
        "aria-valuemax": 100,
        "aria-label": "Loading...",
        [commandAttrs.loading]: "",
        ...this.attachment,
    }));
}
export class CommandSeparatorState {
    static create(opts) {
        return new CommandSeparatorState(opts, CommandRootContext.get());
    }
    opts;
    root;
    attachment;
    shouldRender = $derived.by(() => !this.root._commandState.search || this.opts.forceMount.current);
    constructor(opts, root) {
        this.opts = opts;
        this.root = root;
        this.attachment = attachRef(this.opts.ref);
    }
    props = $derived.by(() => ({
        id: this.opts.id.current,
        // role="separator" cannot belong to a role="listbox"
        "aria-hidden": "true",
        [commandAttrs.separator]: "",
        ...this.attachment,
    }));
}
export class CommandListState {
    static create(opts) {
        return CommandListContext.set(new CommandListState(opts, CommandRootContext.get()));
    }
    opts;
    root;
    attachment;
    constructor(opts, root) {
        this.opts = opts;
        this.root = root;
        this.attachment = attachRef(this.opts.ref);
    }
    props = $derived.by(() => ({
        id: this.opts.id.current,
        role: "listbox",
        "aria-label": this.opts.ariaLabel.current,
        [commandAttrs.list]: "",
        ...this.attachment,
    }));
}
export class CommandLabelState {
    static create(opts) {
        return new CommandLabelState(opts, CommandRootContext.get());
    }
    opts;
    root;
    attachment;
    constructor(opts, root) {
        this.opts = opts;
        this.root = root;
        this.attachment = attachRef(this.opts.ref, (v) => (this.root.labelNode = v));
    }
    props = $derived.by(() => ({
        id: this.opts.id.current,
        [commandAttrs["input-label"]]: "",
        for: this.opts.for?.current,
        style: srOnlyStyles,
        ...this.attachment,
    }));
}
export class CommandViewportState {
    static create(opts) {
        return new CommandViewportState(opts, CommandListContext.get());
    }
    opts;
    list;
    attachment;
    constructor(opts, list) {
        this.opts = opts;
        this.list = list;
        this.attachment = attachRef(this.opts.ref, (v) => (this.list.root.viewportNode = v));
        watch([() => this.opts.ref.current, () => this.list.opts.ref.current], ([node, listNode]) => {
            if (node === null || listNode === null)
                return;
            let aF;
            const observer = new ResizeObserver(() => {
                aF = requestAnimationFrame(() => {
                    const height = node.offsetHeight;
                    listNode.style.setProperty("--bits-command-list-height", `${height.toFixed(1)}px`);
                });
            });
            observer.observe(node);
            return () => {
                cancelAnimationFrame(aF);
                observer.unobserve(node);
            };
        });
    }
    props = $derived.by(() => ({
        id: this.opts.id.current,
        [commandAttrs.viewport]: "",
        ...this.attachment,
    }));
}