//
//  TimelineSegment.swift
//  Pachyderm
//
//  Created by Shadowfacts on 7/29/19.
//  Copyright © 2019 Shadowfacts. All rights reserved.
//

import Foundation

public struct TimelineSegment<Type: Identifiable> where Type.ID == String {
    private var ids: [String]
    
    public init(objects: [Type]) {
        self.ids = objects.map { $0.id }
    }
    
    public mutating func insertAtBeginning(ids: [String]) {
        self.ids.insert(contentsOf: ids, at: 0)
    }
    
    public mutating func insertAtBeginning(objects: [Type]) {
        insertAtBeginning(ids: objects.map { $0.id })
    }
    
    public mutating func append(ids: [String]) {
        self.ids.append(contentsOf: ids)
    }
    
    public mutating func append(objects: [Type]) {
        append(ids: objects.map { $0.id })
    }
}

extension TimelineSegment: RandomAccessCollection {
    public typealias Index = Int
    
    public subscript(index: Int) -> String {
        return ids[index]
    }
    
    public var startIndex: Int {
        return ids.startIndex
    }
    
    public var endIndex: Int {
        return ids.endIndex
    }
}