/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache 2.0
*/

import UIKit
import SoraFoundation

final class LanguageSelectionInteractor {
    weak var presenter: LanguageSelectionInteractorOutputProtocol!

    let localizationManager: LocalizationManagerProtocol

    init(localizationManager: LocalizationManagerProtocol) {
        self.localizationManager = localizationManager
    }

    var logger: LoggerProtocol?
}

extension LanguageSelectionInteractor: LanguageSelectionInteractorInputProtocol {
    func load() {
        let languages: [Language] = localizationManager.availableLocalizations.map { localization in
            return Language(code: localization)
        }

        presenter.didLoad(languages: languages)

        presenter.didLoad(selectedLanguage: localizationManager.selectedLanguage)
    }

    func select(language: Language) {
        if language.code != localizationManager.selectedLocalization {
            localizationManager.selectedLocalization = language.code

            presenter.didLoad(selectedLanguage: localizationManager.selectedLanguage)
        }
    }
}
