"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var components_exports = {}; __export(components_exports, { ErrorBoundary: () => ErrorBoundary, childrenToString: () => childrenToString }); module.exports = __toCommonJS(components_exports); var import_html = require("../helper/html"); var import_html2 = require("../utils/html"); var import_constants = require("./constants"); var import_components = require("./dom/components"); let errorBoundaryCounter = 0; const childrenToString = async (children) => { try { return children.flat().map((c) => c == null || typeof c === "boolean" ? "" : c.toString()); } catch (e) { if (e instanceof Promise) { await e; return childrenToString(children); } else { throw e; } } }; const ErrorBoundary = async ({ children, fallback, fallbackRender, onError }) => { if (!children) { return (0, import_html.raw)(""); } if (!Array.isArray(children)) { children = [children]; } let fallbackStr; const fallbackRes = (error) => { onError?.(error); return (fallbackStr || fallbackRender?.(error) || "").toString(); }; let resArray = []; try { resArray = children.map( (c) => c == null || typeof c === "boolean" ? "" : c.toString() ); } catch (e) { fallbackStr = await fallback?.toString(); if (e instanceof Promise) { resArray = [ e.then(() => childrenToString(children)).catch((e2) => fallbackRes(e2)) ]; } else { resArray = [fallbackRes(e)]; } } if (resArray.some((res) => res instanceof Promise)) { fallbackStr ||= await fallback?.toString(); const index = errorBoundaryCounter++; const replaceRe = RegExp(`(.*?)(.*?)()`); const caught = false; const catchCallback = ({ error, buffer }) => { if (caught) { return ""; } const fallbackResString = fallbackRes(error); if (buffer) { buffer[0] = buffer[0].replace(replaceRe, fallbackResString); } return buffer ? "" : `