//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

import Amplify
import Foundation

extension StorageRequestUtils {
    // MARK: Getter methods

    static func getAccessLevelPrefix(
        accessLevel: StorageAccessLevel,
        identityId: String,
        targetIdentityId: String?
    ) -> String {

        let targetIdentityId = targetIdentityId ?? identityId

        if accessLevel == .private || accessLevel == .protected {

            return accessLevel.serviceAccessPrefix + "/" + targetIdentityId + "/"
        }

        return accessLevel.serviceAccessPrefix + "/"
    }

    static func getSize(_ file: URL) throws -> UInt64 {
        if let error = validateFileExists(file) {
            throw StorageError.localFileNotFound(error.errorDescription, error.recoverySuggestion)
        }

        do {
            let attributeOfItem = try FileManager.default.attributesOfItem(atPath: file.path)
            guard let fileSize = attributeOfItem[FileAttributeKey.size] as? UInt64 else {
                throw StorageError.unknown("Could not get size of file.")
            }

            return fileSize
        } catch {
            throw StorageError.unknown("Unexpected error occurred while retrieving attributes of file.", error)
        }
    }
}

public extension StorageAccessLevel {
    /// Service Access Prefix.
    var serviceAccessPrefix: String {
        switch self {
        case .guest:
            return "public"
        case .protected:
            return rawValue
        case .private:
            return rawValue
        }
    }
}
