//
//  NSCollectionLayoutSection+Readable.swift
//  Tusker
//
//  Created by Shadowfacts on 9/28/23.
//  Copyright © 2023 Shadowfacts. All rights reserved.
//

import UIKit

extension NSCollectionLayoutSection {
    
    // The .readableContent insets reference has a bunch of weird behavior,
    // so just calculate the content inset ourselves.
    func readableContentInset(in environment: NSCollectionLayoutEnvironment) {
        guard let maximumReadableWidth = environment.maximumReadableWidth else {
            return
        }
        let inset = max(0, (environment.container.contentSize.width - maximumReadableWidth) / 2)
        // make sure not to overwrite the vertical insets, which are non-zero for grouped styles
        contentInsets.leading = inset
        contentInsets.trailing = inset
    }
    
}

extension NSCollectionLayoutEnvironment {
    var maximumReadableWidth: CGFloat? {
        switch traitCollection.preferredContentSizeCategory {
        case .extraSmall:
            return 560
        case .small:
            return 600
        case .medium:
            return 632
        case .large:
            return 664
        case .extraLarge:
            return 744
        case .extraExtraLarge:
            return 816
        case .extraExtraExtraLarge:
            return 896
        case .accessibilityMedium:
            return 1096
        default:
            // greater accessibility sizes don't have a limit
            return nil
        }
    }
}
