 
 import Foundation
 import AEXML
-
-public enum OPFParserError: Error {
-
-    /// The natvigation link couldn't not be found in the publication.
-    case missingNavLink
-
-    /// The natvigation link have no/empty Href property.
-    case missingNavLinkHref
-
-}
 
 extension OPFParser: Loggable {}
 

            guard let id = item.attributes["id"] else {
                // Manifest item MUST have an id, ignore it
                log(level: .error, "Manifest item has no \"id\" attribute.")
                return
            }
            // If it's the cover's item id, set the rel to cover and add the
            // link to `links`


        /// WIP -------------

        // Get navLink then generate navigation document
        var navLink = publication.spine.first(where: { $0.rel.contains("contents") })
        let navigationDocument = generateNavigationDocument(navigationLink: navLink,
                                                            in: container)
        if let nd = navigationDocument {
            fillTOC(from: nd, to: publication)
            if publication.TOC.isEmpty {
                fillTOCFromNCX(from: nd, to: publication)
                fillPageListFromNCX(from: nd, to: publication)
            }
        }

        // ENDWIP -----------
        publication.internalData["type"] = "epub"
        publication.internalData["rootfile"] = container.rootFile.rootFilePath
        return publication
    }


    /// Return an XML Document representing the navigation document pointed by
    /// the first manifest item navigationLink( id == nav)
    ///
    /// - Parameters:
    ///   - navLink: The Link generated from the manifest id='nav' element.
    ///   - container: The epub container.
    /// - Returns: The XML Document.
    fileprivate func generateNavigationDocument(navigationLink navLink: Link?,
                                                in container: Container) -> AEXMLDocument?
    {
        let rootFilePath = container.rootFile.rootFilePath
        let rootDirPath = rootFilePath.deletingLastPathComponent()

        guard let navLinkHref = navLink?.href else {
            log(level: .error, "Couldn't find the `nav link` in Publication.")
            return nil
        }

        // Get the path of the folder containing the .opf file
        let navigationDocumentPath = rootDirPath.appending(pathComponent: navLinkHref)
        var navigationDocument: AEXMLDocument? = nil

        do {
            navigationDocument = try container.xmlDocumentForFile(atPath: navigationDocumentPath)
        } catch {
            log(level: .error, "Error generating the XML document: \(error)")
        }
        return navigationDocument
    }

    internal func fillTOC(from navigationDocument: AEXMLDocument, to publication: Publication) {
        

    }

