<commit_before>module Entity.ViewModel exposing (..)

import Context
import Dict
import Document
import EditMode exposing (EditMode)
import Lazy
import Model.Types exposing (Entity(ContextEntity, ProjectEntity), EntityAction(ToggleDeleted, NameChanged, Save, StartEditing), EntityStoreType(ContextEntityStoreType, ProjectEntityStoreType), EntityType(ContextEntityType, ProjectEntityType), MainViewType(ContextView, GroupByContextView, GroupByProjectView, ProjectView))
import Msg exposing (Msg)
import Todo
import Toolkit.Helpers exposing (..)
import Toolkit.Operators exposing (..)
import Ext.Function exposing (..)
import Ext.Function.Infix exposing (..)
import List.Extra as List
import Maybe.Extra as Maybe
import Model
import Project


type alias ViewModel =
    { id : String
    , name : String
    , isDeleted : Bool
    , todoList : List Todo.Model
    , isEmpty : Bool
    , count : Int
    , onActiveStateChanged : Bool -> Msg
    , startEditingMsg : Msg
    , onDeleteClicked : Msg
    , onSaveClicked : Msg
    , onNameChanged : String -> Msg
    , onCancelClicked : Msg
    , icon : { name : String, color : String, class : String }
    }


createList config model =
    let
        todoListDict =
            Model.getActiveTodoListGroupedBy config.groupByFn model

        getTodoListWithGroupId id =
            todoListDict |> Dict.get id ?= []

        appendDeletedEntityList =
            if model.showDeleted then
                List.append # (Model.getDeletedEntityList config.entityType model)
            else
                identity

        --        entityList =
        --            Model.getActiveEntityList config.entityType model
        --                |> (::) config.nullEntity
        --                |> appendDeletedEntityList
        entityList =
            if model.showDeleted then
                Model.getDeletedEntityList config.entityType model
            else
                Model.getActiveEntityList config.entityType model
                    |> (::) config.nullEntity
    in
        entityList
            .|> create getTodoListWithGroupId config


create todoListByEntityId config entity =
    let
        id =
            Document.getId entity

        onEntityAction =
            Msg.OnEntityAction (config.entityWrapper entity)

        todoList =
            todoListByEntityId id

        count =
            List.length todoList

        isNull =
            config.isNull entity

        onDeleteClicked =
            if isNull then
                (Msg.NoOp)
            else
                (onEntityAction ToggleDeleted)

        maybeEditModel =
            config.maybeEditModel
                ?+> (\editModel ->
                        if editModel.id == id then
                            Just editModel
                        else
                            Nothing
                    )

        icon =
            if isNull then
                config.nullIcon
            else
                { name = config.defaultIconName, color = "#ddd", class = "sgtdBlue" }
    in
        { id = id
        , name = entity.name
        , isDeleted = Document.isDeleted entity
        , todoList = todoList
        , isEmpty = count == 0
        , count = List.length todoList
        , onActiveStateChanged =
            (\bool ->
                if bool then
                    Msg.SetView (config.getViewType id)
                else
                    Msg.NoOp
            )
        , startEditingMsg = onEntityAction StartEditing
        , onDeleteClicked = onDeleteClicked
        , onSaveClicked = onEntityAction Save
        , onNameChanged = NameChanged >> onEntityAction
        , onCancelClicked = Msg.DeactivateEditingMode
        , icon = icon
        }


sgtdBlue =
    materialInboxBlue


inboxBlue =
    "rgb(66, 133, 244)"


materialInboxBlue =
    "rgb(68, 138, 255)"


projectList : Model.Types.Model -> List ViewModel
projectList model =
    createList
        { groupByFn = Todo.getProjectId
        , entityType = ProjectEntityType
        , entityWrapper = ProjectEntity
        , nullEntity = Project.null
        , isNull = Project.isNull
        , nullIcon = { name = "av:stop", color = sgtdBlue, class = "sgtdBlue" }
        , defaultIconName = "av:stop"
        , getViewType = ProjectView
        , maybeEditModel = Model.getMaybeEditModelForEntityType ProjectEntityType model
        }
        model


contextList : Model.Types.Model -> List ViewModel
contextList model =
    createList
        { groupByFn = Todo.getContextId
        , entityType = ContextEntityType
        , entityWrapper = ContextEntity
        , nullEntity = Context.null
        , isNull = Context.isNull
        , nullIcon = { name = "inbox", color = sgtdBlue, class = "sgtdBlue" }
        , defaultIconName = "av:fiber-manual-record"
        , getViewType = ContextView
        , maybeEditModel = Model.getMaybeEditModelForEntityType ContextEntityType model
        }<commit_msg>drawer UX<commit_after>module Entity.ViewModel exposing (..)

import Context
import Dict
import Document
import EditMode exposing (EditMode)
import Lazy
import Model.Types exposing (Entity(ContextEntity, ProjectEntity), EntityAction(ToggleDeleted, NameChanged, Save, StartEditing), EntityStoreType(ContextEntityStoreType, ProjectEntityStoreType), EntityType(ContextEntityType, ProjectEntityType), MainViewType(ContextView, GroupByContextView, GroupByProjectView, ProjectView))
import Msg exposing (Msg)
import Todo
import Toolkit.Helpers exposing (..)
import Toolkit.Operators exposing (..)
import Ext.Function exposing (..)
import Ext.Function.Infix exposing (..)
import List.Extra as List
import Maybe.Extra as Maybe
import Model
import Project


type alias ViewModel =
    { id : String
    , name : String
    , isDeleted : Bool
    , todoList : List Todo.Model
    , isEmpty : Bool
    , count : Int
    , onActiveStateChanged : Bool -> Msg
    , startEditingMsg : Msg
    , onDeleteClicked : Msg
    , onSaveClicked : Msg
    , onNameChanged : String -> Msg
    , onCancelClicked : Msg
    , icon : { name : String, color : String }
    }


createList config model =
    let
        todoListDict =
            Model.getActiveTodoListGroupedBy config.groupByFn model

        getTodoListWithGroupId id =
            todoListDict |> Dict.get id ?= []

        appendDeletedEntityList =
            if model.showDeleted then
                List.append # (Model.getDeletedEntityList config.entityType model)
            else
                identity

        --        entityList =
        --            Model.getActiveEntityList config.entityType model
        --                |> (::) config.nullEntity
        --                |> appendDeletedEntityList
        entityList =
            if model.showDeleted then
                Model.getDeletedEntityList config.entityType model
            else
                Model.getActiveEntityList config.entityType model
                    |> (::) config.nullEntity
    in
        entityList
            .|> create getTodoListWithGroupId config


create todoListByEntityId config entity =
    let
        id =
            Document.getId entity

        onEntityAction =
            Msg.OnEntityAction (config.entityWrapper entity)

        todoList =
            todoListByEntityId id

        count =
            List.length todoList

        isNull =
            config.isNull entity

        onDeleteClicked =
            if isNull then
                (Msg.NoOp)
            else
                (onEntityAction ToggleDeleted)

        maybeEditModel =
            config.maybeEditModel
                ?+> (\editModel ->
                        if editModel.id == id then
                            Just editModel
                        else
                            Nothing
                    )

        icon =
            if isNull then
                config.nullIcon
            else
                { name = config.defaultIconName, color = "#ddd" }
    in
        { id = id
        , name = entity.name
        , isDeleted = Document.isDeleted entity
        , todoList = todoList
        , isEmpty = count == 0
        , count = List.length todoList
        , onActiveStateChanged =
            (\bool ->
                if bool then
                    Msg.SetView (config.getViewType id)
                else
                    Msg.NoOp
            )
        , startEditingMsg = onEntityAction StartEditing
        , onDeleteClicked = onDeleteClicked
        , onSaveClicked = onEntityAction Save
        , onNameChanged = NameChanged >> onEntityAction
        , onCancelClicked = Msg.DeactivateEditingMode
        , icon = icon
        }


sgtdBlue =
    materialInboxBlue


inboxBlue =
    "rgb(66, 133, 244)"


materialInboxBlue =
    "rgb(68, 138, 255)"


projectList : Model.Types.Model -> List ViewModel
projectList model =
    createList
        { groupByFn = Todo.getProjectId
        , entityType = ProjectEntityType
        , entityWrapper = ProjectEntity
        , nullEntity = Project.null
        , isNull = Project.isNull
        , nullIcon = { name = "vaadin:grid-big-o", color = sgtdBlue }
        , defaultIconName = "av:stop"
        , getViewType = ProjectView
        , maybeEditModel = Model.getMaybeEditModelForEntityType ProjectEntityType model
        }
        model


contextList : Model.Types.Model -> List ViewModel
contextList model =
    createList
        { groupByFn = Todo.getContextId
        , entityType = ContextEntityType
        , entityWrapper = ContextEntity
        , nullEntity = Context.null
        , isNull = Context.isNull
        , nullIcon = { name = "inbox", color = sgtdBlue }
        , defaultIconName = "av:fiber-manual-record"
        , getViewType = ContextView
        , maybeEditModel = Model.getMaybeEditModelForEntityType ContextEntityType model
        }
{'fraction_non_alphanumeric': 0.07052845528455284, 'fraction_numerical': 0.0034552845528455283, 'mean_word_length': 1.8458646616541354, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, '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': '333842', 'n_tokens_mistral': 2651, 'n_tokens_neox': 2536, 'n_words': 619}