-module(sketch@lustre@element). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([none/0, text/1, element/4, element_/3, namespaced/5, namespaced_/4, fragment/1, keyed/2, map/2, styled/1, unstyled/2]). -export_type([element/1]). -opaque element(QAR) :: nothing | {text, binary()} | {fragment, binary(), list(element(QAR))} | {map, fun(() -> element(QAR))} | {element, binary(), binary(), binary(), gleam@option:option(sketch@internals@style:class()), list(lustre@internals@vdom:attribute(QAR)), list(element(QAR))}. -spec none() -> element(any()). none() -> nothing. -spec text(binary()) -> element(any()). text(Content) -> {text, Content}. -spec element( binary(), sketch@internals@style:class(), list(lustre@internals@vdom:attribute(QAV)), list(element(QAV)) ) -> element(QAV). element(Tag, Class, Attributes, Children) -> Class@1 = {some, Class}, {element, <<""/utf8>>, <<""/utf8>>, Tag, Class@1, Attributes, Children}. -spec element_( binary(), list(lustre@internals@vdom:attribute(QBB)), list(element(QBB)) ) -> element(QBB). element_(Tag, Attributes, Children) -> {element, <<""/utf8>>, <<""/utf8>>, Tag, none, Attributes, Children}. -spec namespaced( binary(), binary(), sketch@internals@style:class(), list(lustre@internals@vdom:attribute(QBH)), list(element(QBH)) ) -> element(QBH). namespaced(Namespace, Tag, Class, Attributes, Children) -> Class@1 = {some, Class}, {element, <<""/utf8>>, Namespace, Tag, Class@1, Attributes, Children}. -spec namespaced_( binary(), binary(), list(lustre@internals@vdom:attribute(QBN)), list(element(QBN)) ) -> element(QBN). namespaced_(Namespace, Tag, Attributes, Children) -> {element, <<""/utf8>>, Namespace, Tag, none, Attributes, Children}. -spec fragment(list(element(QBT))) -> element(QBT). fragment(Children) -> {fragment, <<""/utf8>>, Children}. -spec do_keyed(element(QCE), binary()) -> element(QCE). do_keyed(Element, Key) -> case Element of nothing -> nothing; {text, Content} -> {text, Content}; {map, Subtree} -> {map, fun() -> do_keyed(Subtree(), Key) end}; {fragment, _, Children} -> _pipe = Children, _pipe@1 = gleam@list:index_map( _pipe, fun(Element@1, Idx) -> case Element@1 of {element, El_key, _, _, _, _, _} -> New_key = case El_key of <<""/utf8>> -> <<<>/binary, (gleam@int:to_string(Idx))/binary>>; _ -> <<<>/binary, El_key/binary>> end, do_keyed(Element@1, New_key); _ -> do_keyed(Element@1, Key) end end ), {fragment, Key, _pipe@1}; {element, _, Namespace, Tag, Attributes, Children@1, Styles} -> {element, Key, Namespace, Tag, Attributes, Children@1, Styles} end. -spec keyed( fun((list(element(QBX))) -> element(QBX)), list({binary(), element(QBX)}) ) -> element(QBX). keyed(Element, Children) -> Element( (gleam@list:map( Children, fun(_use0) -> {Key, Child} = _use0, do_keyed(Child, Key) end )) ). -spec map(element(QCH), fun((QCH) -> QCJ)) -> element(QCJ). map(Element, Mapper) -> case Element of nothing -> nothing; {text, Content} -> {text, Content}; {map, Subtree} -> {map, fun() -> map(Subtree(), Mapper) end}; {fragment, Key, Children} -> {fragment, Key, gleam@list:map( Children, fun(_capture) -> map(_capture, Mapper) end )}; {element, Key@1, Namespace, Tag, Class, Attributes, Children@1} -> Attributes@1 = gleam@list:map( Attributes, fun(_capture@1) -> lustre@attribute:map(_capture@1, Mapper) end ), Children@2 = gleam@list:map( Children@1, fun(_capture@2) -> map(_capture@2, Mapper) end ), {element, Key@1, Namespace, Tag, Class, Attributes@1, Children@2} end. -spec styled(lustre@internals@vdom:element(QCL)) -> element(QCL). styled(Element) -> case Element of {map, Subtree} -> {map, fun() -> styled(Subtree()) end}; {text, Content} -> {text, Content}; {fragment, Elements, Key} -> {fragment, Key, gleam@list:map(Elements, fun styled/1)}; {element, Key@1, Namespace, Tag, Attrs, Children, _, _} -> Class = none, {element, Key@1, Namespace, Tag, Class, Attrs, gleam@list:map(Children, fun styled/1)} end. -spec unstyled_children(sketch:cache(), list(element(QCO))) -> {sketch:cache(), list(lustre@internals@vdom:element(QCO))}. unstyled_children(Cache, Children) -> gleam@list:fold( lists:reverse(Children), {Cache, []}, fun(Acc, Child) -> {Cache@1, Children@1} = Acc, {Cache@2, Child@1} = unstyled(Cache@1, Child), {Cache@2, [Child@1 | Children@1]} end ). -spec unstyled(sketch:cache(), element(QCO)) -> {sketch:cache(), lustre@internals@vdom:element(QCO)}. unstyled(Cache, Element) -> case Element of nothing -> {Cache, lustre@element:none()}; {text, Content} -> {Cache, lustre@element:text(Content)}; {map, Subtree} -> unstyled(Cache, Subtree()); {fragment, Key, Children} -> _pipe = unstyled_children(Cache, Children), gleam@pair:map_second(_pipe, fun(Node) -> {fragment, Node, Key} end); {element, Key@1, Namespace, Tag, Class, Attributes, Children@1} -> Class@1 = gleam@option:map( Class, fun(_capture) -> sketch:class_name(_capture, Cache) end ), Class_name = gleam@option:map(Class@1, fun gleam@pair:second/1), Cache@1 = begin _pipe@1 = gleam@option:map(Class@1, fun gleam@pair:first/1), gleam@option:unwrap(_pipe@1, Cache) end, {Cache@2, Children@2} = unstyled_children(Cache@1, Children@1), Attributes@1 = case Class_name of none -> Attributes; {some, Class_name@1} -> Class_name@2 = lustre@attribute:class(Class_name@1), [Class_name@2 | Attributes] end, {Cache@2, case lustre@element:element(Tag, Attributes@1, Children@2) of {element, _, _, T, A, C, S, V} -> {element, Key@1, Namespace, T, A, C, S, V}; E -> E end} end.