// @observablehq/notebook-inspector Copyright 2018 Observable, Inc.
function dispatch(e, t, n) {n = n || {};var a = e.ownerDocument,o = a.defaultView.CustomEvent;"function" == typeof o ? o = new o(t, { detail: n }) : ((o = a.createEvent("Event")).initEvent(t, !1, !1), o.detail = n), e.dispatchEvent(o);}function isarray(e) {return Array.isArray(e) || e instanceof Int8Array || e instanceof Int16Array || e instanceof Int32Array || e instanceof Uint8Array || e instanceof Uint8ClampedArray || e instanceof Uint16Array || e instanceof Uint32Array || e instanceof Float32Array || e instanceof Float64Array;}function isindex(e) {return e === (0 | e) + "";}function inspectName(e) {const t = document.createElement("span");return t.className = "observablehq--cellname", t.textContent = `${e} = `, t;}const symbolToString = Symbol.prototype.toString;function formatSymbol(e) {return symbolToString.call(e);}const { getOwnPropertySymbols: getOwnPropertySymbols, prototype: { hasOwnProperty: hasOwnProperty } } = Object,{ toStringTag: toStringTag } = Symbol,FORBIDDEN = {},symbolsof = getOwnPropertySymbols;function isown(e, t) {return hasOwnProperty.call(e, t);}function tagof(e) {return e[toStringTag] || e.constructor && e.constructor.name || "Object";}function valueof(e, t) {try {const n = e[t];return n && n.constructor, n;} catch (e) {return FORBIDDEN;}}function inspectExpanded(e, t, n) {const a = isarray(e);let o, r, i;e instanceof Map ? (o = `Map(${e.size})`, r = iterateMap) : e instanceof Set ? (o = `Set(${e.size})`, r = iterateSet) : a ? (o = `${e.constructor.name}(${e.length})`, r = iterateArray) : (o = tagof(e), r = iterateObject);const s = document.createElement("span");s.className = "observablehq--expanded", n && s.appendChild(inspectName(n));const c = s.appendChild(document.createElement("a"));c.innerHTML = "", c.appendChild(document.createTextNode(`${o}${a ? " [" : " {"}`)), c.addEventListener("mouseup", function (t) {t.stopPropagation(), replace(s, inspectCollapsed(e, null, n));}), r = r(e);for (let e = 0; !(i = r.next()).done && e < 20; ++e) s.appendChild(i.value);if (!i.done) {const e = s.appendChild(document.createElement("a"));e.className = "observablehq--field", e.style.display = "block", e.appendChild(document.createTextNode(" … more")), e.addEventListener("mouseup", function (e) {e.stopPropagation(), s.insertBefore(i.value, s.lastChild.previousSibling);for (let e = 0; !(i = r.next()).done && e < 19; ++e) s.insertBefore(i.value, s.lastChild.previousSibling);i.done && s.removeChild(s.lastChild.previousSibling), dispatch(s, "load");});}return s.appendChild(document.createTextNode(a ? "]" : "}")), s;}function* iterateMap(e) {for (const [t, n] of e) yield formatMapField(t, n);yield* iterateObject(e);}function* iterateSet(e) {for (const t of e) yield formatSetField(t);yield* iterateObject(e);}function* iterateArray(e) {for (let t = 0, n = e.length; t < n; ++t) t in e && (yield formatField(t, valueof(e, t), "observablehq--index"));for (const t in e) !isindex(t) && isown(e, t) && (yield formatField(t, valueof(e, t), "observablehq--key"));for (const t of symbolsof(e)) yield formatField(formatSymbol(t), valueof(e, t), "observablehq--symbol");}function* iterateObject(e) {for (const t in e) isown(e, t) && (yield formatField(t, valueof(e, t), "observablehq--key"));for (const t of symbolsof(e)) yield formatField(formatSymbol(t), valueof(e, t), "observablehq--symbol");}function formatField(e, t, n) {const a = document.createElement("div"),o = a.appendChild(document.createElement("span"));return a.className = "observablehq--field", o.className = n, o.textContent = ` ${e}`, a.appendChild(document.createTextNode(": ")), a.appendChild(inspect(t)), a;}function formatMapField(e, t) {const n = document.createElement("div");return n.className = "observablehq--field", n.appendChild(document.createTextNode(" ")), n.appendChild(inspect(e)), n.appendChild(document.createTextNode(" => ")), n.appendChild(inspect(t)), n;}function formatSetField(e) {const t = document.createElement("div");return t.className = "observablehq--field", t.appendChild(document.createTextNode(" ")), t.appendChild(inspect(e)), t;}function inspectCollapsed(e, t, n) {const a = isarray(e);let o, r, i;if (e instanceof Map ? (o = `Map(${e.size})`, r = iterateMap$1) : e instanceof Set ? (o = `Set(${e.size})`, r = iterateSet$1) : a ? (o = `${e.constructor.name}(${e.length})`, r = iterateArray$1) : (o = tagof(e), r = iterateObject$1), t) {const t = document.createElement("span");return t.className = "observablehq--shallow", n && t.appendChild(inspectName(n)), t.appendChild(document.createTextNode(o)), t.addEventListener("mouseup", function (n) {n.stopPropagation(), replace(t, inspectCollapsed(e));}), t;}const s = document.createElement("span");s.className = "observablehq--collapsed", n && s.appendChild(inspectName(n));const c = s.appendChild(document.createElement("a"));c.innerHTML = "", c.appendChild(document.createTextNode(`${o}${a ? " [" : " {"}`)), s.addEventListener("mouseup", function (t) {t.stopPropagation(), replace(s, inspectExpanded(e, null, n));}, !0), r = r(e);for (let e = 0; !(i = r.next()).done && e < 20; ++e) e > 0 && s.appendChild(document.createTextNode(", ")), s.appendChild(i.value);return i.done || s.appendChild(document.createTextNode(", …")), s.appendChild(document.createTextNode(a ? "]" : "}")), s;}function* iterateMap$1(e) {for (const [t, n] of e) yield formatMapField$1(t, n);yield* iterateObject$1(e);}function* iterateSet$1(e) {for (const t of e) yield inspect(t, !0);yield* iterateObject$1(e);}function* iterateArray$1(e) {for (let t = -1, n = 0, a = e.length; n < a; ++n) if (n in e) {let a = n - t - 1;if (a > 0) {const e = document.createElement("span");e.className = "observablehq--empty", e.textContent = 1 === a ? "empty" : `empty × ${n - t - 1}`, yield e;}yield inspect(valueof(e, n), !0), t = n;}for (const t in e) !isindex(t) && isown(e, t) && (yield formatField$1(t, valueof(e, t), "observablehq--key"));for (const t of symbolsof(e)) yield formatField$1(formatSymbol(t), valueof(e, t), "observablehq--symbol");}function* iterateObject$1(e) {for (const t in e) isown(e, t) && (yield formatField$1(t, valueof(e, t), "observablehq--key"));for (const t of symbolsof(e)) yield formatField$1(formatSymbol(t), valueof(e, t), "observablehq--symbol");}function formatField$1(e, t, n) {const a = document.createDocumentFragment(),o = a.appendChild(document.createElement("span"));return o.className = n, o.textContent = e, a.appendChild(document.createTextNode(": ")), a.appendChild(inspect(t, !0)), a;}function formatMapField$1(e, t) {const n = document.createDocumentFragment();return n.appendChild(inspect(e, !0)), n.appendChild(document.createTextNode(" => ")), n.appendChild(inspect(t, !0)), n;}function pad(e, t) {var n = e + "",a = n.length;return a < t ? new Array(t - a + 1).join(0) + n : n;}function formatDate(e) {return isNaN(e) ? "Invalid Date" : pad(e.getFullYear(), 4) + "-" + pad(e.getMonth() + 1, 2) + "-" + pad(e.getDate(), 2) + (e.getMilliseconds() ? "T" + pad(e.getHours(), 2) + ":" + pad(e.getMinutes(), 2) + ":" + pad(e.getSeconds(), 2) + "." + pad(e.getMilliseconds(), 3) : e.getSeconds() ? "T" + pad(e.getHours(), 2) + ":" + pad(e.getMinutes(), 2) + ":" + pad(e.getSeconds(), 2) : e.getMinutes() || e.getHours() ? "T" + pad(e.getHours(), 2) + ":" + pad(e.getMinutes(), 2) : "");}var errorToString = Error.prototype.toString;function formatError(e) {return e.stack || errorToString.call(e);}var regExpToString = RegExp.prototype.toString;function formatRegExp(e) {return regExpToString.call(e);}const NEWLINE_LIMIT = 20;function formatString(e, t, n, a) {if (!1 === t) {if (count(e, /["\n]/g) <= count(e, /`|\${/g)) {const t = document.createElement("span");a && t.appendChild(inspectName(a));const n = t.appendChild(document.createElement("span"));return n.className = "observablehq--string", n.textContent = JSON.stringify(e), t;}const o = e.split("\n");if (o.length > NEWLINE_LIMIT && !n) {const n = document.createElement("div");a && n.appendChild(inspectName(a));const r = n.appendChild(document.createElement("span"));r.className = "observablehq--string", r.textContent = "`" + templatify(o.slice(0, NEWLINE_LIMIT).join("\n"));const i = n.appendChild(document.createElement("span")),s = o.length - NEWLINE_LIMIT;return i.textContent = `Show ${s} truncated line${s > 1 ? "s" : ""}`, i.className = "observablehq--string-expand", i.addEventListener("mouseup", function (o) {o.stopPropagation(), replace(n, inspect(e, t, !0, a));}), n;}const r = document.createElement("span");a && r.appendChild(inspectName(a));const i = r.appendChild(document.createElement("span"));return i.className = `observablehq--string${n ? " observablehq--expanded" : ""}`, i.textContent = "`" + templatify(e) + "`", r;}const o = document.createElement("span");a && o.appendChild(inspectName(a));const r = o.appendChild(document.createElement("span"));return r.className = "observablehq--string", r.textContent = JSON.stringify(e.length > 100 ? `${e.slice(0, 50)}…${e.slice(-49)}` : e), o;}function templatify(e) {return e.replace(/[\\`\x00-\x09\x0b-\x19]|\${/g, templatifyChar);}function templatifyChar(e) {var t = e.charCodeAt(0);return t < 16 ? "\\x0" + t.toString(16) : t < 32 ? "\\x" + t.toString(16) : "\\" + e;}function count(e, t) {for (var n = 0; t.exec(e);) ++n;return n;}var toString = Function.prototype.toString,TYPE_ASYNC = { prefix: "async ƒ" },TYPE_ASYNC_GENERATOR = { prefix: "async ƒ*" },TYPE_CLASS = { prefix: "class" },TYPE_FUNCTION = { prefix: "ƒ" },TYPE_GENERATOR = { prefix: "ƒ*" };function inspectFunction(e, t) {var n,a,o = toString.call(e);switch (e.constructor && e.constructor.name) {case "AsyncFunction":n = TYPE_ASYNC;break;case "AsyncGeneratorFunction":n = TYPE_ASYNC_GENERATOR;break;case "GeneratorFunction":n = TYPE_GENERATOR;break;default:n = /^class\b/.test(o) ? TYPE_CLASS : TYPE_FUNCTION;}return n === TYPE_CLASS ? formatFunction(n, "", t) : (a = /^(?:async\s*)?(\w+)\s*=>/.exec(o)) ? formatFunction(n, "(" + a[1] + ")", t) : (a = /^(?:async\s*)?\(\s*(\w+(?:\s*,\s*\w+)*)?\s*\)/.exec(o)) ? formatFunction(n, a[1] ? "(" + a[1].replace(/\s*,\s*/g, ", ") + ")" : "()", t) : (a = /^(?:async\s*)?function(?:\s*\*)?(?:\s*\w+)?\s*\(\s*(\w+(?:\s*,\s*\w+)*)?\s*\)/.exec(o)) ? formatFunction(n, a[1] ? "(" + a[1].replace(/\s*,\s*/g, ", ") + ")" : "()", t) : formatFunction(n, "(…)", t);}function formatFunction(e, t, n) {var a = document.createElement("span");a.className = "observablehq--function", n && a.appendChild(inspectName(n));var o = a.appendChild(document.createElement("span"));return o.className = "observablehq--keyword", o.textContent = e.prefix, a.appendChild(document.createTextNode(t)), a;}const { prototype: { toString: toString$1 } } = Object;function inspect(e, t, n, a) {let o = typeof e;switch (o) {case "boolean":case "undefined":e += "";break;case "number":e = 0 === e && 1 / e < 0 ? "-0" : e + "";break;case "bigint":e += "n";break;case "symbol":e = formatSymbol(e, a);break;case "function":return inspectFunction(e, a);case "string":return formatString(e, t, n, a);default:if (null === e) {o = null, e = "null";break;}if (e instanceof Date) {o = "date", e = formatDate(e, a);break;}if (e === FORBIDDEN) {o = "forbidden", e = "[forbidden]";break;}switch (toString$1.call(e)) {case "[object RegExp]":o = "regexp", e = formatRegExp(e, a);break;case "[object Error]":case "[object DOMException]":o = "error", e = formatError(e, a);break;default:return (n ? inspectExpanded : inspectCollapsed)(e, t, a);}}const r = document.createElement("span");a && r.appendChild(inspectName(a));const i = r.appendChild(document.createElement("span"));return i.className = `observablehq--${o}`, i.textContent = e, r;}function replace(e, t) {e.classList.contains("observablehq--inspect") && t.classList.add("observablehq--inspect"), e.parentNode.replaceChild(t, e), dispatch(t, "load");}const LOCATION_MATCH = /\s+\(\d+:\d+\)$/m;class Inspector {constructor(e) {if (!e) throw new Error("invalid node");this._node = e, e.classList.add("observablehq");}pending() {const { _node: e } = this;e.classList.remove("observablehq--error"), e.classList.add("observablehq--running");}fulfilled(e, t) {const { _node: n } = this;if ((!(e instanceof Element || e instanceof Text) || e.parentNode && e.parentNode !== n) && (e = inspect(e, !1, n.firstChild && n.firstChild.classList && n.firstChild.classList.contains("observablehq--expanded"), t)).classList.add("observablehq--inspect"), n.classList.remove("observablehq--running", "observablehq--error"), n.firstChild !== e) if (n.firstChild) {for (; n.lastChild !== n.firstChild;) n.removeChild(n.lastChild);n.replaceChild(e, n.firstChild);} else n.appendChild(e);dispatch(n, "update");}rejected(e, t) {const { _node: n } = this;for (n.classList.remove("observablehq--running"), n.classList.add("observablehq--error"); n.lastChild;) n.removeChild(n.lastChild);var a = document.createElement("div");a.className = "observablehq--inspect", t && a.appendChild(inspectName(t)), a.appendChild(document.createTextNode((e + "").replace(LOCATION_MATCH, ""))), n.appendChild(a), dispatch(n, "error", { error: e });}}Inspector.into = function (e) {if ("string" == typeof e && null == (e = document.querySelector(e))) throw new Error("container not found");return function () {return new Inspector(e.appendChild(document.createElement("div")));};};export { Inspector };