hyperchad_actions 0.2.0

HyperChad actions package
Documentation
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
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
//! DSL AST for `HyperChad` Actions
//!
//! This module provides a flexible AST that can represent Rust-like syntax
//! for defining actions in `HyperChad` templates.

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::{ActionEffect, ActionType, ElementTarget, Target};
use hyperchad_transformer_models::Visibility;

/// Top-level DSL statement
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Statement {
    /// Expression statement (e.g., `hide("test");`)
    Expression(Expression),
    /// Variable assignment (e.g., `let x = get_visibility("test");`)
    Let {
        /// Variable name
        name: String,
        /// Value expression
        value: Expression,
    },
    /// If statement with optional else
    If {
        /// Condition expression
        condition: Expression,
        /// Then branch block
        then_block: Block,
        /// Optional else branch block
        else_block: Option<Block>,
    },
    /// Match statement
    Match {
        /// Expression to match against
        expr: Expression,
        /// Match arms
        arms: Vec<MatchArm>,
    },
    /// For loop (for iteration over collections)
    For {
        /// Pattern to bind iteration variable
        pattern: String,
        /// Iterator expression
        iter: Expression,
        /// Loop body
        body: Block,
    },
    /// While loop
    While {
        /// Loop condition
        condition: Expression,
        /// Loop body
        body: Block,
    },
    /// Block statement
    Block(Block),
}

/// A block of statements
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Block {
    /// Statements in this block
    pub statements: Vec<Statement>,
}

/// Match arm
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct MatchArm {
    /// Pattern to match against
    pub pattern: Pattern,
    /// Expression to evaluate when pattern matches
    pub body: Expression,
}

/// Pattern for match arms
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Pattern {
    /// Literal pattern (e.g., `true`, `"hello"`, `42`)
    Literal(Literal),
    /// Variable pattern (e.g., `x`)
    Variable(String),
    /// Wildcard pattern (`_`)
    Wildcard,
    /// Enum variant pattern (e.g., `Visibility::Hidden`)
    Variant {
        /// Name of the enum type
        enum_name: String,
        /// Name of the variant
        variant: String,
        /// Fields of the variant
        fields: Vec<Self>,
    },
}

/// Variable representing an element reference for DSL expressions
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ElementVariable {
    /// Element variable name
    pub name: String,
}

impl ElementVariable {
    /// Shows the element
    #[must_use]
    pub fn show(self) -> ActionType {
        ActionType::show_by_id(Target::reference(self.name))
    }

    /// Hides the element
    #[must_use]
    pub fn hide(self) -> ActionType {
        ActionType::hide_by_id(Target::reference(self.name))
    }

    /// Focuses the element
    #[must_use]
    pub fn focus(self) -> ActionType {
        ActionType::focus_by_id(Target::reference(self.name))
    }

    /// Selects the element
    #[must_use]
    pub fn select(self) -> ActionType {
        ActionType::select_by_id(Target::reference(self.name))
    }

    /// Toggles visibility of the element
    #[cfg(feature = "logic")]
    #[must_use]
    pub fn toggle_visibility(self) -> ActionType {
        ActionType::toggle_visibility_by_id(Target::reference(self.name))
    }

    /// Gets the visibility value of the element
    #[cfg(feature = "logic")]
    #[must_use]
    pub fn visibility(self) -> crate::logic::CalcValue {
        crate::logic::get_visibility_by_id(Target::reference(self.name))
    }

    /// Enables display on the element
    #[must_use]
    pub fn display(self) -> ActionType {
        ActionType::display_by_id(Target::reference(self.name))
    }

    /// Disables display on the element
    #[must_use]
    pub fn no_display(self) -> ActionType {
        ActionType::no_display_by_id(Target::reference(self.name))
    }

    /// Sets display property on the element
    #[must_use]
    pub fn set_display(self, display: bool) -> ActionType {
        ActionType::set_display_by_id(display, Target::reference(self.name))
    }

    /// Toggles display property on the element
    #[cfg(feature = "logic")]
    #[must_use]
    pub fn toggle_display(self) -> ActionType {
        ActionType::toggle_display_by_id(Target::reference(self.name))
    }

    /// Gets the display value of the element
    #[cfg(feature = "logic")]
    #[must_use]
    pub fn get_display(self) -> crate::logic::CalcValue {
        crate::logic::get_display_by_id(Target::reference(self.name))
    }

    /// Gets the width in pixels of the element
    #[cfg(feature = "logic")]
    #[must_use]
    pub fn get_width_px(self) -> crate::logic::CalcValue {
        crate::logic::get_width_px_by_id(Target::reference(self.name))
    }

    /// Gets the height in pixels of the element
    #[cfg(feature = "logic")]
    #[must_use]
    pub fn get_height_px(self) -> crate::logic::CalcValue {
        crate::logic::get_height_px_by_id(Target::reference(self.name))
    }

    /// Gets the mouse X coordinate relative to the element
    #[cfg(feature = "logic")]
    #[must_use]
    pub fn get_mouse_x(self) -> crate::logic::CalcValue {
        crate::logic::get_mouse_x_by_id(Target::reference(self.name))
    }

    /// Gets the mouse Y coordinate relative to the element
    #[cfg(feature = "logic")]
    #[must_use]
    pub fn get_mouse_y(self) -> crate::logic::CalcValue {
        crate::logic::get_mouse_y_by_id(Target::reference(self.name))
    }

    /// Sets visibility on the element
    #[must_use]
    pub fn set_visibility(self, visibility: Visibility) -> ActionType {
        ActionType::set_visibility_by_id(visibility, Target::reference(self.name))
    }
}

/// DSL Expression
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Expression {
    /// Literal value
    Literal(Literal),
    /// Variable reference
    Variable(String),
    /// Element reference by selector for object-oriented API
    ElementRef(Box<Self>),
    /// Element reference by ID for object-oriented API
    ElementByIdRef(Box<Self>),
    /// Function call
    Call {
        /// Function name
        function: String,
        /// Function arguments
        args: Vec<Self>,
    },
    /// Method call (e.g., `expr.method(args)`)
    MethodCall {
        /// Receiver expression
        receiver: Box<Self>,
        /// Method name
        method: String,
        /// Method arguments
        args: Vec<Self>,
    },
    /// Field access (e.g., `obj.field`)
    Field {
        /// Object expression
        object: Box<Self>,
        /// Field name
        field: String,
    },
    /// Binary operation
    Binary {
        /// Left operand
        left: Box<Self>,
        /// Binary operator
        op: BinaryOp,
        /// Right operand
        right: Box<Self>,
    },
    /// Unary operation
    Unary {
        /// Unary operator
        op: UnaryOp,
        /// Operand expression
        expr: Box<Self>,
    },
    /// Conditional expression (e.g., `if condition { a } else { b }`)
    If {
        /// Condition expression
        condition: Box<Self>,
        /// Then branch expression
        then_branch: Box<Self>,
        /// Optional else branch expression
        else_branch: Option<Box<Self>>,
    },
    /// Match expression
    Match {
        /// Expression to match against
        expr: Box<Self>,
        /// Match arms
        arms: Vec<MatchArm>,
    },
    /// Block expression
    Block(Block),
    /// Array/collection literal
    Array(Vec<Self>),
    /// Tuple literal
    Tuple(Vec<Self>),
    /// Range expression (e.g., `1..10`)
    Range {
        /// Optional start expression
        start: Option<Box<Self>>,
        /// Optional end expression
        end: Option<Box<Self>>,
        /// Whether the range is inclusive
        inclusive: bool,
    },
    /// Closure expression (e.g., `|param| { ... }`)
    Closure {
        /// Parameter names
        params: Vec<String>,
        /// Closure body
        body: Box<Self>,
    },
    /// Parenthesized expression for explicit grouping
    Grouping(Box<Self>),
    /// Raw Rust code that couldn't be parsed by the DSL
    /// This is a fallback for complex expressions
    RawRust(String),
}

impl std::fmt::Display for Expression {
    /// Formats the expression (only supports literals and variables currently)
    ///
    /// # Panics
    ///
    /// * If the expression type is not yet implemented for display
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Literal(literal) => std::fmt::Display::fmt(literal, f),
            Self::Variable(variable) => std::fmt::Display::fmt(variable, f),
            Self::ElementRef(..) => unimplemented!("element_ref"),
            Self::ElementByIdRef(..) => unimplemented!("element_by_id_ref"),
            Self::Call { .. } => unimplemented!("call"),
            Self::MethodCall { .. } => unimplemented!("method_call"),
            Self::Field { .. } => unimplemented!("field"),
            Self::Binary { .. } => unimplemented!("binary"),
            Self::Unary { .. } => unimplemented!("unary"),
            Self::If { .. } => unimplemented!("if"),
            Self::Match { .. } => unimplemented!("match"),
            Self::Block(..) => unimplemented!("block"),
            Self::Array(..) => unimplemented!("array"),
            Self::Tuple(..) => unimplemented!("tuple"),
            Self::Range { .. } => unimplemented!("range"),
            Self::Closure { .. } => unimplemented!("closure"),
            Self::RawRust(_) => unimplemented!("raw_rust"),
            Self::Grouping(_) => unimplemented!("grouping"),
        }
    }
}

/// Binary operators
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum BinaryOp {
    /// Addition operator (+)
    Add,
    /// Subtraction operator (-)
    Subtract,
    /// Multiplication operator (*)
    Multiply,
    /// Division operator (/)
    Divide,
    /// Modulo operator (%)
    Modulo,

    /// Equality operator (==)
    Equal,
    /// Inequality operator (!=)
    NotEqual,
    /// Less than operator (<)
    Less,
    /// Less than or equal operator (<=)
    LessEqual,
    /// Greater than operator (>)
    Greater,
    /// Greater than or equal operator (>=)
    GreaterEqual,

    /// Logical AND operator (&&)
    And,
    /// Logical OR operator (||)
    Or,

    /// Bitwise AND operator (&)
    BitAnd,
    /// Bitwise OR operator (|)
    BitOr,
    /// Bitwise XOR operator (^)
    BitXor,
}

/// Unary operators
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum UnaryOp {
    /// Logical NOT operator (!)
    Not,
    /// Unary minus operator (-)
    Minus,
    /// Unary plus operator (+)
    Plus,
    /// Reference operator (&)
    Ref,
}

/// Literal values
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Literal {
    /// String literal
    String(String),
    /// Integer literal
    Integer(i64),
    /// Float literal
    Float(f64),
    /// Boolean literal
    Bool(bool),
    /// Unit literal (equivalent to `()`)
    Unit,
}

impl Literal {
    /// Creates a string literal
    #[must_use]
    pub fn string(x: impl Into<String>) -> Self {
        Self::String(x.into())
    }

    /// Creates an integer literal
    #[must_use]
    pub fn integer(x: impl Into<i64>) -> Self {
        Self::Integer(x.into())
    }

    /// Creates a float literal
    #[must_use]
    pub fn float(x: impl Into<f64>) -> Self {
        Self::Float(x.into())
    }

    /// Creates a boolean literal
    #[must_use]
    pub fn bool(x: impl Into<bool>) -> Self {
        Self::Bool(x.into())
    }
}

impl std::fmt::Display for Literal {
    /// Formats the literal value as a string
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::String(x) => f.write_str(x),
            Self::Integer(x) => write!(f, "{x}"),
            Self::Float(x) => write!(f, "{x}"),
            Self::Bool(x) => write!(f, "{x}"),
            Self::Unit => write!(f, ""),
        }
    }
}

/// DSL AST root
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Dsl {
    /// Top-level statements in the DSL program
    pub statements: Vec<Statement>,
}

/// Element reference type for object-oriented element manipulation
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ElementReference {
    /// The element selector (e.g., "#my-id", ".my-class")
    /// FIXME: This should just be a `ParseSelector`
    pub selector: String,
}

impl ElementReference {
    /// Parses the selector and determines its type
    #[must_use]
    pub fn parse_selector(&self) -> ParsedSelector {
        if self.selector.starts_with('#') {
            ParsedSelector::Id(self.selector[1..].to_string())
        } else if self.selector.starts_with('.') {
            ParsedSelector::Class(self.selector[1..].to_string())
        } else if self.selector.is_empty() {
            ParsedSelector::Invalid
        } else {
            // If it doesn't start with # or ., treat it as a string ID for backward compatibility
            ParsedSelector::Id(self.selector.clone())
        }
    }
}

/// Parsed selector type to determine the correct function to call
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum ParsedSelector {
    /// ID selector (#my-id -> my-id)
    Id(String),
    /// Class selector (.my-class -> my-class)
    Class(String),
    /// Complex selector (for future implementation)
    Complex(String),
    /// Invalid selector
    Invalid,
}

impl Dsl {
    /// Creates a new DSL AST
    #[must_use]
    pub const fn new(statements: Vec<Statement>) -> Self {
        Self { statements }
    }

    /// Evaluates the DSL and returns the resulting actions
    #[must_use]
    pub const fn evaluate(&self) -> Vec<ActionEffect> {
        // TODO: Implement DSL evaluation
        // For now, return empty vector
        Vec::new()
    }
}

/// Built-in functions available in the DSL
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum BuiltinFunction {
    // Element targeting
    /// Hide an element
    Hide,
    /// Show an element
    Show,
    /// Toggle element visibility
    Toggle,
    /// Set element visibility
    SetVisibility,
    /// Set element display property
    SetDisplay,
    /// Set element background
    SetBackground,

    // Element references
    /// Get element reference
    Element,

    // Getters
    /// Get element visibility
    GetVisibility,
    /// Get element display property
    GetDisplay,
    /// Get element width
    GetWidth,
    /// Get element height
    GetHeight,
    /// Get element X position
    GetPositionX,
    /// Get element Y position
    GetPositionY,
    /// Get mouse X coordinate
    GetMouseX,
    /// Get mouse Y coordinate
    GetMouseY,
    /// Get element data attribute
    GetDataAttr,
    /// Get event value
    GetEventValue,

    // Utilities
    /// No operation
    NoOp,
    /// Log a message
    Log,
    /// Navigate to URL
    Navigate,
    /// Custom action
    Custom,

    // Control flow helpers
    /// If conditional
    If,
    /// Match expression
    Match,

    // Arithmetic
    /// Addition operation
    Add,
    /// Subtraction operation
    Subtract,
    /// Multiplication operation
    Multiply,
    /// Division operation
    Divide,
    /// Minimum of two values
    Min,
    /// Maximum of two values
    Max,
    /// Clamp value between min and max
    Clamp,

    // Logic
    /// Equality comparison
    Eq,
    /// Logical AND operation
    And,
    /// Logical OR operation
    Or,
    /// Logical NOT operation
    Not,
}

/// Evaluation context for DSL expressions
#[derive(Clone, Debug, Default)]
pub struct EvalContext {
    /// Variables in scope
    pub variables: std::collections::BTreeMap<String, DslValue>,
}

/// DSL values that can be stored in variables or used in expressions
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum DslValue {
    /// String value
    String(String),
    /// Numeric value
    Number(f64),
    /// Boolean value
    Bool(bool),
    /// Visibility value
    Visibility(Visibility),
    /// Element target
    Target(ElementTarget),
    /// Element reference for object-oriented API
    ElementRef(ElementReference),
    /// Action effect
    Action(ActionEffect),
    /// List of values
    List(Vec<Self>),
    /// Unit value
    Unit,
}

impl From<Literal> for DslValue {
    /// Converts a `Literal` into a `DslValue`
    fn from(lit: Literal) -> Self {
        #[allow(clippy::cast_precision_loss)]
        match lit {
            Literal::String(s) => Self::String(s),
            Literal::Integer(i) => Self::Number(i as f64),
            Literal::Float(f) => Self::Number(f),
            Literal::Bool(b) => Self::Bool(b),
            Literal::Unit => Self::Unit,
        }
    }
}

impl From<DslValue> for ActionEffect {
    /// Converts a `DslValue` into an `ActionEffect`
    ///
    /// For element references, creates a custom action. For non-action values,
    /// creates a no-op action.
    fn from(value: DslValue) -> Self {
        match value {
            DslValue::Action(action) => action,
            DslValue::ElementRef(element_ref) => {
                // For element references, create a custom action that stores the element reference
                Self {
                    action: ActionType::Custom {
                        action: format!("element_ref:{}", element_ref.selector),
                    },
                    delay_off: None,
                    throttle: None,
                    unique: None,
                }
            }
            _ => Self {
                action: ActionType::NoOp,
                delay_off: None,
                throttle: None,
                unique: None,
            },
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // ============================================
    // ElementReference::parse_selector tests
    // ============================================

    #[test_log::test]
    fn test_parse_selector_id_with_hash() {
        let element_ref = ElementReference {
            selector: "#my-element".to_string(),
        };

        let parsed = element_ref.parse_selector();

        assert_eq!(parsed, ParsedSelector::Id("my-element".to_string()));
    }

    #[test_log::test]
    fn test_parse_selector_class_with_dot() {
        let element_ref = ElementReference {
            selector: ".my-class".to_string(),
        };

        let parsed = element_ref.parse_selector();

        assert_eq!(parsed, ParsedSelector::Class("my-class".to_string()));
    }

    #[test_log::test]
    fn test_parse_selector_bare_string_as_id() {
        // Bare string without prefix should be treated as ID for backward compatibility
        let element_ref = ElementReference {
            selector: "my-element-id".to_string(),
        };

        let parsed = element_ref.parse_selector();

        assert_eq!(parsed, ParsedSelector::Id("my-element-id".to_string()));
    }

    #[test_log::test]
    fn test_parse_selector_empty_string() {
        let element_ref = ElementReference {
            selector: String::new(),
        };

        let parsed = element_ref.parse_selector();

        assert_eq!(parsed, ParsedSelector::Invalid);
    }

    #[test_log::test]
    fn test_parse_selector_hash_only() {
        let element_ref = ElementReference {
            selector: "#".to_string(),
        };

        let parsed = element_ref.parse_selector();

        // "#" parses as ID with empty string
        assert_eq!(parsed, ParsedSelector::Id(String::new()));
    }

    #[test_log::test]
    fn test_parse_selector_dot_only() {
        let element_ref = ElementReference {
            selector: ".".to_string(),
        };

        let parsed = element_ref.parse_selector();

        // "." parses as Class with empty string
        assert_eq!(parsed, ParsedSelector::Class(String::new()));
    }

    #[test_log::test]
    fn test_parse_selector_special_characters() {
        let element_ref = ElementReference {
            selector: "#element-with-dash_and_underscore".to_string(),
        };

        let parsed = element_ref.parse_selector();

        assert_eq!(
            parsed,
            ParsedSelector::Id("element-with-dash_and_underscore".to_string())
        );
    }

    // ============================================
    // ActionEffect From<DslValue> tests
    // ============================================

    #[test_log::test]
    fn test_action_effect_from_dsl_value_action() {
        let action_effect = ActionEffect {
            action: ActionType::NoOp,
            delay_off: Some(100),
            throttle: Some(200),
            unique: Some(true),
        };
        let dsl_value = DslValue::Action(action_effect);

        let result: ActionEffect = dsl_value.into();

        assert_eq!(result.action, ActionType::NoOp);
        assert_eq!(result.delay_off, Some(100));
        assert_eq!(result.throttle, Some(200));
        assert_eq!(result.unique, Some(true));
    }

    #[test_log::test]
    fn test_action_effect_from_dsl_value_element_ref() {
        let element_ref = ElementReference {
            selector: "#my-element".to_string(),
        };
        let dsl_value = DslValue::ElementRef(element_ref);

        let result: ActionEffect = dsl_value.into();

        match result.action {
            ActionType::Custom { action } => {
                assert_eq!(action, "element_ref:#my-element");
            }
            _ => panic!("Expected Custom action type"),
        }
        assert_eq!(result.delay_off, None);
        assert_eq!(result.throttle, None);
        assert_eq!(result.unique, None);
    }

    #[test_log::test]
    fn test_action_effect_from_dsl_value_non_action() {
        // Non-action DslValues should convert to NoOp
        let dsl_value = DslValue::String("test".to_string());

        let result: ActionEffect = dsl_value.into();

        assert_eq!(result.action, ActionType::NoOp);
        assert_eq!(result.delay_off, None);
        assert_eq!(result.throttle, None);
        assert_eq!(result.unique, None);
    }

    // ============================================
    // ElementVariable tests
    // ============================================

    #[test_log::test]
    fn test_element_variable_creates_target_ref_not_literal() {
        // The key behavior of ElementVariable is that it creates Target::Ref
        // rather than Target::Literal, enabling runtime resolution of element names
        let element_var = ElementVariable {
            name: "my-element".to_string(),
        };

        // Test style action uses Ref target
        let style_action = element_var.clone().show();
        match style_action {
            ActionType::Style { target, .. } => {
                match target {
                    ElementTarget::ById(target_ref) => {
                        // Verify it's a Ref (not Literal) and contains the variable name
                        assert!(
                            matches!(&target_ref, Target::Ref(name) if name == "my-element"),
                            "Expected Target::Ref with variable name, got {target_ref:?}",
                        );
                    }
                    _ => panic!("Expected ElementTarget::ById"),
                }
            }
            _ => panic!("Expected Style action"),
        }

        // Test input action also uses Ref target
        let input_action = element_var.select();
        match input_action {
            ActionType::Input(crate::InputActionType::Select { target }) => match target {
                ElementTarget::ById(target_ref) => {
                    assert!(
                        matches!(&target_ref, Target::Ref(name) if name == "my-element"),
                        "Expected Target::Ref with variable name, got {target_ref:?}",
                    );
                }
                _ => panic!("Expected ElementTarget::ById"),
            },
            _ => panic!("Expected Input Select action"),
        }
    }

    #[test_log::test]
    fn test_element_variable_hide_uses_target_ref() {
        let element_var = ElementVariable {
            name: "target-element".to_string(),
        };

        let action = element_var.hide();
        match action {
            ActionType::Style { target, action } => {
                match target {
                    ElementTarget::ById(target_ref) => {
                        assert!(
                            matches!(&target_ref, Target::Ref(name) if name == "target-element"),
                            "Expected Target::Ref, got {target_ref:?}",
                        );
                    }
                    _ => panic!("Expected ElementTarget::ById"),
                }
                assert_eq!(
                    action,
                    crate::StyleAction::SetVisibility(Visibility::Hidden)
                );
            }
            _ => panic!("Expected Style action"),
        }
    }

    #[test_log::test]
    fn test_element_variable_focus_uses_target_ref() {
        let element_var = ElementVariable {
            name: "focus-target".to_string(),
        };

        let action = element_var.focus();
        match action {
            ActionType::Style { target, action } => {
                match target {
                    ElementTarget::ById(target_ref) => {
                        assert!(
                            matches!(&target_ref, Target::Ref(name) if name == "focus-target"),
                            "Expected Target::Ref, got {target_ref:?}",
                        );
                    }
                    _ => panic!("Expected ElementTarget::ById"),
                }
                assert_eq!(action, crate::StyleAction::SetFocus(true));
            }
            _ => panic!("Expected Style action"),
        }
    }

    #[test_log::test]
    fn test_element_variable_display_methods() {
        let element_var = ElementVariable {
            name: "display-element".to_string(),
        };

        // Test display()
        let action = element_var.clone().display();
        match action {
            ActionType::Style { target, action } => {
                match target {
                    ElementTarget::ById(target_ref) => {
                        assert!(
                            matches!(&target_ref, Target::Ref(name) if name == "display-element")
                        );
                    }
                    _ => panic!("Expected ElementTarget::ById"),
                }
                assert_eq!(action, crate::StyleAction::SetDisplay(true));
            }
            _ => panic!("Expected Style action"),
        }

        // Test no_display()
        let action = element_var.clone().no_display();
        match action {
            ActionType::Style { action, .. } => {
                assert_eq!(action, crate::StyleAction::SetDisplay(false));
            }
            _ => panic!("Expected Style action"),
        }

        // Test set_display(false)
        let action = element_var.set_display(false);
        match action {
            ActionType::Style { action, .. } => {
                assert_eq!(action, crate::StyleAction::SetDisplay(false));
            }
            _ => panic!("Expected Style action"),
        }
    }

    #[test_log::test]
    fn test_element_variable_set_visibility() {
        let element_var = ElementVariable {
            name: "visibility-element".to_string(),
        };

        let action = element_var.set_visibility(Visibility::Hidden);
        match action {
            ActionType::Style { target, action } => {
                match target {
                    ElementTarget::ById(target_ref) => {
                        assert!(
                            matches!(&target_ref, Target::Ref(name) if name == "visibility-element")
                        );
                    }
                    _ => panic!("Expected ElementTarget::ById"),
                }
                assert_eq!(
                    action,
                    crate::StyleAction::SetVisibility(Visibility::Hidden)
                );
            }
            _ => panic!("Expected Style action"),
        }
    }

    // ============================================
    // DslValue to ActionEffect conversion tests
    // ============================================

    #[test_log::test]
    fn test_action_effect_from_dsl_value_number() {
        let dsl_value = DslValue::Number(42.5);

        let result: ActionEffect = dsl_value.into();

        // Non-action DslValues convert to NoOp
        assert_eq!(result.action, ActionType::NoOp);
    }

    #[test_log::test]
    fn test_action_effect_from_dsl_value_bool() {
        let dsl_value = DslValue::Bool(true);

        let result: ActionEffect = dsl_value.into();

        // Non-action DslValues convert to NoOp
        assert_eq!(result.action, ActionType::NoOp);
    }

    #[test_log::test]
    fn test_action_effect_from_dsl_value_visibility() {
        let dsl_value = DslValue::Visibility(Visibility::Hidden);

        let result: ActionEffect = dsl_value.into();

        // Non-action DslValues convert to NoOp
        assert_eq!(result.action, ActionType::NoOp);
    }

    #[test_log::test]
    fn test_action_effect_from_dsl_value_unit() {
        let dsl_value = DslValue::Unit;

        let result: ActionEffect = dsl_value.into();

        assert_eq!(result.action, ActionType::NoOp);
    }

    #[test_log::test]
    fn test_action_effect_from_dsl_value_list() {
        let dsl_value = DslValue::List(vec![DslValue::Number(1.0), DslValue::Number(2.0)]);

        let result: ActionEffect = dsl_value.into();

        assert_eq!(result.action, ActionType::NoOp);
    }

    // ============================================
    // Literal conversion tests
    // ============================================

    #[test_log::test]
    fn test_dsl_value_from_literal_string() {
        let literal = Literal::String("test".to_string());
        let value: DslValue = literal.into();

        assert_eq!(value, DslValue::String("test".to_string()));
    }

    #[test_log::test]
    fn test_dsl_value_from_literal_integer() {
        let literal = Literal::Integer(42);
        let value: DslValue = literal.into();

        assert_eq!(value, DslValue::Number(42.0));
    }

    #[test_log::test]
    fn test_dsl_value_from_literal_float() {
        let literal = Literal::Float(4.567);
        let value: DslValue = literal.into();

        assert_eq!(value, DslValue::Number(4.567));
    }

    #[test_log::test]
    fn test_dsl_value_from_literal_bool() {
        let literal = Literal::Bool(true);
        let value: DslValue = literal.into();

        assert_eq!(value, DslValue::Bool(true));
    }

    #[test_log::test]
    fn test_dsl_value_from_literal_unit() {
        let literal = Literal::Unit;
        let value: DslValue = literal.into();

        assert_eq!(value, DslValue::Unit);
    }

    // ============================================
    // Literal Display tests
    // ============================================

    #[test_log::test]
    fn test_literal_display_string() {
        let literal = Literal::String("hello".to_string());
        assert_eq!(format!("{literal}"), "hello");
    }

    #[test_log::test]
    fn test_literal_display_integer() {
        let literal = Literal::Integer(42);
        assert_eq!(format!("{literal}"), "42");
    }

    #[test_log::test]
    fn test_literal_display_float() {
        let literal = Literal::Float(4.567);
        assert_eq!(format!("{literal}"), "4.567");
    }

    #[test_log::test]
    fn test_literal_display_bool() {
        let literal = Literal::Bool(true);
        assert_eq!(format!("{literal}"), "true");

        let literal_false = Literal::Bool(false);
        assert_eq!(format!("{literal_false}"), "false");
    }

    #[test_log::test]
    fn test_literal_display_unit() {
        let literal = Literal::Unit;
        assert_eq!(format!("{literal}"), "");
    }

    // ============================================
    // Literal constructor tests
    // ============================================

    #[test_log::test]
    fn test_literal_constructors() {
        let string_lit = Literal::string("test");
        assert_eq!(string_lit, Literal::String("test".to_string()));

        let int_lit = Literal::integer(42i32);
        assert_eq!(int_lit, Literal::Integer(42));

        let float_lit = Literal::float(4.567f32);
        // Approximate equality due to float conversion
        match float_lit {
            Literal::Float(f) => assert!((f - 4.567).abs() < 0.001),
            _ => panic!("Expected Float literal"),
        }

        let bool_lit = Literal::bool(true);
        assert_eq!(bool_lit, Literal::Bool(true));
    }

    // ============================================
    // ElementVariable logic feature method tests
    // ============================================
    //
    // These methods are only available when the "logic" feature is enabled.
    // They create CalcValue instances that use Target::Ref to enable
    // runtime resolution of element names from variables.

    #[cfg(feature = "logic")]
    #[test_log::test]
    fn test_element_variable_toggle_visibility() {
        let element_var = ElementVariable {
            name: "toggle-element".to_string(),
        };

        let action = element_var.toggle_visibility();

        // toggle_visibility creates a Logic action with If condition
        match action {
            ActionType::Logic(if_action) => {
                // The condition should check visibility of the element
                match &if_action.condition {
                    crate::logic::Condition::Eq(left, _) => match left {
                        crate::logic::Value::Calc(crate::logic::CalcValue::Visibility {
                            target,
                        }) => match target {
                            ElementTarget::ById(target_ref) => {
                                assert!(
                                    matches!(target_ref, Target::Ref(name) if name == "toggle-element"),
                                    "Expected Target::Ref for variable, got {target_ref:?}",
                                );
                            }
                            _ => panic!("Expected ElementTarget::ById"),
                        },
                        _ => panic!("Expected CalcValue::Visibility"),
                    },
                    crate::logic::Condition::Bool(_) => panic!("Expected Eq condition"),
                }
            }
            _ => panic!("Expected Logic action type"),
        }
    }

    #[cfg(feature = "logic")]
    #[test_log::test]
    fn test_element_variable_visibility_returns_calc_value() {
        let element_var = ElementVariable {
            name: "visibility-var".to_string(),
        };

        let calc_value = element_var.visibility();

        match calc_value {
            crate::logic::CalcValue::Visibility { target } => match target {
                ElementTarget::ById(target_ref) => {
                    assert!(
                        matches!(&target_ref, Target::Ref(name) if name == "visibility-var"),
                        "Expected Target::Ref for variable, got {target_ref:?}",
                    );
                }
                _ => panic!("Expected ElementTarget::ById"),
            },
            _ => panic!("Expected CalcValue::Visibility"),
        }
    }

    #[cfg(feature = "logic")]
    #[test_log::test]
    fn test_element_variable_toggle_display() {
        let element_var = ElementVariable {
            name: "display-toggle".to_string(),
        };

        let action = element_var.toggle_display();

        // toggle_display creates a Logic action
        match action {
            ActionType::Logic(if_action) => match &if_action.condition {
                crate::logic::Condition::Eq(left, _) => match left {
                    crate::logic::Value::Calc(crate::logic::CalcValue::Display { target }) => {
                        match target {
                            ElementTarget::ById(target_ref) => {
                                assert!(
                                    matches!(target_ref, Target::Ref(name) if name == "display-toggle"),
                                    "Expected Target::Ref, got {target_ref:?}",
                                );
                            }
                            _ => panic!("Expected ElementTarget::ById"),
                        }
                    }
                    _ => panic!("Expected CalcValue::Display"),
                },
                crate::logic::Condition::Bool(_) => panic!("Expected Eq condition"),
            },
            _ => panic!("Expected Logic action type"),
        }
    }

    #[cfg(feature = "logic")]
    #[test_log::test]
    fn test_element_variable_get_display_returns_calc_value() {
        let element_var = ElementVariable {
            name: "get-display-var".to_string(),
        };

        let calc_value = element_var.get_display();

        match calc_value {
            crate::logic::CalcValue::Display { target } => match target {
                ElementTarget::ById(target_ref) => {
                    assert!(
                        matches!(&target_ref, Target::Ref(name) if name == "get-display-var"),
                        "Expected Target::Ref, got {target_ref:?}",
                    );
                }
                _ => panic!("Expected ElementTarget::ById"),
            },
            _ => panic!("Expected CalcValue::Display"),
        }
    }

    #[cfg(feature = "logic")]
    #[test_log::test]
    fn test_element_variable_get_width_px_returns_calc_value() {
        let element_var = ElementVariable {
            name: "width-element".to_string(),
        };

        let calc_value = element_var.get_width_px();

        match calc_value {
            crate::logic::CalcValue::WidthPx { target } => match target {
                ElementTarget::ById(target_ref) => {
                    assert!(
                        matches!(&target_ref, Target::Ref(name) if name == "width-element"),
                        "Expected Target::Ref, got {target_ref:?}",
                    );
                }
                _ => panic!("Expected ElementTarget::ById"),
            },
            _ => panic!("Expected CalcValue::WidthPx"),
        }
    }

    #[cfg(feature = "logic")]
    #[test_log::test]
    fn test_element_variable_get_height_px_returns_calc_value() {
        let element_var = ElementVariable {
            name: "height-element".to_string(),
        };

        let calc_value = element_var.get_height_px();

        match calc_value {
            crate::logic::CalcValue::HeightPx { target } => match target {
                ElementTarget::ById(target_ref) => {
                    assert!(
                        matches!(&target_ref, Target::Ref(name) if name == "height-element"),
                        "Expected Target::Ref, got {target_ref:?}",
                    );
                }
                _ => panic!("Expected ElementTarget::ById"),
            },
            _ => panic!("Expected CalcValue::HeightPx"),
        }
    }

    #[cfg(feature = "logic")]
    #[test_log::test]
    fn test_element_variable_get_mouse_x_returns_calc_value() {
        let element_var = ElementVariable {
            name: "mouse-x-element".to_string(),
        };

        let calc_value = element_var.get_mouse_x();

        match calc_value {
            crate::logic::CalcValue::MouseX { target } => match target {
                Some(ElementTarget::ById(target_ref)) => {
                    assert!(
                        matches!(&target_ref, Target::Ref(name) if name == "mouse-x-element"),
                        "Expected Target::Ref, got {target_ref:?}",
                    );
                }
                _ => panic!("Expected Some(ElementTarget::ById)"),
            },
            _ => panic!("Expected CalcValue::MouseX"),
        }
    }

    #[cfg(feature = "logic")]
    #[test_log::test]
    fn test_element_variable_get_mouse_y_returns_calc_value() {
        let element_var = ElementVariable {
            name: "mouse-y-element".to_string(),
        };

        let calc_value = element_var.get_mouse_y();

        match calc_value {
            crate::logic::CalcValue::MouseY { target } => match target {
                Some(ElementTarget::ById(target_ref)) => {
                    assert!(
                        matches!(&target_ref, Target::Ref(name) if name == "mouse-y-element"),
                        "Expected Target::Ref, got {target_ref:?}",
                    );
                }
                _ => panic!("Expected Some(ElementTarget::ById)"),
            },
            _ => panic!("Expected CalcValue::MouseY"),
        }
    }
}