import Foundation

/// Supporting types and pure checks for `InstanceFormView`, kept out of the view file
/// so it stays within the file length limit.
extension InstanceFormView {
    enum Mode: Identifiable {
        case add
        case edit(ForgejoInstance)

        var id: String {
            switch self {
            case .add: "add"
            case let .edit(instance): instance.serverURL + instance.username
            }
        }
    }

    enum AuthMode: String, CaseIterable {
        case credentials = "Password"
        case token = "API Token"
    }

    /// True if `instances` already contains an account with this server+username,
    /// ignoring `excluding` (the instance being edited). Accounts that share a
    /// server+username collide on the same sourceKey, which crashes merged overviews.
    static func accountAlreadyExists(
        serverURL normalizedURL: String,
        username: String,
        in instances: [ForgejoInstance],
        excluding: ForgejoInstance? = nil,
    ) -> Bool {
        instances.contains { inst in
            inst !== excluding && inst.serverURL == normalizedURL && inst.username == username
        }
    }
}
