<commit_before>module Dosar.Actiune.IncheiereIntentare.RezultatIncercareConciliere.PartileNuAjungLaIntelegere.MasuriDeAsigurare.MasuraDeAsigurare.UrmarirePatrimoniu.EditableList
    exposing
        ( EditableList
        , fromItems
        , view
        )

import Html exposing (Html, h1, fieldset, legend, div, span, ul, li, p, button, input, text, strong, br)
import Utils.MyHtml exposing (whenTrue, whenNonEmpty, whenNonNothing)
import Utils.MyHtmlEvents exposing (onClick)
import Utils.MyList as MyList


type EditableList a
    = EditableList
        { items : List a
        , maybeItemToEdit : Maybe (ItemToEdit a)
        }


type ItemToEdit a
    = ItemToEdit
        { item : a
        , maybeIndex : Maybe Int
        }


type alias ItemView a msg =
    a -> Html msg


type alias ItemEditForm a msg =
    a
    -> UpdateItemCallback a msg
    -> SubmitItemCallback a msg
    -> CancellCallback a msg
    -> Html msg


type alias UpdateItemCallback a msg =
    a -> msg


type alias SubmitItemCallback a msg =
    a -> msg


type alias CancellCallback a msg =
    a -> msg


fromItems : List a -> EditableList a
fromItems items =
    EditableList
        { items = items
        , maybeItemToEdit = Nothing
        }


updateItem : EditableList a -> a -> Maybe Int -> EditableList a
updateItem ((EditableList ({ items } as data)) as bunuriUrmarite) item maybeIndex =
    let
        newItems =
            case maybeIndex of
                Just index ->
                    MyList.replace items index item

                Nothing ->
                    items ++ [ item ]
    in
        EditableList
            { data
                | items = newItems
                , maybeItemToEdit = Nothing
            }


resetItemToEdit : EditableList a -> EditableList a
resetItemToEdit (EditableList data) =
    EditableList { data | maybeItemToEdit = Nothing }


updateItemToEdit : EditableList a -> a -> Maybe Int -> EditableList a
updateItemToEdit (EditableList data) bunUrmarit maybeIndex =
    EditableList { data | maybeItemToEdit = Just (ItemToEdit { item = bunUrmarit, maybeIndex = maybeIndex }) }


view : EditableList a -> ItemEditForm a msg -> ItemView a msg -> a -> (EditableList a -> msg) -> Html msg
view ((EditableList { items, maybeItemToEdit }) as v) itemEditFormView itemView newItem callback =
    let
        this =
            fieldset []
                [ legend [] [ text "EditableList" ]
                , whenNonEmpty items
                    (\items ->
                        itemListView itemView
                            items
                            (\bunUrmarit index -> updateItemToEdit v bunUrmarit (Just index) |> callback)
                    )
                , case maybeItemToEdit of
                    Just itemToEdit ->
                        itemEditForm itemToEdit

                    Nothing ->
                        addItemButton
                ]

        itemEditForm (ItemToEdit { item, maybeIndex }) =
            itemEditFormView item
                (\bunUrmarit -> updateItemToEdit v bunUrmarit maybeIndex |> callback)
                (\bunUrmarit -> updateItem v bunUrmarit maybeIndex |> callback)
                (\bunUrmarit -> resetItemToEdit v |> callback)

        addItemButton =
            button
                [ onClick (\_ -> updateItemToEdit v newItem Nothing |> callback) ]
                [ text "Adaugă bun" ]
    in
        this


itemListView : ItemView a msg -> List a -> (a -> Int -> msg) -> Html msg
itemListView itemView items editCallback =
    let
        this =
            ul [] (List.indexedMap renderItem items)

        renderItem index item =
            li []
                [ itemView item
                , button [ onClick (\_ -> editCallback item index) ] [ text "Edit" ]
                ]
    in
        this<commit_msg>EditableList: housekeeping<commit_after>module Dosar.Actiune.IncheiereIntentare.RezultatIncercareConciliere.PartileNuAjungLaIntelegere.MasuriDeAsigurare.MasuraDeAsigurare.UrmarirePatrimoniu.EditableList
    exposing
        ( EditableList
        , fromItems
        , view
        )

import Html exposing (Html, h1, fieldset, legend, div, span, ul, li, p, button, input, text, strong, br)
import Utils.MyHtml exposing (whenTrue, whenNonEmpty, whenNothing, whenNonNothing)
import Utils.MyHtmlEvents exposing (onClick)
import Utils.MyList as MyList


type EditableList a
    = EditableList
        { items : List a
        , maybeItemToEdit : Maybe (ItemToEdit a)
        }


type ItemToEdit a
    = ItemToEdit
        { item : a
        , maybeIndex : Maybe Int
        }


type alias ItemView a msg =
    a -> Html msg


type alias ItemEditForm a msg =
    a
    -> UpdateItemCallback a msg
    -> SubmitItemCallback a msg
    -> CancellCallback a msg
    -> Html msg


type alias UpdateItemCallback a msg =
    a -> msg


type alias SubmitItemCallback a msg =
    a -> msg


type alias CancellCallback a msg =
    a -> msg


fromItems : List a -> EditableList a
fromItems items =
    EditableList
        { items = items
        , maybeItemToEdit = Nothing
        }


submitItem : EditableList a -> a -> Maybe Int -> EditableList a
submitItem (EditableList ({ items } as data)) item maybeIndex =
    let
        newItems =
            case maybeIndex of
                Just index ->
                    MyList.replace items index item

                Nothing ->
                    items ++ [ item ]
    in
        EditableList
            { data
                | items = newItems
                , maybeItemToEdit = Nothing
            }


resetItemToEdit : EditableList a -> EditableList a
resetItemToEdit (EditableList data) =
    EditableList { data | maybeItemToEdit = Nothing }


updateItemToEdit : EditableList a -> a -> Maybe Int -> EditableList a
updateItemToEdit (EditableList data) item maybeIndex =
    EditableList { data | maybeItemToEdit = Just (ItemToEdit { item = item, maybeIndex = maybeIndex }) }


view : EditableList a -> ItemEditForm a msg -> ItemView a msg -> a -> (EditableList a -> msg) -> Html msg
view ((EditableList { items, maybeItemToEdit }) as v) itemEditFormView itemView newItem callback =
    let
        this =
            div []
                [ whenNonEmpty items
                    (itemListView itemView (\item index -> updateItemToEdit v item (Just index) |> callback))
                , whenNonNothing maybeItemToEdit itemEditForm
                , whenNothing maybeItemToEdit addItemButton
                ]

        itemEditForm (ItemToEdit { item, maybeIndex }) =
            itemEditFormView item
                (\item -> updateItemToEdit v item maybeIndex |> callback)
                (\item -> submitItem v item maybeIndex |> callback)
                (\item -> resetItemToEdit v |> callback)

        addItemButton _ =
            button
                [ onClick (\_ -> updateItemToEdit v newItem Nothing |> callback) ]
                [ text "Adaugă" ]
    in
        this


itemListView : ItemView a msg -> (a -> Int -> msg) -> List a -> Html msg
itemListView itemView editCallback items =
    let
        this =
            ul [] (List.indexedMap renderItem items)

        renderItem index item =
            li []
                [ itemView item
                , button [ onClick (\_ -> editCallback item index) ] [ text "Editează" ]
                ]
    in
        this
{'fraction_non_alphanumeric': 0.07370707207818651, 'fraction_numerical': 0.00027148092846477533, 'mean_word_length': 1.8436896950984176, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 86, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6083903', 'n_tokens_mistral': 2108, 'n_tokens_neox': 1999, 'n_words': 614}