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
//! Handle Dilla Renderable.

use crate::{
    attribute::{Attribute, KEY_ATTRIBUTES},
    renderer::Renderer,
    DEFINITION, KEY_PREFIX,
};

use minijinja::{context, value::ValueKind, Environment};
use serde::{Deserialize, Serialize};
use serde_json::{json, Map};

/// Component renderable key name.
pub const KEY_COMPONENT: &str = "component";
pub const KEY_COMPONENT_VARIANT: &str = "variant";
pub const SEP_COMPONENT_VARIANT: &str = ".";

/// Element renderable key name.
pub const KEY_ELEMENT: &str = "element";
const KEY_ELEMENT_CONTENT: &str = "content";
const VOID_ELEMENTS: &[&str] = &[
    "area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "source", "track",
    "wbr", "use",
];

/// Name of the template property.
pub const KEY_TEMPLATE: &str = "template";

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub enum RenderableType {
    Component,
    Element,
    Template,
    #[default]
    Unknown,
}

/// This struct represents a renderable object.
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct Renderable {
    attributes: Attribute,
    data: Map<String, serde_json::Value>,
    fields: Map<String, serde_json::Value>,
    output: String,
    renderable_type: RenderableType,
    /// Component: Name of the component, used to resolve template.
    component_name: String,
    /// Component: Template name of the component.
    component_template: String,
    // Template: Source of the template.
    template_source: String,
    // Element: HTML tag
    element_tag: String,
    // Element: content property.
    element_content: String,
}

impl Renderable {
    /// Creates a new [`Renderable`].
    pub fn new(data: Map<String, serde_json::Value>) -> Self {
        Self {
            data,
            attributes: Attribute::new(),
            fields: Map::new(),
            output: String::new(),
            renderable_type: RenderableType::Unknown,
            component_name: String::new(),
            component_template: String::new(),
            template_source: String::new(),
            element_tag: String::new(),
            element_content: String::new(),
        }
    }

    pub fn add_attr(&mut self, name: &str, class: String) {
        self.attributes.add_attr(name, vec![class])
    }

    pub fn output(&self) -> String {
        self.output.clone() as _
    }

    pub fn build(&mut self) -> &mut Self {
        self.set_type_from_data();

        if self.renderable_type == RenderableType::Element {
            self.set_element_content_string()
        }

        self.set_renderable_values();
        self.render();

        self
    }

    pub fn build_with_env(&mut self, env: &mut Environment, ctx: minijinja::Value) -> &mut Self {
        self.set_type_from_data();

        if self.renderable_type == RenderableType::Element {
            self.set_element_content(env)
        }

        self.set_renderable_values();
        self.render_with_env(env, ctx);

        self
    }

    fn render_with_env(&mut self, env: &mut Environment, ctx: minijinja::Value) {
        if self.renderable_type == RenderableType::Template {
            self.output = self.render_template(env, ctx)
        } else if self.renderable_type == RenderableType::Component {
            self.output = self.render_component(env, ctx)
        } else if self.renderable_type == RenderableType::Element {
            self.render_element();
        }
    }

    fn render(&mut self) {
        if self.renderable_type == RenderableType::Element {
            self.render_element();
        } else {
            self.output = format!("[Warn] no render for: {:?}", self.renderable_type)
        }
    }

    fn render_element(&mut self) {
        // Differentiate tag with closing and void elements without content.
        if VOID_ELEMENTS.contains(&self.element_tag.as_str()) {
            self.output = format!(
                "<{tag}{attributes} />",
                tag = self.element_tag,
                attributes = self.attributes,
            );
            return;
        }

        self.output = format!(
            "<{tag}{attributes}>{content}</{tag}>",
            tag = self.element_tag,
            attributes = self.attributes,
            content = self.element_content,
        )
    }

    fn render_component(&mut self, env: &mut Environment, ctx: minijinja::Value) -> String {
        // Merge context values to have translation and fields.
        // @todo have fields directly as minijinja::value?
        let ctx_fields = minijinja::value::Value::from_serializable(&self.fields);
        let ctx = context! { ..ctx, ..ctx_fields };

        // Add attributes object to the template for manipulation and functions.
        // @todo use or not?
        // env.add_global(
        //     "attributes",
        //     minijinja::value::Value::from_object(component.attributes()),
        // );

        let template = env.get_template(self.component_template.as_str());
        if template.is_err() {
            #[cfg(feature = "debug")]
            println!("<!-- Debug\n{:?}\n-->", &env);
            return format!(
                "<!-- [Error] component: {}, template not found: {} -->",
                self.component_name, self.component_template
            );
        }

        template.unwrap().render(&ctx).unwrap()
    }

    fn render_template(&mut self, env: &mut Environment, ctx: minijinja::Value) -> String {
        let mut env: Environment = env.clone();

        env.add_template("inline", &self.template_source).unwrap();
        let template = env.get_template("inline").unwrap();

        // Merge context values to have translation and fields.
        let ctx_fields = minijinja::value::Value::from_serializable(&self.data);
        let ctx = context! { ..ctx, ..ctx_fields };

        let output = template.render(&ctx).unwrap();

        // @todo do we need to?
        env.remove_template("inline");

        output
    }

    fn set_type_from_data(&mut self) {
        let component_key: String = format!("{KEY_PREFIX}{KEY_COMPONENT}");
        let element_key: String = format!("{KEY_PREFIX}{KEY_ELEMENT}");
        let template_key: String = format!("{KEY_PREFIX}{KEY_TEMPLATE}");

        self.renderable_type = match (
            self.data.get(&element_key),
            self.data.get(&component_key),
            self.data.get(&template_key),
        ) {
            (Some(_), _, _) => RenderableType::Element,
            (_, Some(_), _) => RenderableType::Component,
            (_, _, Some(_)) => RenderableType::Template,
            _ => RenderableType::Unknown,
        }
    }

    fn set_renderable_values(&mut self) {
        if self.renderable_type == RenderableType::Component {
            self.set_component_name();
            self.set_component_name();
            self.set_fields();
            self.set_component_template();
            self.set_component_attributes();
        } else if self.renderable_type == RenderableType::Template {
            self.set_template_source();
            self.set_fields();
        } else if self.renderable_type == RenderableType::Element {
            self.set_element_tag();
            self.set_element_attributes();
        }
    }

    fn set_type(&mut self, renderable_type: RenderableType) {
        self.renderable_type = renderable_type;
    }

    fn set_tag(&mut self, tag: String) {
        self.element_tag = tag;
    }

    fn set_attributes(&mut self, attributes: Attribute) {
        self.attributes = attributes;
    }

    fn set_fields(&mut self) {
        self.fields = self
            .data
            .iter()
            .filter(|(k, _)| !k.starts_with(KEY_PREFIX))
            .map(|(k, v)| (k.clone(), v.clone()))
            .collect();
    }

    fn set_element_tag(&mut self) {
        self.element_tag = self
            .data
            .get(&format!("{KEY_PREFIX}{KEY_ELEMENT}"))
            .unwrap()
            .as_str()
            .unwrap_or_default()
            .to_string();
    }

    fn set_element_content(&mut self, env: &mut Environment) {
        // Element content need to access env and create a new renderer.
        let content_key = format!("{KEY_PREFIX}{KEY_ELEMENT_CONTENT}");

        if self.data.contains_key(&content_key) {
            let content_element = self.data.get(&content_key).unwrap();

            let mut single_renderer = Renderer::new();
            single_renderer.do_render(&[content_element.to_owned()], env);

            self.element_content = single_renderer.output.body;
        }
    }

    fn set_element_content_string(&mut self) {
        let content_key = format!("{KEY_PREFIX}{KEY_ELEMENT_CONTENT}");

        if self.data.contains_key(&content_key) {
            let content_element = self.data.get(&content_key).unwrap();
            if content_element.is_string() {
                // @todo set a @raw key or @markup to allow some html tags?
                // self.element_content = HtmlEscape(content_element.as_str().unwrap()).to_string();
                self.element_content = content_element.as_str().unwrap().to_string();
            } else if content_element.is_array() {
                for cont in content_element.as_array().unwrap().iter() {
                    // self.element_content
                    //     .push_str(&HtmlEscape(cont.as_str().unwrap()).to_string());
                    self.element_content.push_str(cont.as_str().unwrap());
                }
            }
        }
    }

    fn set_element_attributes(&mut self) {
        let attributes: &serde_json::Value = &self
            .data
            .iter()
            .filter(|(k, _)| !k.starts_with(KEY_PREFIX))
            .map(|(k, v)| (k.clone(), v.clone()))
            .collect();

        let mut obj_attributes = Attribute::new();

        obj_attributes.build_scoped(&self.data);
        obj_attributes.add_attr_from_serde(attributes);

        self.attributes = obj_attributes;
    }

    /// Shortcut to generate a specific `link` HTML tag with Element.
    pub fn link(href: &str, attrs: &serde_json::Value) -> Self {
        let mut defined_attributes = Attribute::new();

        defined_attributes.add_attr("type", vec!["text/css"]);
        defined_attributes.add_attr("rel", vec!["stylesheet"]);
        defined_attributes.add_attr("href", vec![href]);

        defined_attributes.add_attr_from_serde(attrs);

        let mut renderable = Renderable::default();
        renderable.set_type(RenderableType::Element);
        renderable.set_tag("link".to_string());
        renderable.set_attributes(defined_attributes);
        renderable.render();

        renderable
    }

    /// Shortcut to generate a specific `script` HTML tag with Element.
    pub fn script(src: &str, attrs: &serde_json::Value) -> Self {
        let mut defined_attributes = Attribute::new();

        defined_attributes.add_attr("src", vec![src]);

        defined_attributes.add_attr_from_serde(attrs);

        let mut renderable = Renderable::default();
        renderable.set_type(RenderableType::Element);
        renderable.set_tag("script".to_string());
        renderable.set_attributes(defined_attributes);
        renderable.render();

        renderable
    }

    fn set_template_source(&mut self) {
        let template_key: String = format!("{KEY_PREFIX}{KEY_TEMPLATE}");

        self.template_source = match self.data.get(&template_key) {
            Some(serde_json::Value::String(value)) => value.to_string(),
            _ => "".to_string(),
        };
    }

    // Set name and trim to avoid error.
    fn set_component_name(&mut self) {
        let key_component = KEY_PREFIX.to_string() + KEY_COMPONENT;

        self.component_name = match self
            .data
            .get(&key_component)
            .unwrap_or(&serde_json::Value::from(""))
            .as_str()
        {
            Some(name) if !name.is_empty() => name.trim().to_string(),
            _ => String::new(),
        };
    }

    // Detect @variant injected from config to add a slot with the related value.
    fn set_component_template(&mut self) {
        let key_variant = KEY_PREFIX.to_string() + KEY_COMPONENT_VARIANT;

        if self.data.get(&key_variant).is_some() {
            let variant = self.data.get(&key_variant).unwrap();

            // Insert the 'variant' property to be used in Jinja template.
            self.fields
                .insert(KEY_COMPONENT_VARIANT.to_owned(), variant.to_owned());

            if let Some(variants) = DEFINITION
                .components_variant_template
                .get(&self.component_name)
            {
                if variants.contains(&variant.as_str().unwrap()) {
                    self.component_template = self.component_name.to_string()
                        + SEP_COMPONENT_VARIANT
                        + variant.as_str().unwrap();
                    return;
                }
            }
        }

        self.component_template = self.component_name.clone()
    }

    fn set_component_attributes(&mut self) {
        // If `@attributes`, use it as Attribute and ignore `attributes`
        // If NOT `@attributes` and `attributes`, use it as Attribute
        let key_attributes = KEY_PREFIX.to_string() + KEY_ATTRIBUTES;

        let attributes = self.data.get(&key_attributes).unwrap_or_else(|| {
            self.data
                .get(KEY_ATTRIBUTES)
                .unwrap_or(&serde_json::Value::Null)
        });

        let mut obj_attributes = Attribute::new();

        obj_attributes.build_scoped(&self.data);
        if !attributes.is_null() {
            obj_attributes.build_attributes(attributes);
        }

        self.fields
            .insert(KEY_ATTRIBUTES.to_owned(), obj_attributes.into());
    }
}

impl Html for Renderable {
    fn to_html_string(&self) -> String {
        self.output()
    }
}

impl From<Renderable> for minijinja::Value {
    fn from(renderable: Renderable) -> Self {
        minijinja::Value::from_serializable(&renderable)
    }
}

impl From<minijinja::Value> for Renderable {
    fn from(v: minijinja::Value) -> Self {
        let json = serde_json::to_string(&v).unwrap();
        let data: serde_json::Map<String, serde_json::Value> = serde_json::from_str(&json).unwrap();
        let mut renderable = Renderable::new(data);
        renderable.build();

        renderable
    }
}

pub trait Html: std::fmt::Debug {
    fn to_html_string(&self) -> String;
}

impl std::fmt::Display for dyn Html {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self.to_html_string())
    }
}

impl Html for String {
    fn to_html_string(&self) -> String {
        self.clone()
    }
}

impl Html for &str {
    fn to_html_string(&self) -> String {
        self.to_string()
    }
}

/// Checks if a MiniJinja Value contains a renderable element.
///
/// This function takes an immutable reference to a minijinja::Value and checks if it contains
/// a renderable element.
///
/// # Arguments
///
/// * value - An immutable reference to the minijinja::Value that needs to be checked.
///
/// # Returns
///
/// * true if the value contains a renderable element, otherwise false.
///
pub fn is_renderable(value: &minijinja::Value) -> bool {
    if let Ok(mut iter) = value.try_iter() {
        iter.any(|key| match key.as_str() {
            Some(name) => {
                name == format!("{}{}", KEY_PREFIX, KEY_COMPONENT)
                    || name == format!("{}{}", KEY_PREFIX, KEY_ELEMENT)
                    || name == format!("{}{}", KEY_PREFIX, KEY_TEMPLATE)
            }
            _ => false,
        })
    } else {
        false
    }
}

/// Detect the RenderableType of a Minijinja value.
///
/// # Arguments
///
/// * value - An immutable reference to the minijinja::Value that needs to be checked.
///
/// # Returns
///
/// * [RenderableType] for this value.
///
pub fn get_renderable_type_from(value: &minijinja::Value) -> RenderableType {
    if let Ok(iter) = value.try_iter() {
        for key in iter {
            if let Some(cmp) = key.as_str() {
                if cmp == format!("{}{}", KEY_PREFIX, KEY_ELEMENT) {
                    return RenderableType::Element;
                } else if cmp == format!("{}{}", KEY_PREFIX, KEY_COMPONENT) {
                    return RenderableType::Component;
                } else if cmp == format!("{}{}", KEY_PREFIX, KEY_TEMPLATE) {
                    return RenderableType::Template;
                }
            }
        }
    }
    RenderableType::Unknown
}

/// Add class to a payload by converting minijinja to [`serde_json::Map`] for manipulation.
///
/// # Arguments
///
/// * `v` - A reference to a `minijinja::Value` object.
/// * `class` - A `minijinja::Value` string representing the class to be added.
///
/// # Returns
///
/// A `minijinja::Value` object representing the updated data map.
///
pub fn add_class_to_json_component(
    v: &minijinja::Value,
    class: minijinja::Value,
) -> minijinja::Value {
    let mut data = convert_to_map(v);

    let key_attributes = KEY_PREFIX.to_string() + KEY_ATTRIBUTES;
    let key_to_update = if !data.contains_key(KEY_ATTRIBUTES) && !data.contains_key(&key_attributes)
    {
        if class.kind() == ValueKind::String {
            data.insert(key_attributes, json!({"class": class.to_string()}));
        } else if class.kind() == ValueKind::Seq {
            let mut classes: String = "".to_string();
            if let Ok(iter) = class.try_iter() {
                for val in iter {
                    classes.push_str(&format!(" {} ", &val.to_string()));
                }
                classes = classes.trim().to_string();
            }
            data.insert(key_attributes, json!({"class": classes}));
        }
        return minijinja::Value::from_serializable(&data);
    } else if data.contains_key(KEY_ATTRIBUTES) {
        KEY_ATTRIBUTES.to_string()
    } else if data.contains_key(&key_attributes) {
        key_attributes
    } else {
        return minijinja::Value::from_serializable(&data);
    };

    let attributes = data.get(&key_to_update).unwrap().as_object().unwrap();
    let mut new_data = attributes.clone();

    add_or_merge_attr(&mut new_data, "class", class);

    data.remove(&key_to_update);
    data.insert(key_to_update, serde_json::Value::Object(new_data));

    minijinja::Value::from_serializable(&data)
}

/// Adds a class attribute to a JSON component.
///
/// # Arguments
///
/// * `v` - A reference to a `minijinja::Value` that represents the JSON component to which the attribute should be added.
/// * `class` - A minijinja::Value that represents the class name to be added to the JSON element.
///
/// # Returns
///
/// * `minijinja::Value` - The updated JSON component with the added attribute.
pub fn add_class_to_json_element(
    v: &minijinja::Value,
    class: minijinja::Value,
) -> minijinja::Value {
    let mut data = convert_to_map(v);
    add_or_merge_attr(&mut data, "class", class);
    minijinja::Value::from_serializable(&data)
}

/// Adds an attribute to a JSON component.
///
/// # Arguments
///
/// * `v` - A reference to a `minijinja::Value` that represents the JSON component to which the attribute should be added.
/// * `name` - A `String` that represents the name of the attribute to be added.
/// * `value` - An `Option<minijinja::Value>` that represents the value of the attribute to be added. If `None`, an empty string is used as the value.
///
/// # Returns
///
/// * `minijinja::Value` - The updated JSON component with the added attribute.
pub fn add_attr_to_json_component(
    v: &minijinja::Value,
    name: String,
    value: Option<minijinja::Value>,
) -> minijinja::Value {
    let mut data = convert_to_map(v);
    let key_attributes = KEY_PREFIX.to_string() + KEY_ATTRIBUTES;
    let key_to_update;

    if !data.contains_key(KEY_ATTRIBUTES) && !data.contains_key(&key_attributes) {
        let value_str = value.map_or_else(|| "".to_string(), |v| v.to_string());
        data.insert(
            key_attributes,
            json!({
                name: value_str
            }),
        );
        return minijinja::Value::from_serializable(&data);
    } else if data.contains_key(KEY_ATTRIBUTES) {
        key_to_update = KEY_ATTRIBUTES.to_string();
    } else if data.contains_key(&key_attributes) {
        key_to_update = key_attributes;
    } else {
        return minijinja::Value::from_serializable(&data);
    }

    let attributes = data.get(&key_to_update).unwrap().as_object().unwrap();
    let mut new_data = attributes.clone();

    let value_str = value.map_or_else(|| "".to_string(), |v| v.to_string());
    add_or_merge_attr(&mut new_data, &name, minijinja::Value::from(value_str));

    data.remove(&key_to_update);
    data.insert(key_to_update, serde_json::Value::Object(new_data));

    minijinja::Value::from_serializable(&data)
}

/// Adds an attribute to a JSON element.
///
/// # Arguments
///
/// * `v` - A reference to the `minijinja::Value` that will be converted to a map and modified.
/// * `name` - The name of the attribute to be added to the JSON element.
/// * `value` - The value of the attribute to be added. If `None`, an empty string will be added.
///
/// # Returns
///
/// * `minijinja::Value` - The modified JSON element with the new attribute added.
pub fn add_attr_to_json_element(
    v: &minijinja::Value,
    name: String,
    value: Option<minijinja::Value>,
) -> minijinja::Value {
    let mut data = convert_to_map(v);
    if value.is_none() {
        data.insert(name, json!(""));
    } else {
        data.insert(name, json!(value.unwrap().to_string()));
    }
    minijinja::Value::from_serializable(&data)
}

fn add_or_merge_attr(
    data: &mut Map<String, serde_json::Value>,
    key: &str,
    value: minijinja::Value,
) {
    if !data.contains_key(key) {
        data.insert(key.to_string(), serde_json::Value::from(value.to_string()));
        return;
    }

    let existing_value = data.get_mut(key).unwrap();
    let mut merged_values: Vec<String> = Vec::new();

    // Check the type of the existing value
    match existing_value {
        serde_json::Value::Array(existing_array) => {
            // If the existing value is an array, convert each element to a string and add it to the merged values vector
            for val in existing_array.iter() {
                if let Some(string_val) = val.as_str() {
                    merged_values.push(string_val.to_owned());
                }
            }
        }
        serde_json::Value::String(existing_string) => {
            // If the existing value is a string, add it to the merged values vector
            merged_values.push(existing_string.to_owned());
        }
        _ => {}
    }

    merged_values.push(value.to_string());

    // Remove the existing key from the data map
    data.remove(key);

    // Insert the key back into the data map with the merged values vector as the value
    data.insert(key.to_string(), serde_json::Value::from(merged_values));
}

fn convert_to_map(value: &minijinja::Value) -> Map<String, serde_json::Value> {
    let json = serde_json::to_string(value).unwrap();
    serde_json::from_str(&json).unwrap()
}