//
//  UICollectionViewLeftAlignedLayout.swift
//  MVMCoreUI
//
//  Created by Dhamodaram Nandi on 05/06/20.
//  Copyright © 2020 Verizon Wireless. All rights reserved.
//
//  Left aligns items and makes the minimumInteritemSpacing absolute.

import Foundation

public class UICollectionViewLeftAlignedLayout: UICollectionViewFlowLayout {
    public override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        guard let attributes = super.layoutAttributesForElements(in: rect) else { return nil }
        var newAttributesForElementsInRect = [UICollectionViewLayoutAttributes]()
        for attribute in attributes {
            if let previousFrame = newAttributesForElementsInRect.last,
                MVMCoreGetterUtility.cgfequal(previousFrame.frame.minY, attribute.frame.minY) {
                attribute.frame.origin.x = previousFrame.frame.maxX + minimumInteritemSpacing
            } else {
                attribute.frame.origin.x = 0
            }
            newAttributesForElementsInRect.append(attribute)
        }
        return newAttributesForElementsInRect
    }
}
