// @bun
var{defineProperty:$,getOwnPropertyNames:T,getOwnPropertyDescriptor:M}=Object,C=Object.prototype.hasOwnProperty;function I(q){return this[q]}var h=(q)=>{var z=(B??=new WeakMap).get(q),G;if(z)return z;if(z=$({},"__esModule",{value:!0}),q&&typeof q==="object"||typeof q==="function"){for(var E of T(q))if(!C.call(z,E))$(z,E,{get:I.bind(q,E),enumerable:!(G=M(q,E))||G.enumerable})}return B.set(q,z),z},B;var b=(q)=>q;function y(q,z){this[q]=b.bind(null,z)}var p=(q,z)=>{for(var G in z)$(q,G,{get:z[G],enumerable:!0,configurable:!0,set:y.bind(z,G)})};var F=(q,z)=>()=>(q&&(z=q(q=0)),z);import{isSchemaModel as g}from"@contractspec/lib.schema/SchemaModel";function r(q,z,G={}){let{format:E="auto",title:J,description:P,maxDepth:H=2}=G,A=[];if(J)A.push(`# ${J}`,"");if(P)A.push(`> ${P}`,"");let K=E==="auto"?Array.isArray(z)?"table":"detail":E;if(K==="table"&&Array.isArray(z))A.push(V(q,z,G));else if(K==="list"&&Array.isArray(z))A.push(x(q,z,G));else if(!Array.isArray(z)&&z!==null&&typeof z==="object")A.push(D(q,z,{...G,maxDepth:H}));else A.push(`**Data:** ${String(z)}`,"");return A.join(`
`)}function V(q,z,G={}){let{maxItems:E=100,includeFields:J,excludeFields:P=[]}=G,H=L(q,J,P);if(H.length===0||z.length===0)return`_No data available_
`;let A=z.slice(0,E),K=[],R=H.map((Q)=>Q.label);K.push(`| ${R.join(" | ")} |`),K.push(`| ${R.map(()=>"---").join(" | ")} |`);for(let Q of A){let U=H.map((W)=>Z(Y(Q,W.name)));K.push(`| ${U.join(" | ")} |`)}if(z.length>E)K.push("",`_Showing ${E} of ${z.length} items_`);return K.push(""),K.join(`
`)}function x(q,z,G={}){let{maxItems:E=50,summaryFields:J,includeFields:P,excludeFields:H=[]}=G,A=L(q,P,H),K=J?A.filter((U)=>J.includes(U.name)):A.slice(0,3);if(K.length===0||z.length===0)return`_No data available_
`;let R=z.slice(0,E),Q=[];for(let U of R){let W=Z(Y(U,K[0]?.name??"id")),O=K.slice(1).map((k)=>`${k.label}: ${Z(Y(U,k.name))}`).join(" \xB7 "),S=O?`- **${W}** (${O})`:`- **${W}**`;Q.push(S)}if(z.length>E)Q.push("",`_Showing ${E} of ${z.length} items_`);return Q.push(""),Q.join(`
`)}function D(q,z,G={}){let{includeFields:E,excludeFields:J=[],maxDepth:P=2}=G;if(z===null||typeof z!=="object")return`_No data available_
`;let H=L(q,E,J);if(H.length===0)return`_No fields to display_
`;let A=[];for(let K of H){let R=Y(z,K.name),Q=N(R,P,0);A.push(`**${K.label}:** ${Q}`)}return A.push(""),A.join(`
`)}function o(q,z={}){let{numericFields:G=[],countByField:E}=z,J=[];J.push(`**Total Items:** ${q.length}`);for(let P of G){let H=q.map((A)=>Y(A,P)).filter((A)=>typeof A==="number");if(H.length>0){let A=H.reduce((U,W)=>U+W,0),K=A/H.length,R=Math.min(...H),Q=Math.max(...H);J.push(`**${j(P)}:** Sum: ${X(A)}, Avg: ${X(K)}, Min: ${X(R)}, Max: ${X(Q)}`)}}if(E){let P={};for(let H of q){let A=String(Y(H,E)??"Unknown");P[A]=(P[A]??0)+1}J.push("",`**By ${j(E)}:**`);for(let[H,A]of Object.entries(P))J.push(`- ${H}: ${A}`)}return J.push(""),J.join(`
`)}function L(q,z,G=[]){if(!g(q))return[];let E=q.config.fields;return Object.keys(E).filter((H)=>{if(G.includes(H))return!1;if(z&&!z.includes(H))return!1;return!0}).map((H)=>{let A=E[H],K=Boolean(A?.type&&typeof A.type==="object"&&"config"in A.type&&typeof A.type.config?.fields==="object");return{name:H,label:j(H),isOptional:A?.isOptional??!1,isArray:A?.isArray===!0,isNested:K}})}function Y(q,z){if(q===null||typeof q!=="object")return;let G=q;if(z.includes(".")){let E=z.split("."),J=G;for(let P of E){if(J===null||typeof J!=="object")return;J=J[P]}return J}return G[z]}function Z(q){if(q===null||q===void 0)return"-";if(typeof q==="boolean")return q?"\u2713":"\u2717";if(typeof q==="number")return X(q);if(q instanceof Date)return _(q);if(typeof q==="string"&&w(q))return _(new Date(q));if(Array.isArray(q))return`[${q.length} items]`;if(typeof q==="object")return"[Object]";let z=String(q);return z.length>50?`${z.slice(0,47)}...`:z.replace(/\|/g,"\\|")}function N(q,z,G){if(q===null||q===void 0)return"_none_";if(typeof q==="boolean")return q?"Yes":"No";if(typeof q==="number")return X(q);if(q instanceof Date)return _(q);if(typeof q==="string"&&w(q))return _(new Date(q));if(Array.isArray(q)){if(q.length===0)return"_empty list_";if(G>=z)return`[${q.length} items]`;if(q.length<=5&&q.every((E)=>typeof E!=="object"))return q.map((E)=>Z(E)).join(", ");return`[${q.length} items]`}if(typeof q==="object"){if(G>=z)return"[Object]";return`{${Object.keys(q).length} fields}`}return String(q)}function j(q){return q.replace(/([a-z])([A-Z])/g,"$1 $2").replace(/_/g," ").replace(/^\w/,(z)=>z.toUpperCase())}function X(q){if(Math.abs(q)>=1000)return new Intl.NumberFormat("en-US",{maximumFractionDigits:2}).format(q);if(Number.isInteger(q))return String(q);return q.toFixed(2)}function _(q){return q.toLocaleDateString("en-US",{year:"numeric",month:"short",day:"numeric"})}function w(q){if(!/^\d{4}-\d{2}-\d{2}/.test(q))return!1;let z=new Date(q);return!isNaN(z.getTime())}export{V as schemaToMarkdownTable,o as schemaToMarkdownSummary,x as schemaToMarkdownList,D as schemaToMarkdownDetail,r as schemaToMarkdown};
