import{n as e,t}from"./lib-u36oR1k0.js";import{a as n,c as r,d as i,f as a,i as o,l as s,n as c,o as l,t as u,u as d}from"./workbook_model-CeXzIW3o.js";function f(e){let{ok:t,data:n}=i(e);if(!t)return`<p>Unable to parse spreadsheet data.</p>`;if(!n?.workbook?.sheets)return`<p>Empty spreadsheet.</p>`;let{workbook:a}=n,o=s(a);if(o.length===0)return`<p>Empty spreadsheet.</p>`;let c=[];for(let e of o){o.length>1&&c.push(`<h3>${I(e.name)}</h3>`);let t=p(e,r(a,e.id)),n=S(e,a.styles??{},t);c.push(h(n,t))}return c.join(`
`)}function p(e,t){let n=e.rowHeader?.hidden?0:d(e.rowHeader?.width)?e.rowHeader.width:0,r=e.columnHeader?.hidden?0:d(e.columnHeader?.height)?e.columnHeader.height:0,i=[];for(let e of t){let t=_(e.source);!t||!e.transform||i.push({src:t,left:y(e.transform.left)-n,top:y(e.transform.top)-r,width:y(e.transform.width),height:y(e.transform.height),transform:m(e.transform)})}return i}function m(e){let t=[];return d(e.angle)&&e.angle%360!=0&&t.push(`rotate(${v(e.angle)}deg)`),e.flipX&&t.push(`scaleX(-1)`),e.flipY&&t.push(`scaleY(-1)`),t.join(` `)}function h(e,t){if(t.length===0)return e;let n=0,r=[];for(let e of t){n=Math.max(n,e.top+e.height);let t=e.transform?`;transform:${e.transform}`:``;r.push(`<img class="spreadsheet-floating-image" style="position:absolute;left:${v(e.left)}px;top:${v(e.top)}px;width:${v(e.width)}px;height:${v(e.height)}px${t}" src="${I(e.src)}" alt="">`)}return`<div class="spreadsheet-sheet" style="position:relative;min-height:${v(n)}px">\n${e}\n${r.join(`
`)}\n</div>`}function g(e){let t=e.p,n=t?.drawings;if(!n)return``;let r=Array.isArray(t?.drawingsOrder)?t.drawingsOrder:Object.keys(n),i=[];for(let e of r){let t=n[e],r=t?_(t.source):null;if(!t||!r)continue;let a=[];d(t.transform?.width)&&a.push(`width:${v(t.transform.width)}px`),d(t.transform?.height)&&a.push(`height:${v(t.transform.height)}px`);let o=a.length?` style="${a.join(`;`)}"`:``;i.push(`<img class="spreadsheet-cell-image"${o} src="${I(r)}" alt="">`)}return i.join(``)}function _(e){if(typeof e!=`string`)return null;let t=e.trim();return/^api\/attachments\/[a-zA-Z0-9_]+\/image\//.test(t)||/^data:image\/(?:png|jpe?g|gif|webp|bmp|svg\+xml)[;,]/i.test(t)?t:null}function v(e){return String(Math.round(e*100)/100)}function y(e){return d(e)?e:0}function b(e,t,n,r){let i=0,a=0;for(let e of r)i=Math.max(i,e.top+e.height),a=Math.max(a,e.left+e.width);return i<=0&&a<=0?{maxRow:t,maxCol:n}:{maxRow:Math.max(t,x(i,e.defaultRowHeight??24,e.rowCount,t=>e.rowData?.[t])),maxCol:Math.max(n,x(a,e.defaultColumnWidth??88,e.columnCount,t=>e.columnData?.[t]))}}function x(e,t,n,r){if(e<=0||t<=0)return 0;let i=d(n)&&n>0?n:1e5,a=0,o=0;for(;a<e&&o<i;){let e=r(o),n=e?.h??e?.w;a+=e?.hd?0:d(n)?n:t,o++}return o-1}function S(e,t,n){let{cellData:r,mergeData:i=[],columnData:o={},rowData:s={}}=e,c=l(r,i),{maxRow:u,maxCol:f}=b(e,c?.maxRow??-1,c?.maxCol??-1,n);if(u<0||f<0)return`<p>Empty sheet.</p>`;let p=T(i,0,u,0,f),m=e.defaultColumnWidth??88,h=[];for(let e=0;e<=f;e++){let t=o[e];t?.hd||h.push(d(t?.w)?t.w:m)}let _=h.reduce((e,t)=>e+t,0),y=[];y.push(C(e,_)),y.push(`<colgroup>`);for(let e of h)y.push(`<col style="width:${v(e)}px">`);y.push(`</colgroup>`);let x=e.defaultRowHeight??24;for(let e=0;e<=u;e++){let n=s[e];if(n?.hd)continue;let i=d(n?.h)?n.h:x;y.push(`<tr style="height:${i}px">`);for(let n=0;n<=f;n++){if(o[n]?.hd)continue;let i=p.get(w(e,n));if(i===`hidden`)continue;let s=r[e]?.[n],c=a(s?.s,t),l=E(c,s),u=P(s,c)+(s?g(s):``),d=[];c?.bg?.rgb&&d.push(`class="has-fill"`),l&&d.push(`style="${l}"`),i&&(i.rowSpan>1&&d.push(`rowspan="${i.rowSpan}"`),i.colSpan>1&&d.push(`colspan="${i.colSpan}"`)),y.push(`<td${d.length?` `+d.join(` `):``}>${u}</td>`)}y.push(`</tr>`)}return y.push(`</table>`),y.join(`
`)}function C(e,t){let n=e.showGridlines!==0,r=n?`spreadsheet-table show-gridlines`:`spreadsheet-table`,i=[];return n&&e.gridlinesColor&&i.push(`--spreadsheet-gridline-color:${N(e.gridlinesColor)}`),i.push(`width:${v(t)}px`),`<table class="${r}" style="${i.join(`;`)}">`}function w(e,t){return`${e},${t}`}function T(e,t,n,r,i){let a=new Map;for(let o of e){let e=Math.max(o.startRow,t),s=Math.min(o.endRow,n),c=Math.max(o.startColumn,r),l=Math.min(o.endColumn,i);a.set(w(o.startRow,o.startColumn),{rowSpan:s-e+1,colSpan:l-c+1});for(let t=e;t<=s;t++)for(let e=c;e<=l;e++)t===o.startRow&&e===o.startColumn||a.set(w(t,e),`hidden`)}return a}function E(e,t){if(!e)return``;let r=[];if(e.bl&&r.push(`font-weight:bold`),e.it&&r.push(`font-style:italic`),e.ul?.s&&r.push(`text-decoration:underline`),e.st?.s){let e=r.findIndex(e=>e.startsWith(`text-decoration:`));e>=0?r[e]=`text-decoration:underline line-through`:r.push(`text-decoration:line-through`)}e.fs&&d(e.fs)&&r.push(`font-size:${e.fs}pt`),e.ff&&r.push(`font-family:${M(e.ff)}`),e.bg?.rgb&&r.push(`background-color:${N(e.bg.rgb)}`);let i=F(e,t)??e.cl?.rgb;if(i&&r.push(`color:${N(i)}`),e.ht!=null){let t=D(e.ht);t&&r.push(`text-align:${t}`)}if(e.vt!=null){let t=O(e.vt);t&&r.push(`vertical-align:${t}`)}return e.tb===n.WRAP&&(r.push(`white-space:normal`),r.push(`overflow-wrap:break-word`)),e.bd&&(k(r,`border-top`,e.bd.t),k(r,`border-right`,e.bd.r),k(r,`border-bottom`,e.bd.b),k(r,`border-left`,e.bd.l)),r.join(`;`)}function D(e){switch(e){case c.LEFT:return`left`;case c.CENTER:return`center`;case c.RIGHT:return`right`;default:return null}}function O(e){switch(e){case o.TOP:return`top`;case o.MIDDLE:return`middle`;case o.BOTTOM:return`bottom`;default:return null}}function k(e,t,n){if(!n||n.s===u.NONE)return;let r=A(n.s),i=N(n.cl?.rgb??`#000`),a=j(n.s);e.push(`${t}:${r} ${a} ${i}`)}function A(e){switch(e){case u.MEDIUM:case u.MEDIUM_DASHED:case u.MEDIUM_DASH_DOT:case u.MEDIUM_DASH_DOT_DOT:case u.SLANT_DASH_DOT:return`2px`;case u.THICK:case u.DOUBLE:return`3px`;default:return`1px`}}function j(e){switch(e){case u.DOTTED:return`dotted`;case u.DASHED:case u.DASH_DOT:case u.DASH_DOT_DOT:case u.MEDIUM_DASHED:case u.MEDIUM_DASH_DOT:case u.MEDIUM_DASH_DOT_DOT:case u.SLANT_DASH_DOT:return`dashed`;case u.DOUBLE:return`double`;default:return`solid`}}function M(e){return e.replace(/[;<>{}\\/()'"]/g,``)}function N(e){let t=e.trim();return/^#[0-9a-fA-F]{3,8}$/.test(t)||/^[a-zA-Z]{1,30}$/.test(t)||/^(?:rgb|hsl)a?\([0-9.,\s%]+\)$/.test(t)?t:`transparent`}function P(e,n){if(!e||e.v==null)return``;if(typeof e.v==`boolean`)return e.v?`TRUE`:`FALSE`;let r=n?.n?.pattern;if(r&&d(e.v))try{return I(t(r,e.v))}catch{}return I(String(e.v))}function F(t,n){let r=t?.n?.pattern;if(!r||!n||!d(n.v))return null;try{let t=e(r,n.v);return typeof t==`string`?t:null}catch{return null}}function I(e){return e.replace(/&/g,`&amp;`).replace(/</g,`&lt;`).replace(/>/g,`&gt;`).replace(/"/g,`&quot;`).replace(/'/g,`&#39;`)}export{f as renderSpreadsheetToHtml};