UNPKG

cozy-iiif

Version:

A developer-friendly collection of abstractions and utilities built on top of @iiif/presentation-3 and @iiif/parser

922 lines (921 loc) 45.4 kB
import { imageSize as le } from "image-size"; import { g, a as k, b as S } from "./resource-DS2brz47.js"; import { c as Qt } from "./resource-DS2brz47.js"; var pe = "http://library.stanford.edu/iiif/image-api/compliance.html#level0", ve = "http://library.stanford.edu/iiif/image-api/compliance.html#level1", F = "http://library.stanford.edu/iiif/image-api/compliance.html#level2", he = "http://library.stanford.edu/iiif/image-api/conformance.html#level0", ue = "http://library.stanford.edu/iiif/image-api/conformance.html#level1", W = "http://library.stanford.edu/iiif/image-api/conformance.html#level2", fe = "http://library.stanford.edu/iiif/image-api/1.1/compliance.html#level0", me = "http://library.stanford.edu/iiif/image-api/1.1/compliance.html#level1", z = "http://library.stanford.edu/iiif/image-api/1.1/compliance.html#level2", ge = "http://library.stanford.edu/iiif/image-api/1.1/conformance.html#level0", de = "http://library.stanford.edu/iiif/image-api/1.1/conformance.html#level1", H = "http://library.stanford.edu/iiif/image-api/1.1/conformance.html#level2", ye = "http://iiif.io/api/image/1/level0.json", Ae = "http://iiif.io/api/image/1/profiles/level0.json", Ce = "http://iiif.io/api/image/1/level1.json", be = "http://iiif.io/api/image/1/profiles/level1.json", J = "http://iiif.io/api/image/1/level2.json", G = "http://iiif.io/api/image/1/profiles/level2.json", Re = "http://iiif.io/api/image/2/level0.json", Le = "http://iiif.io/api/image/2/profiles/level0.json", we = "http://iiif.io/api/image/2/level1.json", Ie = "http://iiif.io/api/image/2/profiles/level1.json", K = "http://iiif.io/api/image/2/level2.json", Q = "http://iiif.io/api/image/2/profiles/level2.json", Se = "level0", xe = "level1", X = "level2", Te = "http://iiif.io/api/image/2/level0", Me = "http://iiif.io/api/image/2/level1", Y = "http://iiif.io/api/image/2/level2", $e = [Y, F, W, z, H, J, G, K, Q, X], je = [Te, Me, Y, pe, ve, F, he, ue, W, fe, me, z, ge, de, H, ye, Ae, Ce, be, J, G, Re, Le, we, Ie, K, Q, Se, xe, X], ke = je; function d(t) { for (let e in t) (typeof t[e] > "u" || t[e] === null) && delete t[e]; return t; } function w(t) { return Array.isArray(t) ? t : t ? [t] : []; } var Pe = Object.defineProperty, Oe = (t, e, r) => e in t ? Pe(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r, R = (t, e, r) => (Oe(t, typeof e != "symbol" ? e + "" : e, r), r), E = ["sc:Collection", "sc:Manifest", "sc:Canvas", "sc:AnnotationList", "oa:Annotation", "sc:Range", "sc:Layer", "sc:Sequence", "oa:Choice", "Service", "ContentResource"]; function Ue(t) { if (typeof t > "u" || t === null) throw new Error("Null or undefined is not a valid entity."); if (Array.isArray(t)) throw new Error("Array is not a valid entity"); if (typeof t != "object") throw new Error(`${typeof t} is not a valid entity`); if (typeof t["@type"] == "string") { let e = E.indexOf(t["@type"]); if (e !== -1) return E[e]; } if (t.profile) return "Service"; if (t.format || t["@type"]) return "ContentResource"; throw new Error("Resource type is not known"); } var De = class Z { constructor(e, r = {}) { R(this, "traversals"), R(this, "options"), this.traversals = { collection: [], manifest: [], canvas: [], annotationList: [], sequence: [], annotation: [], contentResource: [], choice: [], range: [], service: [], layer: [], ...e }, this.options = { convertPropsToArray: !0, mergeMemberProperties: !0, allowUndefinedReturn: !1, ...r }; } static all(e) { return new Z({ collection: [e], manifest: [e], canvas: [e], annotationList: [e], sequence: [e], annotation: [e], contentResource: [e], choice: [e], range: [e], service: [e], layer: [e] }); } traverseCollection(e) { return this.traverseType(this.traverseDescriptive(this.traverseLinking(this.traverseCollectionItems(e))), this.traversals.collection); } traverseCollectionItems(e) { if (this.options.mergeMemberProperties) { let r = [...(e.manifests || []).map((n) => typeof n == "string" ? { "@id": n, "@type": "sc:Manifest" } : n), ...(e.collections || []).map((n) => typeof n == "string" ? { "@id": n, "@type": "sc:Collection" } : n), ...e.members || []], i = [], s = r.filter((n) => i.includes(n["@id"]) ? !1 : (i.push(n["@id"]), !0)); delete e.collections, delete e.manifests, e.members = s; } return e.manifests && (e.manifests = e.manifests.map((r) => this.traverseManifest(typeof r == "string" ? { "@id": r, "@type": "sc:Manifest" } : r))), e.collections && (e.collections = e.collections.map((r) => this.traverseCollection(typeof r == "string" ? { "@id": r, "@type": "sc:Collection" } : r))), e.members && (e.members = e.members.map((r) => typeof r == "string" ? r : r["@type"] === "sc:Collection" ? this.traverseCollection(r) : r["@type"] === "sc:Manifest" ? this.traverseManifest(r) : this.traverseUnknown(r))), e; } traverseManifest(e) { return this.traverseType(this.traverseDescriptive(this.traverseLinking(this.traverseManifestItems(e))), this.traversals.manifest); } traverseManifestItems(e) { return e.sequences && (e.sequences = e.sequences.map((r) => this.traverseSequence(r))), e.structures && (e.structures = e.structures.map((r) => this.traverseRange(r))), e; } traverseSequence(e) { return this.traverseType(this.traverseDescriptive(this.traverseLinking(this.traverseSequenceItems(e))), this.traversals.sequence); } traverseSequenceItems(e) { return e.canvases && (e.canvases = e.canvases.map((r) => this.traverseCanvas(r))), e; } traverseCanvas(e) { return this.traverseType(this.traverseDescriptive(this.traverseLinking(this.traverseCanvasItems(e))), this.traversals.canvas); } traverseCanvasItems(e) { return e.images && (e.images = e.images.map((r) => this.traverseAnnotation(r))), e.otherContent && (e.otherContent = e.otherContent.map((r) => this.traverseAnnotationList(r))), e; } traverseRange(e) { return this.traverseType(this.traverseDescriptive(this.traverseLinking(this.traverseRangeItems(e))), this.traversals.range); } traverseRangeItems(e) { if (this.options.mergeMemberProperties) { let r = [...(e.ranges || []).map((i) => typeof i == "string" ? { "@id": i, "@type": "sc:Range" } : i), ...(e.canvases || []).map((i) => typeof i == "string" ? { "@id": i, "@type": "sc:Canvas" } : i), ...e.members || []]; delete e.ranges, delete e.canvases, e.members = r.length ? r.map((i) => this.traverseUnknown(i)) : void 0; } return e; } traverseAnnotationList(e) { let r = typeof e == "string" ? { "@id": e, "@type": "sc:AnnotationList" } : e; return this.traverseType(this.traverseDescriptive(this.traverseAnnotationListItems(r)), this.traversals.annotationList); } traverseAnnotationListItems(e) { return e.resources && (e.resources = e.resources.map((r) => this.traverseAnnotation(r))), e; } traverseAnnotation(e) { return this.traverseType(this.traverseDescriptive(this.traverseLinking(this.traverseAnnotationItems(e))), this.traversals.annotation); } traverseAnnotationItems(e) { return e.resource && (Array.isArray(e.resource) ? e.resource = e.resource.map((r) => this.traverseContentResource(r)) : e.resource = this.traverseContentResource(e.resource)), e.on, e; } traverseLayer(e) { return this.traverseType(this.traverseLinking(this.traverseLayerItems(e)), this.traversals.layer); } traverseLayerItems(e) { return e.otherContent && (e.otherContent = e.otherContent.map((r) => this.traverseAnnotationList(r))), e; } traverseChoice(e) { return this.traverseType(this.traverseChoiceItems(e), this.traversals.choice); } traverseChoiceItems(e) { return e.default && e.default !== "rdf:nil" && (e.default = this.traverseContentResource(e.default)), e.item && e.item !== "rdf:nil" && (e.item = e.item.map((r) => this.traverseContentResource(r))), e; } traverseService(e) { return this.traverseType(this.traverseLinking(e), this.traversals.service); } traverseContentResource(e) { return e["@type"] === "oa:Choice" ? this.traverseChoice(e) : this.traverseType(this.traverseDescriptive(this.traverseLinking(e)), this.traversals.contentResource); } traverseUnknown(e) { if (!e["@type"] || typeof e == "string") return e; switch (Ue(e)) { case "sc:Collection": return this.traverseCollection(e); case "sc:Manifest": return this.traverseManifest(e); case "sc:Canvas": return this.traverseCanvas(e); case "sc:Sequence": return this.traverseSequence(e); case "sc:Range": return this.traverseRange(e); case "oa:Annotation": return this.traverseAnnotation(e); case "sc:AnnotationList": return this.traverseAnnotationList(e); case "sc:Layer": return this.traverseLayer(e); case "Service": return this.traverseService(e); case "oa:Choice": return this.traverseChoice(e); case "ContentResource": return this.traverseContentResource(e); } return e.profile ? this.traverseService(e) : e; } traverseImageResource(e) { let r = Array.isArray(e), i = Array.isArray(e) ? e : [e], s = []; for (let n of i) typeof n == "string" ? s.push(this.traverseContentResource({ "@id": n, "@type": "dctypes:Image" })) : s.push(this.traverseContentResource(n)); return !r && !this.options.convertPropsToArray ? s[0] : s; } traverseDescriptive(e) { return e.thumbnail && (e.thumbnail = this.traverseImageResource(e.thumbnail)), e.logo && (e.logo = this.traverseImageResource(e.logo)), e; } traverseOneOrMoreServices(e) { let r = Array.isArray(e), i = Array.isArray(e) ? e : [e], s = []; for (let n of i) s.push(this.traverseService(n)); return !r && !this.options.convertPropsToArray ? s[0] : s; } traverseLinking(e) { return e.related && (e.related = this.traverseOneOrManyType(e.related, this.traversals.contentResource)), e.rendering && (e.rendering = this.traverseOneOrManyType(e.rendering, this.traversals.contentResource)), e.service && (e.service = this.traverseOneOrMoreServices(e.service)), e.seeAlso && (e.seeAlso = this.traverseOneOrManyType(e.seeAlso, this.traversals.contentResource)), e.within && (typeof e.within == "string" || (e.within = this.traverseOneOrManyType(e.within, this.traversals.contentResource))), e.startCanvas && (typeof e.startCanvas == "string" ? e.startCanvas = this.traverseType({ "@id": e.startCanvas, "@type": "sc:Canvas" }, this.traversals.canvas) : e.startCanvas && this.traverseType(e.startCanvas, this.traversals.canvas)), e.contentLayer && (typeof e.contentLayer == "string" ? e.contentLayer = this.traverseLayer({ "@id": e.contentLayer, "@type": "sc:Layer" }) : e.contentLayer = this.traverseLayer(e.contentLayer)), e; } traverseOneOrManyType(e, r) { if (!Array.isArray(e)) if (this.options.convertPropsToArray) e = [e]; else return this.traverseType(e, r); return e.map((i) => this.traverseType(i, r)); } traverseType(e, r) { return r.reduce((i, s) => { let n = s(i); return typeof n > "u" && !this.options.allowUndefinedReturn ? i : n; }, e); } }, Ne = "http://library.stanford.edu/iiif/image-api/compliance.html#level1", Ee = "http://library.stanford.edu/iiif/image-api/compliance.html#level2", _e = "http://library.stanford.edu/iiif/image-api/conformance.html#level1", qe = "http://library.stanford.edu/iiif/image-api/conformance.html#level2", Be = "http://library.stanford.edu/iiif/image-api/1.1/compliance.html#level1", Ve = "http://library.stanford.edu/iiif/image-api/1.1/compliance.html#level2", Fe = "http://library.stanford.edu/iiif/image-api/1.1/conformance.html#level1", We = "http://library.stanford.edu/iiif/image-api/1.1/conformance.html#level2", ze = "http://iiif.io/api/image/1/level1.json", He = "http://iiif.io/api/image/1/profiles/level1.json", Je = "http://iiif.io/api/image/1/level2.json", Ge = "http://iiif.io/api/image/1/profiles/level2.json", Ke = "http://iiif.io/api/image/2/level1.json", Qe = "http://iiif.io/api/image/2/profiles/level1.json", Xe = "http://iiif.io/api/image/2/level2.json", Ye = "http://iiif.io/api/image/2/profiles/level2.json", Ze = "level1", et = "level2", tt = "http://iiif.io/api/image/2/level1", rt = "http://iiif.io/api/image/2/level2", it = [tt, rt, Ne, Ee, _e, qe, Be, Ve, Fe, We, ze, He, Je, Ge, Ke, Qe, Xe, Ye, Ze, et], P = { attributionLabel: "Attribution", providerId: "http://example.org/provider", providerName: "" }; function st(t) { if (typeof t == "string") return [t]; if (!t) return []; let e = Array.isArray(t) ? t : [t], r = []; for (let i of e) { if (typeof i == "string") { r.push(i); continue; } r.push({ "@language": i["@language"] || i.language, "@value": i["@value"] || i.value }); } return r; } function I(t, e = "none") { if (!t) return { none: [""] }; let r = st(t), i = {}; for (let s of r) { if (typeof s == "string") { i[e] = i[e] ? i[e] : [], i[e].push(s || ""); continue; } if (!s["@language"]) { i[e] = i[e] ? i[e] : [], i[e].push(s["@value"] || ""); continue; } let n = s["@language"]; i[n] = i[n] ? i[n] : [], i[n].push(s["@value"] || ""); } return Object.keys(i).length === 0 ? { none: [""] } : i; } function ee(t) { if (Array.isArray(t)) return ee(t.find((e) => typeof e == "string")); if ($e.indexOf(t) !== -1) return "level2"; if (it.indexOf(t) !== -1) return "level1"; if (ke.indexOf(t) !== -1) return "level0"; if (typeof t == "string") return t; } function nt(t) { let e = Array.isArray(t) ? t : [t]; for (let r of e) switch (r) { case "http://iiif.io/api/image/2/context.json": case "http://library.stanford.edu/iiif/image-api/1.1/compliance.html#level2": return "ImageService2"; case "http://iiif.io/api/image/1/context.json": case "http://library.stanford.edu/iiif/image-api/1.1/context.json": return "ImageService1"; case "http://iiif.io/api/annex/openannotation/context.json": return "ImageApiSelector"; } } function at(t) { switch (t) { case "http://iiif.io/api/image/2/level0.json": case "http://iiif.io/api/image/2/level1.json": case "http://iiif.io/api/image/2/level2.json": return "ImageService2"; case "http://iiif.io/api/auth/1/kiosk": case "http://iiif.io/api/auth/1/login": case "http://iiif.io/api/auth/1/clickthrough": case "http://iiif.io/api/auth/1/external": case "http://iiif.io/api/auth/0/kiosk": case "http://iiif.io/api/auth/0/login": case "http://iiif.io/api/auth/0/clickthrough": case "http://iiif.io/api/auth/0/external": return "AuthCookieService1"; case "http://iiif.io/api/auth/1/token": case "http://iiif.io/api/auth/0/token": return "AuthTokenService1"; case "http://iiif.io/api/auth/1/logout": case "http://iiif.io/api/auth/0/logout": return "AuthLogoutService1"; case "http://iiif.io/api/search/1/search": case "http://iiif.io/api/search/0/search": return "SearchService1"; case "http://iiif.io/api/search/1/autocomplete": case "http://iiif.io/api/search/0/autocomplete": return "AutoCompleteService1"; } } function _(t) { for (let e of ["sc", "oa", "dcterms", "dctypes", "iiif"]) if (t.startsWith(`${e}:`)) return t.slice(e.length + 1); return t; } var ot = ["Collection", "Manifest", "Annotation", "AnnotationPage", "Range", "Service"]; function D(t) { let e = t["@id"] || t.id, r = t["@type"] || t.type, i = t.profile || void 0, s = t["@context"] || void 0; if (i) { let n = at(i); if (n) return n; } if (s) { let n = nt(s); if (n) return n; } if (r) { if (Array.isArray(r)) { if (r.indexOf("oa:CssStylesheet") !== -1) return "CssStylesheet"; if (r.indexOf("cnt:ContentAsText") !== -1) return "TextualBody"; r = r[0]; } for (let n of ["sc", "oa", "dcterms", "dctypes", "iiif"]) if (r.startsWith(`${n}:`)) { r = r.slice(n.length + 1); break; } switch (r) { case "Layer": return "AnnotationCollection"; case "AnnotationList": return "AnnotationPage"; case "cnt:ContentAsText": return "TextualBody"; } } if (r && ot.indexOf(r) !== -1) return r; if (t.format) { if (t.format.startsWith("image/")) return "Image"; if (t.format.startsWith("text/") || t.format === "application/pdf") return "Text"; if (t.format.startsWith("application/")) return "Dataset"; } return e && (e.endsWith(".jpg") || e.endsWith(".png") || e.endsWith(".jpeg")) ? "Image" : r || "unknown"; } var ct = /http(s)?:\/\/(creativecommons.org|rightsstatements.org)[^"'\\<\n]+/gm; function lt(t) { let e = t.match(ct); return e ? e[0] : t; } function pt(t, e = "Rights/License", r = "none") { let i = null, s = [], n = Array.isArray(t) ? t : [t]; for (let a of n) { let c = a ? lt(a) : void 0; if (c && (c.indexOf("creativecommons.org") !== -1 || c.indexOf("rightsstatements.org") !== -1)) { c.startsWith("https://") ? i = `http://${c.slice(8)}` : i = c; continue; } c && s.push({ label: { [r]: [e] }, value: { [r]: [c] } }); } return [i, s]; } var vt = ["http://iiif.io/api/presentation/2/context.json", "http://iiif.io/api/image/2/context.json", "http://iiif.io/api/image/1/context.json", "http://library.stanford.edu/iiif/image-api/1.1/context.json", "http://iiif.io/api/search/1/context.json", "http://iiif.io/api/search/0/context.json", "http://iiif.io/api/auth/1/context.json", "http://iiif.io/api/auth/0/context.json", "http://iiif.io/api/annex/openannotation/context.json"]; function ht(t) { if (t) { let e = Array.isArray(t) ? t : [t], r = []; for (let i of e) i === "http://iiif.io/api/presentation/2/context.json" && r.push("http://iiif.io/api/presentation/3/context.json"), vt.indexOf(i) === -1 && r.push(i); if (e.length) return r.length === 1 ? r[0] : r; } } function ut(t) { return t ? t.map((e) => ({ label: I(e.label), value: I(e.value) })) : []; } var q = 0; function te(t, e) { let r = encodeURI(t.id || t["@id"] || "").trim(); return r && e ? `${r}/${e}` : r || (q++, `http://example.org/${t["@type"]}${e ? `/${e}` : ""}/${q}`); } function C(t) { let e = [...t.behavior || []]; t.viewingHint && e.push(t.viewingHint); let r; return Array.isArray(t.motivation) ? r = t.motivation.map(_) : t.motivation && (r = _(t.motivation)), { "@context": t["@context"] ? ht(t["@context"]) : void 0, id: (t["@id"] || te(t)).trim(), type: D(t), behavior: e.length ? e : void 0, height: t.height ? t.height : void 0, width: t.width ? t.width : void 0, motivation: r, viewingDirection: t.viewingDirection, profile: t.profile, format: t.format ? t.format : void 0, duration: void 0, timeMode: void 0 }; } function b(t) { let [e, r] = pt(t.license), i = [...t.metadata ? ut(t.metadata) : [], ...r]; return { rights: e, metadata: i.length ? i : void 0, label: t.label ? I(t.label) : void 0, requiredStatement: t.attribution ? { label: I(P.attributionLabel), value: I(t.attribution) } : void 0, navDate: t.navDate, summary: t.description ? I(t.description) : void 0, thumbnail: ft(t.thumbnail) }; } function ft(t) { return t && (Array.isArray(t) ? t : [t]).map((e) => typeof e == "string" ? { id: e, type: "Image" } : (e.type === "unknown" && (e.type = "Image"), e)); } function mt(t) { if (!t.within) return; let e = Array.isArray(t.within) ? t.within : [t.within], r = []; for (let i of e) if (typeof i == "string") { if (i) switch (t["@type"]) { case "sc:Manifest": r.push({ id: i, type: "Collection" }); break; } } else i["@id"] && r.push({ id: i["@id"], type: D(i) }); return r.length ? r : void 0; } function L(t) { let e = t.related ? Array.isArray(t.related) ? t.related : [t.related] : [], r = t.contentLayer; return { provider: t.logo || e.length ? [{ id: P.providerId, type: "Agent", homepage: e.length ? [e[0]] : void 0, logo: t.logo ? Array.isArray(t.logo) ? t.logo : [t.logo] : void 0, label: I(P.providerName) }] : void 0, partOf: mt(t), rendering: t.rendering, seeAlso: t.seeAlso, start: t.startCanvas, service: t.service ? w(t.service) : void 0, supplementary: r ? [r] : void 0 }; } function gt(t) { return { chars: t.chars, format: t.format ? t.format : void 0, language: t.language }; } function $(t, e) { return t ? typeof t == "string" ? { id: t, type: e } : typeof (t == null ? void 0 : t["@id"]) == "string" ? { id: t["@id"], type: e } : typeof t.id == "string" ? { id: t.id, type: e } : null : null; } function dt(t) { let e = {}; if (t.first) { let r = $(t.first, "Collection"); r && (e.first = r); } if ((t.total || t.total === 0) && (e.total = t.total), t.prev) { let r = $(t.prev, "Collection"); r && (e.prev = r); } if (t.next) { let r = $(t.next, "Collection"); r && (e.next = r); } return e; } function yt(t) { let e = []; for (let r of t) { let i = { ...r }; i.items && i.items.length === 0 && delete i.items, e.push(i); } return e; } function At(t) { return d({ ...C(t), ...b(t), ...L(t), ...dt(t), items: yt(t.members) }); } function Ct(t) { let e = [], r = [], i, s; for (let a of t.sequences || []) a.canvases.length && e.push(...a.canvases), a.behavior && r.push(...a.behavior), a.viewingDirection && (s = a.viewingDirection), a.startCanvas && (i = a.startCanvas); let n = C(t); return r.length && (n.behavior ? n.behavior.push(...r) : n.behavior = r), d({ ...n, ...b(t), ...L(t), viewingDirection: s, start: i, items: e, structures: bt(t.structures) }); } function bt(t) { if (!t) return t; let e = /* @__PURE__ */ new Map(); for (let i of t) e.set(i.id, i); let r = []; for (let i of t) if (i.items) { let s = i.items.map((n) => typeof n == "string" ? (r.push(n), e.get(n) || n) : n && n.id ? (r.push(n.id), e.get(n.id) || n) : n); i.items = s; } return t.filter((i) => r.indexOf(i.id) === -1); } function Rt(t) { return d({ ...C(t), ...b(t), ...L(t), annotations: t.otherContent && t.otherContent.length ? t.otherContent : void 0, items: t.images && t.images.length ? [{ id: te(t, "annotation-page"), type: "AnnotationPage", items: t.images }] : void 0 }); } function Lt(t) { return d({ ...C(t), ...b(t), ...L(t), items: t.resources && t.resources.length ? t.resources : void 0 }); } function wt(t) { return !t.canvases || t.canvases.length === 0 ? { canvases: [], behavior: [] } : { canvases: t.canvases, behavior: t.viewingHint ? [t.viewingHint] : [], viewingDirection: t.viewingDirection, startCanvas: t.startCanvas }; } function It(t) { function e(r) { if (Array.isArray(r)) { if (r.length > 1) return { type: "List", items: r.map(e) }; r = r[0]; } if (typeof r == "string") return encodeURI(r).trim(); if ("@type" in r) { let i; if (typeof r.full == "string") i = r.full; else if (r.full["@type"] === "dctypes:Image") i = { id: r.full["@id"], type: "Image" }; else if (r.full["@type"] === "sc:Canvas") i = { id: r.full["@id"], type: "Canvas" }; else throw new Error(`Unsupported source type on annotation: ${r.full["@type"]}`); return { type: "SpecificResource", source: i, selector: O(r.selector) }; } else return encodeURI(r["@id"]).trim(); } return d({ ...C(t), ...b(t), ...L(t), target: e(t.on), body: Array.isArray(t.resource) ? t.resource.map(B) : B(t.resource) }); } function B(t) { return t.type === "Choice" ? t : re(t); } function re(t) { let e = t; return d({ ...C(e), ...b(e), ...L(e), ...gt(e) }); } function St(t) { let e = []; return t.default && t.default !== "rdf:nil" && e.push(t.default), t.item && t.item !== "rdf:nil" && e.push(...t.item), d({ ...C(t), ...b(t), items: e }); } function xt(t) { return d({ ...C(t), ...b(t), ...L(t), items: t.members }); } function Tt(t) { let { "@id": e, "@type": r, "@context": i, profile: s, ...n } = t, a = {}; return e && (a["@id"] = e), a["@type"] = D(t), a["@type"] === "unknown" && (i && i.length && (a["@context"] = i), a["@type"] = "Service"), s && (a.profile = ee(s)), d({ ...a, ...n }); } function Mt(t) { return d({ ...C(t), ...b(t), ...L(t) }); } var $t = new De({ collection: [At], manifest: [Ct], canvas: [Rt], annotationList: [Lt], sequence: [wt], annotation: [It], contentResource: [re], choice: [St], range: [xt], service: [Tt], layer: [Mt] }); function ie(t) { return t && t["@context"] && (t["@context"] === "http://iiif.io/api/presentation/2/context.json" || t["@context"].indexOf("http://iiif.io/api/presentation/2/context.json") !== -1 || t["@context"] === "http://www.shared-canvas.org/ns/context.json") || t["@context"] === "http://iiif.io/api/image/2/context.json" || t["@id"] && t["@type"] === "sc:Collection" || t["@id"] && t["@type"] === "sc:Manifest" ? (t["@context"] || (t["@context"] = "http://iiif.io/api/presentation/2/context.json"), $t.traverseUnknown(t)) : t; } function O(t) { if ((Array.isArray(t["@type"]) && t["@type"].includes("oa:SvgSelector") || t["@type"] == "oa:SvgSelector") && ("chars" in t || "value" in t)) return { type: "SvgSelector", value: "chars" in t ? t.chars : t.value }; if (t["@type"] === "oa:FragmentSelector") return { type: "FragmentSelector", value: t.value }; if (t["@type"] === "oa:Choice") return [O(t.default), ...(Array.isArray(t.item) ? t.item : [t.item]).map(O)]; if (t["@type"] == "iiif:ImageApiSelector") return { type: "ImageApiSelector", region: "region" in t ? t.region : void 0, rotation: "rotation" in t ? t.rotation : void 0 }; throw new Error(`Unsupported selector type: ${t["@type"]}`); } function j(t) { return typeof t == "string" ? !1 : t && !t.type && "source" in t ? (t.type = "SpecificResource", !0) : !!t && t.type === "SpecificResource"; } function x(...t) { return (e) => t.reduce((r, i) => i(r), e); } var V = ["Collection", "Manifest", "Canvas", "AnnotationPage", "AnnotationCollection", "Annotation", "ContentResource", "Range", "Service", "Selector", "Agent"]; function jt(t, e) { if (typeof t > "u" || t === null) throw new Error("Null or undefined is not a valid entity."); if (Array.isArray(t)) throw new Error("Array is not a valid entity"); if (typeof t != "object") { if (e) return e; throw new Error(`${typeof t} is not a valid entity`); } if (typeof t.type == "string") { let r = V.indexOf(t.type); if (r !== -1) return V[r]; } if (t.profile) return "Service"; throw new Error("Resource type is not known"); } var N = class se { constructor(e, r = {}) { R(this, "traversals"), R(this, "options"), R(this, "_traverseManifest", x(this.traverseManifestItems.bind(this), this.traverseNavPlace.bind(this), this.traverseLinking.bind(this), this.traverseDescriptive.bind(this), this.traverseLinkedCanvases.bind(this), this.traverseManifestStructures.bind(this), this.traverseInlineAnnotationPages.bind(this))), R(this, "_traverseCanvas", x(this.traverseCanvasItems.bind(this), this.traverseLinking.bind(this), this.traverseDescriptive.bind(this), this.traverseLinkedCanvases.bind(this), this.traverseInlineAnnotationPages.bind(this))), R(this, "_traverseAnnotationPage", x(this.traverseAnnotationPageItems.bind(this), this.traverseLinking.bind(this), this.traverseDescriptive.bind(this))), R(this, "_traverseRange", x(this.traverseRangeRanges.bind(this), this.traverseLinking.bind(this), this.traverseDescriptive.bind(this), this.traverseLinkedCanvases.bind(this))), this.traversals = { collection: [], manifest: [], canvas: [], annotationCollection: [], annotationPage: [], annotation: [], contentResource: [], choice: [], range: [], service: [], agent: [], specificResource: [], geoJson: [], ...e }, this.options = { allowUndefinedReturn: !1, ...r }; } static all(e) { return new se({ collection: [e], manifest: [e], canvas: [e], annotationCollection: [e], annotationPage: [e], annotation: [e], contentResource: [e], choice: [e], range: [e], service: [e], geoJson: [e], specificResource: [e], agent: [e] }); } traverseDescriptive(e) { return e.thumbnail && (e.thumbnail = w(e.thumbnail).map((r) => this.traverseType(r, { parent: e }, this.traversals.contentResource))), e.provider && (e.provider = e.provider.map((r) => this.traverseAgent(r, e))), e; } traverseLinking(e) { return e.seeAlso && (e.seeAlso = e.seeAlso.map((r) => this.traverseType(r, { parent: e }, this.traversals.contentResource))), e.service && (e.service = w(e.service).map((r) => this.traverseService(r))), e.services && (e.services = w(e.services).map((r) => this.traverseService(r, e))), e.logo && (e.logo = e.logo.map((r) => this.traverseType(r, { parent: e }, this.traversals.contentResource))), e.homepage && (e.homepage = w(e.homepage).map((r) => this.traverseType(r, { parent: e }, this.traversals.contentResource))), e.partOf && (e.partOf = e.partOf.map((r) => typeof r == "string" || !r.type ? this.traverseType(r, { parent: e }, this.traversals.contentResource) : r.type === "Canvas" ? this.traverseType(r, { parent: e }, this.traversals.canvas) : r.type === "AnnotationCollection" ? this.traverseType(r, { parent: e }, this.traversals.annotationCollection) : r.type === "Collection" ? this.traverseType(r, { parent: e }, this.traversals.collection) : this.traverseType(r, { parent: e }, this.traversals.contentResource))), e.start && (j(e.start) ? e.start = this.traverseSpecificResource(e.start, "Canvas", e) : e.start = this.traverseType(e.start, { parent: e }, this.traversals.canvas)), e.rendering && (e.rendering = e.rendering.map((r) => this.traverseType(r, { parent: e }, this.traversals.contentResource))), e.supplementary && (e.supplementary = e.supplementary.map((r) => this.traverseType(r, { parent: e }, this.traversals.contentResource))), e; } traverseCollectionItems(e) { return e.items && e.items.map((r) => r.type === "Collection" ? this.traverseCollection(r) : this.traverseManifest(r)), e; } traverseCollection(e, r) { return this.traverseType(this.traverseDescriptive(this.traverseNavPlace(this.traverseInlineAnnotationPages(this.traverseLinking(this.traverseLinkedCanvases(this.traverseCollectionItems(e)))))), { parent: r }, this.traversals.collection); } traverseGeoJson(e, r) { return this.traverseType(e, { parent: r }, this.traversals.geoJson); } traverseNavPlace(e) { return e.navPlace && (e.navPlace = this.traverseGeoJson(e.navPlace, e)), e; } traverseManifestItems(e) { return e.items && (e.items = e.items.map((r) => this.traverseCanvas(r))), e; } traverseManifestStructures(e) { return e.structures && (e.structures = e.structures.map((r) => this.traverseRange(r))), e; } traverseManifest(e, r) { return this.traverseType(this._traverseManifest(e), { parent: r }, this.traversals.manifest); } traverseCanvasItems(e) { return e.items = (e.items || []).map((r) => this.traverseAnnotationPage(r, e)), e; } traverseInlineAnnotationPages(e) { return typeof e == "string" || !e || e.annotations && (e.annotations = e.annotations.map((r) => this.traverseAnnotationPage(r, e))), e; } traverseCanvas(e, r) { return this.traverseType(this._traverseCanvas(e), { parent: r }, this.traversals.canvas); } traverseAnnotationPageItems(e) { return e.items && (e.items = e.items.map((r) => this.traverseAnnotation(r, e))), e; } traverseAnnotationPage(e, r) { return this.traverseType(this._traverseAnnotationPage(e), { parent: r }, this.traversals.annotationPage); } traverseAnnotationBody(e) { return Array.isArray(e.body) ? e.body = e.body.map((r) => this.traverseContentResource(r, e)) : e.body && (e.body = this.traverseContentResource(e.body, e)), e; } traverseLinkedCanvases(e) { return e.placeholderCanvas && (e.placeholderCanvas = this.traverseCanvas(e.placeholderCanvas)), e.accompanyingCanvas && (e.accompanyingCanvas = this.traverseCanvas(e.accompanyingCanvas)), e; } traverseAnnotation(e, r) { return this.traverseType(this.traverseLinking(this.traverseAnnotationBody(this.traverseDescriptive(e))), { parent: r }, this.traversals.annotation); } traverseContentResourceLinking(e) { return typeof e == "string" || !e || e && e.service && (e.service = w(e.service || []).map((r) => this.traverseService(r, e))), e; } traverseContentResource(e, r) { return e.type === "Choice" && (e.items = e.items.map((i) => this.traverseContentResource(i, e))), j(e) ? this.traverseSpecificResource(e, "ContentResource") : this.traverseType(this.traverseInlineAnnotationPages(this.traverseContentResourceLinking(e)), { parent: r }, this.traversals.contentResource); } traverseSpecificResource(e, r, i) { let s = e.source; return typeof e.source == "string" && (s = { id: e.source, type: r || "unknown" }), this.traverseType({ ...e, source: r === "Canvas" || s.type === "Canvas" ? this.traverseType(s, { parent: i }, this.traversals.canvas) : r === "ContentResource" ? this.traverseContentResource(s, { parent: i }) : this.traverseUnknown(s, { parent: i, typeHint: r }) }, { parent: i }, this.traversals.specificResource); } traverseRangeRanges(e) { return e.items && (e.items = e.items.map((r) => typeof r == "string" ? this.traverseCanvas({ id: r, type: "Canvas" }, e) : j(r) ? this.traverseSpecificResource(r, "Canvas", e) : r.type === "Manifest" ? this.traverseManifest(r, e) : this.traverseRange(r, e))), e; } traverseRange(e, r) { return this.traverseType(this._traverseRange(e), { parent: r }, this.traversals.range); } traverseAgent(e, r) { return this.traverseType(this.traverseDescriptive(this.traverseLinking(e)), { parent: r }, this.traversals.agent); } traverseType(e, r, i) { return i.reduce((s, n) => { let a = n(s, r); return typeof a > "u" && !this.options.allowUndefinedReturn ? s : a; }, e); } traverseService(e, r) { let i = Object.assign({}, e); return i && i.service && (i.service = w(i.service).map((s) => this.traverseService(s))), this.traverseType(i, { parent: r }, this.traversals.service); } traverseUnknown(e, { parent: r, typeHint: i } = {}) { let s = jt(e, i); switch (s) { case "Collection": return this.traverseCollection(e, r); case "Manifest": return this.traverseManifest(e, r); case "Canvas": return this.traverseCanvas(e, r); case "AnnotationPage": return this.traverseAnnotationPage(e, r); case "Annotation": return this.traverseAnnotation(e, r); case "ContentResource": return this.traverseContentResource(e, r); case "Range": return this.traverseRange(e, r); case "Service": return this.traverseService(e, r); case "Agent": return this.traverseAgent(e, r); default: throw new Error(`Unknown or unsupported resource type of ${s}`); } } }; const ne = (t) => g(t, "type").startsWith("ImageService") || t.profile && t.profile.toString().includes("iiif.io/api/image/"), ae = (t) => { const e = g(t, "type"), r = g(t, "context"); if (e === "ImageService2" || r != null && r.includes("image/2")) { const i = t, s = ["level0", "level1", "level2"]; return { majorVersion: 2, profileLevel: (Array.isArray(i.profile) ? i.profile : [i.profile]).map((c) => s.findIndex((o) => c.toString().includes(o))).filter((c) => c > -1).sort((c, o) => o - c)[0] }; } else if (e || r) return { majorVersion: 3, profileLevel: parseInt(t.profile) }; }, U = (t, e, r) => { const i = g(t, "id"), s = t.profile || ""; if (typeof s == "string" && (s.includes("level0") || s.includes("level:0"))) { if ("sizes" in t && Array.isArray(t.sizes)) { const a = t.sizes.sort((c, o) => o.width * o.height - c.width * c.height).filter((c) => c.width * c.height >= e * r)[0]; if (a) return `${i}/full/${a.width},${a.height}/0/default.jpg`; } return `${i}/full/full/0/default.jpg`; } return `${i}/full/!${e},${r}/0/default.jpg`; }, kt = (t, e, r = { minSize: 400 }) => { const i = g(t, "id"), s = t.profile || ""; if (typeof s == "string" && (s.includes("level0") || s.includes("level:0"))) { console.warn(`Level 0 image service does not support custom region URLs: ${i}`); return; } const { x: a, y: c, w: o, h: p } = e, { minSize: u = 400, maxSize: v } = r, m = o / p, l = m < 1; let f = Math.ceil(l ? u / m : u), h = Math.ceil(l ? u : u / m); v && (h > v || f > v) && (l ? (f = Math.min(f, v), h = Math.ceil(f * m)) : (h = Math.min(h, v), f = Math.ceil(h / m))); const y = `${Math.round(a)},${Math.round(c)},${Math.round(o)},${Math.round(p)}`; return `${i}/${y}/!${h},${f}/0/default.jpg`; }, Pt = (t) => (e, r = { minSize: 400 }) => { if (t.type === "dynamic") return kt(t.service, e, r); console.error("Level 0 or static image canvas: unsupported"); }, Ot = (t, e = []) => (r = 400) => { const { width: i, height: s } = t; if (!i || !s) return; const n = i / s, a = n < 1, c = Math.ceil(a ? r / n : r), o = Math.ceil(a ? r : r / n); if (t.thumbnail && t.thumbnail.length > 0) { const p = t.thumbnail[0]; if ("service" in p && Array.isArray(p.service)) { const u = p.service.find((v) => ne(v)); if (u) return U(u, o, c); } if ("id" in p) return p.id; } for (const p of e) { if (p.type === "dynamic" || p.type === "level0") return U(p.service, o, c); if (p.type === "static") return p.url; } }, oe = (t) => t.endsWith("/info.json") ? t : `${t.endsWith("/") ? t : `${t}/`}info.json`, Ut = (t, e, r) => (i = 800) => { if (!t || !e) return; const s = t / e, n = s < 1, a = Math.ceil(n ? i / s : i), c = Math.ceil(n ? i : i / s); return U(r, c, a); }, Dt = (t) => () => fetch(t).then((e) => e.json()).then((e) => { const r = e.width, i = e.height; return r !== void 0 && i !== void 0 ? { width: r, height: i } : void 0; }), Nt = (t) => () => { const e = typeof window < "u"; return fetch(t).then((r) => r.blob()).then((r) => e ? createImageBitmap(r).then((i) => { const { width: s, height: n } = i; return i.close(), { width: s, height: n }; }) : r.arrayBuffer().then((i) => le(new Uint8Array(i)))); }, Et = (t) => { const { format: e, height: r, width: i } = t, s = g(t, "id"), n = (t.service || []).find(ne), a = n ? ae(n) : void 0; if (n && a) { const c = oe(g(n, "id")), o = { source: t, type: a.profileLevel === 0 ? "level0" : "dynamic", service: n, width: i, height: r, majorVersion: a.majorVersion, serviceUrl: c, getImageURL: Ut(i, r, n), getPixelSize: Dt(c) }; return a.profileLevel === 0 ? o : { ...o, getRegionURL: Pt(o) }; } else return { source: t, type: "static", width: i, height: r, url: s, format: e, getImageURL: () => s, getPixelSize: Nt(s) }; }, _t = (t) => { const e = []; return new N({ annotation: [(i) => { if (i.motivation === "painting" || !i.motivation) { const n = (i.body ? Array.isArray(i.body) ? i.body : [i.body] : []).filter((a) => a.type === "Image"); e.push(...n.map((a) => Et(a))); } }] }).traverseCanvas(t), e; }, qt = (t) => () => { const e = /* @__PURE__ */ new Map(), r = (o, p, u = 0) => { const v = { id: o.id, type: "range", source: o, children: [], navItems: [], navSections: [], parent: p, level: u, getLabel: o.getLabel }; o.items && o.items.length > 0 && o.items.forEach((h) => { if (h.source.type === "Range") { const T = r(h, v, u + 1); v.children.push(T); } else { const y = { id: h.id, type: "canvas", source: h, children: [], navItems: [], navSections: [], parent: v, level: u + 1, getLabel: h.getLabel }; v.children.push(y), e.set(y.id, y); } }); const m = v.children.map((h) => h.type === "canvas" ? h.source : h.children.length === 1 && h.children[0].type === "canvas" ? h.children[0].source : h.source), l = m.filter((h) => h.source.type === "Canvas"), f = m.filter((h) => h.source.type === "Range"); return v.navItems.push(...l), v.navSections.push(...f), e.set(v.id, v), v; }; return { root: t.filter((o) => { var p; return (p = o.source.behavior) == null ? void 0 : p.includes("top"); }).map((o) => r(o, void 0)), enumerateNodes: (o) => { const p = Array.from(e.values()); return o ? p.filter((u) => u.type === o) : p; }, getBreadcrumbs: (o) => { const p = e.get(o); if (!p) return []; const u = (v, m = []) => v.parent ? u(v.parent, [v, ...m]) : [v, ...m]; return u(p); }, getNode: (o) => e.get(o) }; }, Bt = async (t) => { try { new URL(t); } catch { return { type: "error", code: "INVALID_URL", message: "The provided input is not a valid URL" }; } let e; try { if (e = await fetch(t), !e.ok) return { type: "error", code: "INVALID_HTTP_RESPONSE", message: `Server responded: HTTP ${e.status} ${e.statusText ? `(${e.statusText})` : ""}` }; } catch (i) { return { type: "error", code: "FETCH_ERROR", message: i instanceof Error ? i.message : "Failed to fetch resource" }; } const r = e.headers.get("content-type"); if (r != null && r.startsWith("image/")) return { type: "plain-image", url: t }; if (r != null && r.includes("text/html")) return { type: "webpage", url: t }; try { const i = await e.json(); return ce(i, t); } catch { return { type: "error", code: "UNSUPPORTED_FORMAT", message: "Could not parse resource" }; } }, ce = (t, e) => { const r = Array.isArray(t["@context"]) ? t["@context"].find((s) => s.includes("iiif.io/api/presentation") || s.includes("iiif.io/api/image")) : t["@context"]; if (!r) return { type: "error", code: "INVALID_MANIFEST", message: "Missing @context" }; const i = g(t, "id"); if (!i) return { type: "error", code: "INVALID_MANIFEST", message: "Missing id property" }; if (r.includes("presentation/2") || r.includes("presentation/3")) { const s = r.includes("presentation/2") ? 2 : 3; return g(t, "type").includes("Collection") ? { type: "collection", url: e || i, resource: Vt(t, s) } : { type: "manifest", url: e || i, resource: Ft(t, s) }; } if (r.includes("image/2") || r.includes("image/3")) { const s = Wt(t); return s ? { type: "iiif-image", url: e || i, resource: s } : { type: "error", code: "INVALID_MANIFEST", message: "Invalid image service definition" }; } return { type: "error", code: "INVALID_MANIFEST", message: "JSON resource is not a recognized IIIF format" }; }, Vt = (t, e) => { const r = (n) => { const a = []; return new N({ manifest: [(o) => a.push(o)] }).traverseCollection(n), a.map((o) => ({ id: o.id, type: o.type, getLabel: S(o), source: o })); }, i = e === 2 ? ie(t) : t, s = r(i); return { source: i, id: i.id, majorVersion: e, items: s, getLabel: S(i), getMetadata: k(i) }; }, Ft = (t, e) => { const r = (a) => { const c = [], o = []; new N({ canvas: [(l) => { l.items && c.push(l); }], range: [(l) => { l.type === "Range" && o.push(l); }] }).traverseManifest(a); const u = c.map((l) => { const f = _t(l); return { source: l, id: l.id, width: l.width, height: l.height, images: f, annotations: l.annotations || [], getImageURL: f.length > 0 ? f[0].getImageURL : () => { }, getLabel: S(l), getMetadata: k(l), getThumbnailURL: Ot(l, f) }; }), v = (l) => { const f = l.items || [], h = f.filter((A) => A.type === "Canvas").map((A) => u.find((M) => M.id === A.id)).filter(Boolean), y = f.filter((A) => A.type === "Range").map((A) => v(A)), T = [...h, ...y]; return { source: l, id: l.id, // Maintain original order items: f.map((A) => T.find((M) => M.id === A.id)), canvases: h, ranges: y, getLabel: S(l) }; }, m = o.map((l) => v(l)); return { canvases: u, ranges: m }; }, i = e === 2 ? ie(t) : t, { canvases: s, ranges: n } = r(i); return { source: i, id: i.id, majorVersion: e, canvases: s, structure: n, getLabel: S(i), getMetadata: k(i), getTableOfContents: qt(n) }; }, Wt = (t) => { const { width: e, height: r } = t, i = ae(t); if (i) return { type: i.profileLevel === 0 ? "level0" : "dynamic", service: t, width: e, height: r, majorVersion: i.majorVersion, serviceUrl: oe(g(t, "id")) }; }, Jt = { parse: ce, parseURL: Bt }; export { Jt as Cozy, U as getImageURLFromService, _t as getImages, S as getLabel, k as getMetadata, g as getPropertyValue, Pt as getRegionURL, kt as getRegionURLFromService, Qt as getStringValue, qt as getTableOfContents, Ot as getThumbnailURL, ne as isImageService, oe as normalizeServiceUrl, ae as parseImageService };