import SwiftUI

struct AddTagView: View {

    @Environment(\.language)
    private var language

    @EnvironmentObject
    private var content: Content

    @Environment(\.dismiss)
    private var dismiss

    @Binding
    var selectedTag: Tag?

    init(selected: Binding<Tag?>) {
        self._selectedTag = selected
    }

    var body: some View {
        Text("Creating tag...")
            .onAppear(perform: addNewTag)
    }

    private func addNewTag() {
        let newTag = Tag(
            content: content,
            id: "tag",
            isVisible: true,
            german: .init(content: content, urlComponent: "tag", name: "Neuer Tag"),
            english: .init(content: content, urlComponent: "tag-en", name: "New Tag"))
        // Add to top of the list, and resort when changing the name
        content.tags.insert(newTag, at: 0)
        dismiss()
    }
}
