import Foundation

final class PageSettings: ObservableObject {

    @Published
    var contentWidth: Int

    @Published
    var largeImageWidth: Int

    @Published
    var pageLinkImageSize: Int

    @Published
    var defaultCssFile: FileResource?

    @Published
    var codeHighlightingJsFile: FileResource?

    @Published
    var modelViewerJsFile: FileResource?

    @Published
    var imageCompareJsFile: FileResource?

    @Published
    var imageCompareCssFile: FileResource?

    init(file: PageSettingsFile, files: [String : FileResource]) {
        self.contentWidth = file.contentWidth
        self.largeImageWidth = file.largeImageWidth
        self.pageLinkImageSize = file.pageLinkImageSize
        self.defaultCssFile = file.defaultCssFile.map { files[$0] }
        self.codeHighlightingJsFile = file.codeHighlightingJsFile.map { files[$0] }
        self.modelViewerJsFile = file.modelViewerJsFile.map { files[$0] }
        self.imageCompareCssFile = file.imageCompareCssFile.map { files[$0] }
        self.imageCompareJsFile = file.imageCompareJsFile.map { files[$0] }
    }

    var file: PageSettingsFile {
        .init(contentWidth: contentWidth,
              largeImageWidth: largeImageWidth,
              pageLinkImageSize: pageLinkImageSize,
              defaultCssFile: defaultCssFile?.id,
              codeHighlightingJsFile: codeHighlightingJsFile?.id,
              modelViewerJsFile: modelViewerJsFile?.id,
              imageCompareJsFile: imageCompareJsFile?.id,
              imageCompareCssFile: imageCompareCssFile?.id)
    }
}
