//
//  Endpoint.swift
//  Pachyderm
//
//  Created by Shadowfacts on 3/29/22.
//  Copyright © 2022 Shadowfacts. All rights reserved.
//

import Foundation

public struct Endpoint: ExpressibleByStringInterpolation, CustomStringConvertible, Sendable {
    let components: [Component]
    
    public init(stringLiteral value: StringLiteralType) {
        self.components = [.literal(value)]
    }
    
    public init(stringInterpolation: StringInterpolation) {
        self.components = stringInterpolation.components
    }
    
    var path: String {
        components.map {
            switch $0 {
            case .literal(let s), .interpolated(let s):
                return s
            }
        }.joined(separator: "")
    }
    
    public var description: String {
        components.map {
            switch $0 {
            case .literal(let s):
                return s
            #if DEBUG
            case .interpolated(let s):
                return s
            #else
            case .interpolated(_):
                return "<redacted>"
            #endif
            }
        }.joined(separator: "")
    }
    
    public struct StringInterpolation: StringInterpolationProtocol {
        var components = [Component]()
        
        public init(literalCapacity: Int, interpolationCount: Int) {
        }
        
        public mutating func appendLiteral(_ literal: StringLiteralType) {
            components.append(.literal(literal))
        }
        
        public mutating func appendInterpolation(_ value: String) {
            components.append(.interpolated(value))
        }
    }
    
    enum Component: Sendable {
        case literal(String)
        case interpolated(String)
    }
    
}
