//
//  UITableView+Extensions.swift
//  WoWonder
//
//  Created by iMac on 10/08/23.
//  Copyright © 2023 ScriptSun. All rights reserved.
//

import Foundation
import UIKit
import LinkPresentation

extension UITableViewCell {
    
    func isStringContainsURL(_ string: String) -> URL? {
        let detector = try? NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue)
        if let matches = detector?.matches(in: string, options: [], range: NSRange(location: 0, length: string.utf16.count)) {
            for match in matches {
                if match.resultType == .link, let url = match.url {
                    return url
                }
            }
        }
        return nil
    }
    
    func fetchMetaDetaProvider(urlString: String, completion: @escaping (LPLinkMetadata?) -> Void) {
        if let url = URL(string: urlString) {
            let metadataProvider = LPMetadataProvider()
            metadataProvider.timeout = 5
            metadataProvider.startFetchingMetadata(for: url) { metadata, error in
                if let metadata = metadata, error == nil {
                    completion(metadata)
                } else {
                    completion(nil)
                }
            }
        } else {
            completion(nil)
        }
    }
    
}
