 import WebKit
 import AVFoundation
 import Carbon.HIToolbox
-
-extension WKWebViewConfiguration {
-    /// Async Factory method to acquire WKWebViewConfigurations packaged with system cookies
-    static func cookiesIncluded(completion: @escaping (WKWebViewConfiguration?) -> Void) {
-        let config = WKWebViewConfiguration()
-        guard let cookies = HTTPCookieStorage.shared.cookies else {
-            completion(config)
-            return
-        }
-        // Use nonPersistent() or default() depending on if you want cookies persisted to disk
-        // and shared between WKWebViews of the same app (default), or not persisted and not shared
-        // across WKWebViews in the same app.
-        let dataStore = WKWebsiteDataStore.nonPersistent()
-        let waitGroup = DispatchGroup()
-        for cookie in cookies {
-            waitGroup.enter()
-            if #available(OSX 10.13, *) {
-                dataStore.httpCookieStore.setCookie(cookie) { waitGroup.leave() }
-            } else {
-                // Fallback on earlier versions
-            }
-        }
-        waitGroup.notify(queue: DispatchQueue.main) {
-            config.websiteDataStore = dataStore
-            completion(config)
-        }
+import Quartz
+
+fileprivate var defaults : UserDefaults {
+    get {
+        return UserDefaults.standard
     }
 }
-
+fileprivate var appDelegate : AppDelegate {
+    get {
+        return NSApp.delegate as! AppDelegate
+    }
+}
+fileprivate var docController : HeliumDocumentController {
+    get {
+        return NSDocumentController.shared as! HeliumDocumentController
+    }
+}
+
+extension WKNavigationType {
+    var name : String {
+        get {
+            let names = ["linkActivated","formSubmitted","backForward","reload","formResubmitted"]
+            return names.indices.contains(self.rawValue) ? names[self.rawValue] : "other"
+        }
+    }
+}
 class WebBorderView : NSView {
     var isReceivingDrag = false {
         didSet {

            return wvc
        }
    }
/*
    override func load(_ request: URLRequest) -> WKNavigation? {
        Swift.print("we got \(request)")
        return super.load(request)
    }
*/
/*
    @objc @IBAction internal func cut(_ sender: Any) {
        guard let doc : Document = self.window?.windowController?.document as? Document, doc.docGroup == .helium else { return }

        let pb = NSPasteboard.general
        pb.clearContents()
        if let urlString = self.url?.absoluteString {
            pb.setString(urlString, forType: NSPasteboard.PasteboardType.string)
            (self.uiDelegate as! WebViewController).clear()
        }
    }
    @objc @IBAction internal func copy(_ sender: Any) {
        let pb = NSPasteboard.general
        pb.clearContents()
        if let urlString = self.url?.absoluteString {
            pb.setString(urlString, forType: NSPasteboard.PasteboardType.string)
        }
    }
    @objc @IBAction internal func paste(_ sender: Any) {
        let whoAmI : NSResponder = (self.window?.firstResponder)!
        
        //  We want to add to existing play item list
        guard whoAmI == self else { whoAmI.nextResponder?.perform(#selector(paste(_:)), with: sender); return }

        guard let doc : Document = self.window?.windowController?.document as? Document, doc.docGroup == .helium else { return }
        
        let pb = NSPasteboard.general
        guard let rawString = pb.string(forType: NSPasteboard.PasteboardType.string), rawString.isValidURL() else { return }
        
        self.load(URLRequest.init(url: URL.init(string: rawString)!))
    }
    @objc @IBAction internal func delete(_ sender: Any) {
        self.cancelOperation(sender)
        Swift.print("cancel")
    }
*/
    func data(_ data : Data) -> Bool {
        guard let url = URL.init(cache: data) else { return false }
        return next(url: url)
    }
    
    func html(_ html : String) -> Bool {
        guard let url = URL.init(cache: html) else { return false }
        return next(url: url)
    }

    func next(url: URL) -> Bool {

                        if url.isFileURL || dirty { viewOptions.insert(.t_view) }
                        
                        if viewOptions.contains(.t_view) {
                            handled += appDelegate.openURLInNewWindow(url, attachTo: window) ? 1 : 0
                        }
                        else
                        if viewOptions.contains(.w_view) {
                            handled += appDelegate.openURLInNewWindow(url) ? 1 : 0
                        }
                        else
                        {
