package dev.inmo.postssystem.services.posts.client.ui.create

import dev.inmo.postssystem.features.common.common.AbstractUIModel
import dev.inmo.postssystem.features.content.common.Content
import dev.inmo.postssystem.services.posts.common.FullNewPost
import dev.inmo.postssystem.services.posts.common.WritePostsService

class DefaultPostCreateUIModel(
    private val postCreationService: WritePostsService
) : PostCreateUIModel, AbstractUIModel<PostCreateUIState>(
    PostCreateUIState.Init
) {
    override suspend fun create(content: List<Content>) {
        runCatching {
            _currentState.value = PostCreateUIState.Uploading
            postCreationService.create(
                FullNewPost(content)
            )
        }.onFailure {
            _currentState.value = PostCreateUIState.Fail
        }.onSuccess {
            _currentState.value = PostCreateUIState.Completed
        }
    }
}
