@objectstack/console 9.1.0 → 9.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assets/{AiPendingActionsPage-B9iWZUDQ.js → AiPendingActionsPage-B-n-gqzM.js} +1 -1
- package/dist/assets/{ApiConsolePage-ZBL7GLt2.js → ApiConsolePage-uPTuvhXX.js} +1 -1
- package/dist/assets/{AppManagementPage-A8AufLcC.js → AppManagementPage-CNpBOuFm.js} +1 -1
- package/dist/assets/ApprovalsInboxPage-BFNNZBXy.js +1 -0
- package/dist/assets/{AuditLogPage-DWQl9my4.js → AuditLogPage-_9ECzJM8.js} +1 -1
- package/dist/assets/{ComponentNavView-CqfUzDMU.js → ComponentNavView-CDdExuCL.js} +1 -1
- package/dist/assets/ConsoleFloatingChatbot-DThmuzxB.js +1 -0
- package/dist/assets/{DeveloperHubPage-D7pt27Ji.js → DeveloperHubPage-mdknfgGp.js} +1 -1
- package/dist/assets/FlowRunsPage-yleJQxJv.js +1 -0
- package/dist/assets/{IntegrationsPage-CHg7ApBe.js → IntegrationsPage-7SUsLOvq.js} +2 -2
- package/dist/assets/ProfilePage-D4yjFTMG.js +1 -0
- package/dist/assets/PublicFormsPage-DWj4wR8t.js +1 -0
- package/dist/assets/{SettingsHub-DerOpof3.js → SettingsHub-7kLSZWnI.js} +1 -1
- package/dist/assets/{SettingsView-BNq6smI8.js → SettingsView-DqnY47y1.js} +1 -1
- package/dist/assets/{SystemHubPage-UTl8x-Bf.js → SystemHubPage-DeiksJan.js} +1 -1
- package/dist/assets/assistantBus-Bppk-Y9t.js +1 -0
- package/dist/assets/data-adapter-D1EJuDUI.js +1 -0
- package/dist/assets/framework-CHiz03zG.js +34 -0
- package/dist/assets/highlighted-body-OFNGDK62-Du4wVUV7.js +1 -0
- package/dist/assets/index-B_NAVTba.js +32 -0
- package/dist/assets/index-DwG7_t-6.css +2 -0
- package/dist/assets/mergeServerFields-CNavewPY.js +7 -0
- package/dist/assets/mermaid-GHXKKRXX-D0s_nNwO.js +1 -0
- package/dist/assets/plugin-calendar-u9Aa9OAR.js +1 -0
- package/dist/assets/{plugin-charts-BjUzf65B.js → plugin-charts-DSBNwB9E.js} +2 -2
- package/dist/assets/{plugin-chatbot-DUla2_T9.js → plugin-chatbot-BzJE6C-F.js} +6 -6
- package/dist/assets/plugin-dashboard-C5nSNZfG.js +5 -0
- package/dist/assets/{plugin-form-CcC4fdyJ.js → plugin-form-DrL6xDfP.js} +1 -1
- package/dist/assets/plugin-gantt-aDdnMW0u.js +1 -0
- package/dist/assets/plugin-grid-CSHgWd0O.js +6 -0
- package/dist/assets/plugin-kanban-D45w069C.js +1 -0
- package/dist/assets/{plugin-map-Bt30Mxn6.js → plugin-map-ue6Mjnhb.js} +1 -1
- package/dist/assets/{plugin-markdown-tayYSGF3.js → plugin-markdown-6MvgfANj.js} +1 -1
- package/dist/assets/plugin-report-QmyiWpfC.js +59 -0
- package/dist/assets/plugin-timeline-DrUpbBdn.js +1 -0
- package/dist/assets/plugin-view-DS-dSLs-.js +1 -0
- package/dist/assets/plugins-views-DH1gGZh6.js +4 -0
- package/dist/assets/{src-DOKKicsm.js → src-s-NNbVef.js} +1 -1
- package/dist/assets/{types-TLjbGkho.js → types-Decs33Tv.js} +1 -1
- package/dist/assets/{ui-components-DeXgqLMc.js → ui-components-BbPoD3rS.js} +2 -2
- package/dist/assets/{ui-layout-CvIgXgRF.js → ui-layout-CWeej0a8.js} +1 -1
- package/dist/assets/vendor-objectstack-RB3xvhcR.js +102 -0
- package/dist/index.html +4 -4
- package/package.json +1 -1
- package/dist/assets/ApprovalsInboxPage-Be6DrS7X.js +0 -1
- package/dist/assets/ConsoleFloatingChatbot-ByiTKdo6.js +0 -1
- package/dist/assets/FlowRunsPage-R-JzHY35.js +0 -1
- package/dist/assets/ProfilePage-D7kxyCKj.js +0 -1
- package/dist/assets/PublicFormsPage-B1VZJXuJ.js +0 -1
- package/dist/assets/data-adapter-DlR5JXpU.js +0 -1
- package/dist/assets/framework-DoGxClbR.js +0 -34
- package/dist/assets/highlighted-body-OFNGDK62-Bu5WidAv.js +0 -1
- package/dist/assets/index-Bt0jFx__.js +0 -32
- package/dist/assets/index-CJppnf0b.css +0 -2
- package/dist/assets/mergeServerFields-DL5isEen.js +0 -7
- package/dist/assets/mermaid-GHXKKRXX-BAcku4Xs.js +0 -1
- package/dist/assets/plugin-calendar-CUnlH4Wo.js +0 -1
- package/dist/assets/plugin-dashboard-DbPGiGy-.js +0 -5
- package/dist/assets/plugin-gantt-BRef4N3J.js +0 -1
- package/dist/assets/plugin-grid-BiWgglXK.js +0 -6
- package/dist/assets/plugin-kanban-C1UyFHza.js +0 -1
- package/dist/assets/plugin-report-qca5M0OZ.js +0 -59
- package/dist/assets/plugin-timeline-DZoFKL82.js +0 -1
- package/dist/assets/plugin-view-Ei4BPIYk.js +0 -1
- package/dist/assets/plugins-views-qoGCOQ47.js +0 -4
- package/dist/assets/vendor-objectstack-B2q1QmZh.js +0 -102
|
@@ -1 +1 @@
|
|
|
1
|
-
import{o as e}from"./rolldown-runtime-CMxvf4Kt.js";import{_ as t,t as n}from"./vendor-react-DR370ovB.js";import{n as r}from"./infrastructure-CxityHZF.js";import{B as i,G as a,H as o,g as s,mt as c}from"./framework-DoGxClbR.js";import{At as l,Bn as u,Da as d,Ea as f,Fn as p,Ft as m,Gt as h,It as g,Jn as _,Kn as v,Kt as y,Ln as b,Mt as x,Nt as S,Pt as C,Qs as w,Xn as T,Yn as E,as as D,ds as O,et as k,fs as A,h as j,jt as M,k as N,mr as P,ms as F,n as I,nt as L,o as R,p as z,pi as B,ps as V,qn as H,qt as U,rt as W,t as G,tc as K,tt as ee,zn as te}from"./ui-components-DeXgqLMc.js";import{r as ne}from"./vendor-ui-utils-rQOI1gAo.js";var q=e(t(),1),J=n(),Y=({label:e,description:t,collapsible:n=!1,collapsed:r=!1,columns:i=1,children:a,className:o,gridClassName:s,showBorder:c})=>{let[l,u]=(0,q.useState)(r),d={1:`grid-cols-1`,2:`grid-cols-1 md:grid-cols-2`,3:`grid-cols-1 md:grid-cols-2 lg:grid-cols-3`,4:`grid-cols-1 md:grid-cols-2 lg:grid-cols-4`},f=()=>{n&&u(!l)},p=c??!!e,m=e||t?(0,J.jsxs)(`div`,{className:w(`flex items-start gap-2`,!p&&`mb-4`,n&&`cursor-pointer select-none`),onClick:f,role:n?`button`:void 0,"aria-expanded":n?!l:void 0,children:[n&&(0,J.jsx)(`span`,{className:`mt-0.5 text-muted-foreground`,children:l?(0,J.jsx)(O,{className:`h-4 w-4`}):(0,J.jsx)(V,{className:`h-4 w-4`})}),(0,J.jsxs)(`div`,{className:`flex-1`,children:[e&&(0,J.jsx)(`h3`,{className:`text-base font-semibold text-foreground`,children:e}),t&&(0,J.jsx)(`p`,{className:`text-sm text-muted-foreground mt-0.5`,children:t})]})]}):null,h=l?null:(0,J.jsx)(`div`,{className:w(`grid gap-4`,s||d[i]),children:a});return p?(0,J.jsxs)(v,{className:w(`form-section`,o),children:[m&&(0,J.jsxs)(E,{className:`pb-3`,children:[e&&(0,J.jsx)(T,{className:`text-base`,children:e}),t&&(0,J.jsx)(_,{children:t}),n&&(0,J.jsx)(`button`,{type:`button`,className:`absolute right-4 top-4 text-muted-foreground`,onClick:f,"aria-expanded":!l,"aria-label":l?`Expand section`:`Collapse section`,children:l?(0,J.jsx)(O,{className:`h-4 w-4`}):(0,J.jsx)(V,{className:`h-4 w-4`})})]}),h&&(0,J.jsx)(H,{children:h})]}):(0,J.jsxs)(`div`,{className:w(`form-section`,o),children:[m,h]})},re=({schema:e,dataSource:t,className:n})=>{let{fieldLabel:r}=K(),[a,o]=(0,q.useState)(null),[s,c]=(0,q.useState)({}),[l,u]=(0,q.useState)(!0),[d,f]=(0,q.useState)(null),[p,m]=(0,q.useState)(e.defaultTab||e.sections[0]?.name||e.sections[0]?.label||`tab-0`);q.useEffect(()=>{(async()=>{if(!t){u(!1);return}try{o(await t.getObjectSchema(e.objectName))}catch(e){f(e)}})()},[e.objectName,t]),q.useEffect(()=>{(a||!t)&&(async()=>{if(e.mode===`create`||!e.recordId||!t){c(e.initialData||e.initialValues||{}),u(!1);return}try{c(await t.findOne(e.objectName,e.recordId)||{})}catch(e){f(e)}finally{u(!1)}})()},[a,e.mode,e.recordId,e.initialData,e.initialValues,t,e.objectName]);let h=(0,q.useCallback)(t=>{let n=[];for(let i of t.fields){let t=typeof i==`string`?i:i.name;if(typeof i==`object`)n.push(i);else if(a?.fields?.[t]){let i=a.fields[t];n.push({name:t,label:r(e.objectName,t,i.label||t),type:z(i.type),required:i.required||!1,disabled:e.readOnly||e.mode===`view`||i.readonly,placeholder:i.placeholder,description:i.help||i.description,validation:G(i),field:i,options:i.options,multiple:i.multiple})}else n.push({name:t,label:t,type:`input`})}return n},[a,e.readOnly,e.mode]),g=(0,q.useCallback)(async n=>{if(!t)return e.onSuccess&&await e.onSuccess(n),n;try{let r;return e.mode===`create`?r=await t.create(e.objectName,n):e.mode===`edit`&&e.recordId&&(r=await t.update(e.objectName,e.recordId,n)),e.onSuccess&&await e.onSuccess(r),r}catch(t){throw e.onError&&e.onError(t),t}},[e,t]),_=(0,q.useCallback)(()=>{e.onCancel&&e.onCancel()},[e]),v=(e,t)=>e.name||e.label||`tab-${t}`;if(d)return(0,J.jsxs)(`div`,{className:`p-4 border border-red-300 bg-red-50 rounded-md`,children:[(0,J.jsx)(`h3`,{className:`text-red-800 font-semibold`,children:`Error loading form`}),(0,J.jsx)(`p`,{className:`text-red-600 text-sm mt-1`,children:d.message})]});if(l)return(0,J.jsxs)(`div`,{className:`p-8 text-center`,children:[(0,J.jsx)(`div`,{className:`inline-block animate-spin rounded-full h-8 w-8 border-b-2 border-gray-900`}),(0,J.jsx)(`p`,{className:`mt-2 text-sm text-gray-600`,children:`Loading form...`})]});let y={type:`form`,fields:e.sections.flatMap(e=>h(e)),layout:`vertical`,defaultValues:s,submitLabel:e.submitText||(e.mode===`create`?`Create`:`Update`),cancelLabel:e.cancelText,showSubmit:e.showSubmit!==!1&&e.mode!==`view`,showCancel:e.showCancel!==!1,onSubmit:g,onCancel:_},b=e.tabPosition===`left`||e.tabPosition===`right`;return(0,J.jsx)(`div`,{className:w(`w-full`,n,e.className),children:(0,J.jsxs)(k,{value:p,onValueChange:m,orientation:b?`vertical`:`horizontal`,className:w(b&&`flex gap-4`),children:[(0,J.jsx)(L,{className:w(b?`flex-col h-auto`:``,e.tabPosition===`bottom`&&`order-last`,e.tabPosition===`right`&&`order-last`),children:e.sections.map((e,t)=>(0,J.jsx)(W,{value:v(e,t),className:b?`w-full justify-start`:``,children:e.label||`Tab ${t+1}`},v(e,t)))}),(0,J.jsx)(`div`,{className:`flex-1`,children:e.sections.map((t,n)=>(0,J.jsx)(ee,{value:v(t,n),className:`mt-0`,children:(0,J.jsx)(Y,{description:t.description,columns:t.columns||1,children:(0,J.jsx)(i,{schema:{...y,fields:h(t),showSubmit:e.showSubmit!==!1&&e.mode!==`view`,showCancel:e.showCancel!==!1}})})},v(t,n)))})]})})},ie=({schema:e,dataSource:t,className:n})=>{let{fieldLabel:r}=K(),[a,o]=(0,q.useState)(null),[s,c]=(0,q.useState)({}),[l,u]=(0,q.useState)(!0),[f,p]=(0,q.useState)(null),[m,h]=(0,q.useState)(0),[g,_]=(0,q.useState)(new Set),[v,y]=(0,q.useState)(!1),b=e.sections.length,x=m===0,S=m===b-1;q.useEffect(()=>{(async()=>{if(!t){u(!1);return}try{o(await t.getObjectSchema(e.objectName))}catch(e){p(e)}})()},[e.objectName,t]),q.useEffect(()=>{(a||!t)&&(async()=>{if(e.mode===`create`||!e.recordId||!t){c(e.initialData||e.initialValues||{}),u(!1);return}try{c(await t.findOne(e.objectName,e.recordId)||{})}catch(e){p(e)}finally{u(!1)}})()},[a,e.mode,e.recordId,e.initialData,e.initialValues,t,e.objectName]);let C=(0,q.useCallback)(t=>{let n=[],i=(e,t)=>typeof t==`string`&&t.trim()?{...e,visible:e=>I(t,e)}:e;for(let o of t.fields){let t=typeof o==`string`?o:o.name;if(typeof o==`object`)n.push(i(o,o.visibleOn));else if(a?.fields?.[t]){let o=a.fields[t];n.push(i({name:t,label:r(e.objectName,t,o.label||t),type:z(o.type),required:o.required||!1,disabled:e.readOnly||e.mode===`view`||o.readonly,placeholder:o.placeholder,description:o.help||o.description,validation:G(o),field:o,options:o.options,multiple:o.multiple},o.visible_on??o.visibleOn))}else n.push({name:t,label:t,type:`input`})}return n},[a,e.readOnly,e.mode,e.objectName]),T=(0,q.useMemo)(()=>m>=0&&m<b?C(e.sections[m]):[],[m,b,e.sections,C]),E=(0,q.useCallback)(async n=>{let r={...s,...n};if(c(r),_(e=>new Set(e).add(m)),S){y(!0);try{if(!t)return e.onSuccess&&await e.onSuccess(r),r;let n;return e.mode===`create`?n=await t.create(e.objectName,r):e.mode===`edit`&&e.recordId&&(n=await t.update(e.objectName,e.recordId,r)),e.onSuccess&&await e.onSuccess(n),n}catch(t){throw e.onError&&e.onError(t),t}finally{y(!1)}}else D(m+1)},[s,m,S,e,t]),D=(0,q.useCallback)(t=>{t>=0&&t<b&&(h(t),e.onStepChange&&e.onStepChange(t))},[b,e]),k=(0,q.useCallback)(()=>{D(m-1)},[m,D]),j=(0,q.useCallback)(()=>{e.onCancel&&e.onCancel()},[e]),M=(0,q.useCallback)(t=>{(e.allowSkip||g.has(t)||t<=m)&&D(t)},[e.allowSkip,g,m,D]);if(f)return(0,J.jsxs)(`div`,{className:`p-4 border border-red-300 bg-red-50 rounded-md`,children:[(0,J.jsx)(`h3`,{className:`text-red-800 font-semibold`,children:`Error loading form`}),(0,J.jsx)(`p`,{className:`text-red-600 text-sm mt-1`,children:f.message})]});if(l)return(0,J.jsxs)(`div`,{className:`p-8 text-center`,children:[(0,J.jsx)(`div`,{className:`inline-block animate-spin rounded-full h-8 w-8 border-b-2 border-gray-900`}),(0,J.jsx)(`p`,{className:`mt-2 text-sm text-gray-600`,children:`Loading form...`})]});let N=e.sections[m];return(0,J.jsxs)(`div`,{className:w(`w-full`,n,e.className),children:[e.showStepIndicator!==!1&&(0,J.jsx)(`nav`,{"aria-label":`Progress`,className:`mb-8`,children:(0,J.jsx)(`ol`,{className:`flex items-center`,children:e.sections.map((t,n)=>{let r=n===m,i=g.has(n),a=e.allowSkip||i||n<=m;return(0,J.jsxs)(`li`,{className:w(`relative flex-1`,n!==b-1&&`pr-8 sm:pr-12`),children:[n!==b-1&&(0,J.jsx)(`div`,{className:`absolute top-3 sm:top-4 left-6 -right-4 sm:left-10 sm:-right-2 h-0.5`,"aria-hidden":`true`,children:(0,J.jsx)(`div`,{className:w(`h-full`,i?`bg-primary`:`bg-muted`)})}),(0,J.jsxs)(`button`,{type:`button`,className:w(`group relative flex items-center`,a?`cursor-pointer`:`cursor-not-allowed`),onClick:()=>M(n),disabled:!a,children:[(0,J.jsx)(`span`,{className:w(`flex h-6 w-6 sm:h-8 sm:w-8 items-center justify-center rounded-full text-xs sm:text-sm font-medium transition-colors`,i&&`bg-primary text-primary-foreground`,r&&!i&&`border-2 border-primary bg-background text-primary`,!r&&!i&&`border-2 border-muted bg-background text-muted-foreground`),children:i?(0,J.jsx)(F,{className:`h-3 w-3 sm:h-4 sm:w-4`}):n+1}),(0,J.jsx)(`span`,{className:`ml-2 sm:ml-3 text-xs sm:text-sm font-medium hidden sm:block`,children:(0,J.jsx)(`span`,{className:w(r?`text-foreground`:`text-muted-foreground`),children:t.label||`Step ${n+1}`})})]})]},n)})})}),(0,J.jsx)(`div`,{className:`min-h-[200px]`,children:N&&(0,J.jsx)(Y,{label:N.label,description:N.description,columns:N.columns||1,children:T.length>0?(0,J.jsx)(i,{schema:{type:`form`,fields:T,layout:`vertical`,defaultValues:s,showSubmit:!1,showCancel:!1,onSubmit:E}}):(0,J.jsx)(`div`,{className:`text-center py-8 text-muted-foreground`,children:`No fields configured for this step`})})}),(0,J.jsxs)(`div`,{className:`flex items-center justify-between mt-6 pt-4 border-t`,children:[(0,J.jsx)(`div`,{children:e.showCancel!==!1&&(0,J.jsx)(P,{variant:`ghost`,onClick:j,children:e.cancelText||`Cancel`})}),(0,J.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,J.jsxs)(`span`,{className:`text-sm text-muted-foreground mr-2`,children:[`Step `,m+1,` of `,b]}),!x&&(0,J.jsxs)(P,{variant:`outline`,onClick:k,children:[(0,J.jsx)(A,{className:`h-4 w-4 mr-1`}),e.prevText||`Back`]}),S?(0,J.jsxs)(P,{onClick:()=>E(s),disabled:v||e.mode===`view`,children:[v&&(0,J.jsx)(d,{className:`mr-2 h-4 w-4 animate-spin`,"aria-hidden":!0}),v?`Submitting...`:e.submitText||(e.mode===`create`?`Create`:`Update`)]}):(0,J.jsxs)(P,{onClick:()=>E(s),children:[e.nextText||`Next`,(0,J.jsx)(O,{className:`h-4 w-4 ml-1`})]})]})]})]})},ae=({schema:e,dataSource:t,className:n})=>{let{fieldLabel:r}=K(),[a,o]=(0,q.useState)(null),[s,c]=(0,q.useState)({}),[l,u]=(0,q.useState)(!0),[d,f]=(0,q.useState)(null);(0,q.useEffect)(()=>{(async()=>{if(!t){u(!1);return}try{o(await t.getObjectSchema(e.objectName))}catch(e){f(e),u(!1)}})()},[e.objectName,t]),(0,q.useEffect)(()=>{(a||!t)&&(async()=>{if(e.mode===`create`||!e.recordId){c(e.initialData||e.initialValues||{}),u(!1);return}if(!t){c(e.initialData||e.initialValues||{}),u(!1);return}try{c(await t.findOne(e.objectName,e.recordId)||{})}catch(e){f(e)}finally{u(!1)}})()},[a,e.mode,e.recordId,e.initialData,e.initialValues,t,e.objectName]);let p=(0,q.useCallback)(t=>{let n=[],i=(e,t)=>typeof t==`string`&&t.trim()?{...e,visible:e=>I(t,e)}:e;for(let o of t.fields){let t=typeof o==`string`?o:o.name;if(typeof o==`object`)n.push(i(o,o.visibleOn));else if(a?.fields?.[t]){let o=a.fields[t];n.push(i({name:t,label:r(e.objectName,t,o.label||t),type:z(o.type),required:o.required||!1,disabled:e.readOnly||e.mode===`view`||o.readonly,placeholder:o.placeholder,description:o.help||o.description,validation:G(o),field:o,options:o.options,multiple:o.multiple},o.visible_on??o.visibleOn))}else n.push({name:t,label:t,type:`input`})}return n},[a,e.readOnly,e.mode,e.objectName]),m=(0,q.useCallback)(async n=>{if(!t)return e.onSuccess&&await e.onSuccess(n),n;try{let r;return e.mode===`create`?r=await t.create(e.objectName,n):e.mode===`edit`&&e.recordId&&(r=await t.update(e.objectName,e.recordId,n)),e.onSuccess&&await e.onSuccess(r),r}catch(t){throw e.onError&&e.onError(t),t}},[e,t]),g=(0,q.useCallback)(()=>{e.onCancel&&e.onCancel()},[e]),_=(0,q.useMemo)(()=>e.sections.slice(0,1),[e.sections]),v=(0,q.useMemo)(()=>e.sections.slice(1),[e.sections]);(0,q.useMemo)(()=>e.sections.flatMap(e=>p(e)),[e.sections,p]);let b=e.splitDirection||`horizontal`,x=e.splitSize||50;if(d)return(0,J.jsxs)(`div`,{className:`p-4 border border-red-300 bg-red-50 rounded-md`,children:[(0,J.jsx)(`h3`,{className:`text-red-800 font-semibold`,children:`Error loading form`}),(0,J.jsx)(`p`,{className:`text-red-600 text-sm mt-1`,children:d.message})]});if(l)return(0,J.jsxs)(`div`,{className:`p-8 text-center`,children:[(0,J.jsx)(`div`,{className:`inline-block animate-spin rounded-full h-8 w-8 border-b-2 border-gray-900`}),(0,J.jsx)(`p`,{className:`mt-2 text-sm text-gray-600`,children:`Loading form...`})]});let S={type:`form`,layout:`vertical`,defaultValues:s,onSubmit:m,onCancel:g},C=(t,n)=>(0,J.jsx)(`div`,{className:`space-y-4 p-4`,children:t.map((t,r)=>(0,J.jsx)(Y,{label:t.label,description:t.description,columns:t.columns||1,children:(0,J.jsx)(i,{schema:{...S,fields:p(t),showSubmit:n&&e.showSubmit!==!1&&e.mode!==`view`,showCancel:n&&e.showCancel!==!1,submitLabel:e.submitText||(e.mode===`create`?`Create`:`Update`),cancelLabel:e.cancelText}})},t.name||t.label||r))});return(0,J.jsx)(`div`,{className:w(`w-full`,n,e.className),children:(0,J.jsxs)(U,{orientation:b,className:`min-h-[300px] rounded-lg border`,children:[(0,J.jsx)(y,{defaultSize:x,minSize:20,children:C(_,v.length===0)}),v.length>0&&(0,J.jsxs)(J.Fragment,{children:[(0,J.jsx)(h,{withHandle:e.splitResizable!==!1}),(0,J.jsx)(y,{defaultSize:100-x,minSize:20,children:C(v,!0)})]})]})})},X=e=>e==null?void 0:e.id??e._id??e.recordId,oe=new Set([`id`,`_id`,`recordId`]);function se(e,t){return e?Object.entries(e).every(([e,n])=>e===t||oe.has(e)?!0:n==null||n===``):!0}function ce(e,t,n){let r=[{object:e,action:`create`,data:t}];for(let e of n)for(let t of e.rows)se(t,e.relationshipField)||r.push({object:e.childObject,action:`create`,data:{...t,[e.relationshipField]:{$ref:0}}});return r}function le(e,t,n,r){let i=[{object:e,action:`update`,id:t,data:n}];for(let e of r){let n=(e.rows||[]).map(n=>({...n,[e.relationshipField]:t})),{toCreate:r,toUpdate:a,toDelete:o}=ue(e.original||[],n);for(let t of r){if(se(t,e.relationshipField))continue;let{id:n,_id:r,recordId:a,...o}=t;i.push({object:e.childObject,action:`create`,data:o})}for(let t of a)i.push({object:e.childObject,action:`update`,id:X(t),data:t});for(let t of o)i.push({object:e.childObject,action:`delete`,id:t})}return i}function ue(e,t){let n=new Set(t.map(X).filter(Boolean));return{toCreate:t.filter(e=>!X(e)),toUpdate:t.filter(e=>X(e)),toDelete:(e||[]).map(X).filter(e=>!!e&&!n.has(e))}}function de(e,t){return Array.isArray(e)?e.reduce((e,n)=>{let r=Number(n?.[t]);return e+(Number.isFinite(r)?r:0)},0):0}function fe(e){return Array.isArray(e)?e:e&&Array.isArray(e.records)?e.records:e&&Array.isArray(e.data)?e.data:e&&(e.id||e._id)?[e]:[]}async function pe(e,t,n){return n.length===0?[]:typeof e.bulk==`function`?fe(await e.bulk(t,`create`,n)):fe(await Promise.all(n.map(n=>e.create(t,n))))}async function me(e,t,n,r){let i=[],a=r.rows.filter(e=>X(e)||!se(e,r.relationshipField)).map(e=>({...e,[r.relationshipField]:n}));if(r.original!==void 0){let{toCreate:t,toUpdate:n,toDelete:o}=ue(r.original,a),s=await pe(e,r.childObject,t);for(let e of s){let t=X(e);t&&i.push({object:r.childObject,id:t})}await Promise.all(n.map(t=>e.update(r.childObject,X(t),t))),await Promise.all(o.map(t=>e.delete(r.childObject,t)))}else{let t=await pe(e,r.childObject,a);for(let e of t){let t=X(e);t&&i.push({object:r.childObject,id:t})}}if(r.totalField){let i=de(r.rows,r.amountField||`amount`);await e.update(t,n,{[r.totalField]:i})}return{created:i}}var he=new Set([`id`,`_id`,`recordId`,`created_at`,`updated_at`,`created_by`,`updated_by`,`createdAt`,`updatedAt`,`createdBy`,`updatedBy`,`organization_id`,`tenant_id`,`space`,`owner`]),ge=new Set([`position`,`sort_order`,`sequence`,`line_no`,`line_number`,`sort`]),_e=new Set([`formula`,`summary`,`rollup`,`autonumber`,`auto_number`,`file`,`image`,`avatar`,`json`,`object`,`grid`,`table`,`location`,`vector`,`html`,`markdown`,`richtext`]);function ve(e){switch(e){case`number`:case`percent`:case`rating`:case`slider`:return`number`;case`currency`:return`currency`;case`date`:case`datetime`:case`time`:return`date`;case`select`:case`picklist`:case`radio`:case`boolean`:case`toggle`:return`select`;case`lookup`:case`master_detail`:return`lookup`;default:return`text`}}function ye(e){if(e?.type===`boolean`||e?.type===`toggle`)return[{label:`Yes`,value:`true`},{label:`No`,value:`false`}];let t=e?.options;if(Array.isArray(t))return t.map(e=>typeof e==`object`&&e?{label:String(e.label??e.value),value:String(e.value)}:{label:String(e),value:String(e)})}function be(e,t){let n=e?.fields;if(!n||typeof n!=`object`)return;let r;for(let[e,i]of Object.entries(n)){let n=i;if(n?.reference===t){if(n?.type===`master_detail`)return e;n?.type===`lookup`&&!r&&(r=e)}}return r}var xe=[`name`,`title`,`subject`,`label`,`full_name`,`display_name`,`code`],Se={select:0,currency:1,number:1,lookup:2,date:3,text:4};function Ce(e,t){if(t<=0||e.length<=t)return e;let n=new Set,r=e.find(e=>xe.includes(e.field))??e[0];r&&n.add(r.field);for(let t of e)t.required&&n.add(t.field);let i=e.map((e,t)=>({c:e,i:t})).filter(({c:e})=>!n.has(e.field)).sort((e,t)=>(Se[e.c.type]??5)-(Se[t.c.type]??5)||e.i-t.i);for(let{c:e}of i){if(n.size>=t)break;n.add(e.field)}return e.map(e=>n.has(e.field)?e:{...e,defaultHidden:!0})}function we(e,t={}){let n=e?.fields;if(!n||typeof n!=`object`)return[];let r=new Set([...t.exclude??[],...t.relationshipField?[t.relationshipField]:[]]),i=[];for(let[e,t]of Object.entries(n)){let n=t;if(he.has(e)||r.has(e)||ge.has(e)||n?.system||n?.readonly||n?.hidden||_e.has(n?.type))continue;let a={field:e,label:n?.label||e,type:ve(n?.type),required:!!n?.required},o=ye(n);a.type===`select`&&o&&(a.options=o),a.type===`lookup`&&(a.reference=n?.reference,a.displayField=n?.display_field||n?.reference_field),n?.readonlyWhen&&(a.readonlyWhen=n.readonlyWhen),(n?.requiredWhen??n?.conditionalRequired)&&(a.requiredWhen=n.requiredWhen??n.conditionalRequired);let s=typeof n?.expression==`string`?n.expression:n?.expression?.source;s&&typeof s==`string`&&(a.computed=!0,a.expr=s,a.required=!1,typeof n?.scale==`number`&&(a.scale=n.scale)),i.push(a)}return Ce(i,t.maxColumns??6)}var Te=new Set([`formula`,`summary`,`rollup`,`autonumber`,`auto_number`]);function Ee(e,t={}){let n=e?.fields;if(!n||typeof n!=`object`)return[];let r=new Set([...t.exclude??[],...t.relationshipField?[t.relationshipField]:[]]),i=[];for(let[e,t]of Object.entries(n)){let n=t;he.has(e)||r.has(e)||ge.has(e)||n?.system||n?.hidden||Te.has(n?.type)||i.push(e)}return i}var De=new Set([`textarea`,`richtext`,`html`,`markdown`,`rich-text`,`file`,`image`,`avatar`,`attachment`,`json`,`location`,`address`]);function Oe(e,t,n={}){if(t===`grid`||t===`form`)return t;let r=e?.fields??{},i=Ee(e,{relationshipField:n.relationshipField});return i.some(e=>De.has(r[e]?.type))||i.length>8?`form`:`grid`}var ke=[`amount`,`total`,`subtotal`,`line_total`,`line_amount`,`net_amount`];function Ae(e){let t=e.filter(e=>e.type===`number`||e.type===`currency`);if(t.length===0)return;let n=t.find(e=>e.computed);if(n)return n.field;let r=t.find(e=>ke.includes(e.field));if(r)return r.field;let i=[...t].reverse().find(e=>e.type===`currency`);return i?i.field:t[t.length-1].field}function je(e,t,n,r={}){let i=r.relationshipField||be(t,n);if(!i)throw Error(`MasterDetailForm: could not find a lookup/master_detail field on "${e}" referencing "${n}". Set relationshipField explicitly.`);let a=r.columns?.length?r.columns:we(t,{relationshipField:i}),o=r.amountField||Ae(a);return{childObject:e,relationshipField:i,columns:a,formFields:Ee(t,{relationshipField:i}),mode:Oe(t,r.inlineEdit??(t?.fields)?.[i]?.inlineEdit,{relationshipField:i}),amountField:o,sortField:Object.keys(t?.fields??{}).find(e=>ge.has(e))}}function Me(e){if(!e)return{};let t={};return e.querySelectorAll(`[name]`).forEach(e=>{let n=e.getAttribute(`name`);if(n){if(e.tagName===`INPUT`){let r=e;if(r.type===`checkbox`){t[n]=r.checked;return}if(r.type===`radio`){r.checked&&(t[n]=r.value);return}if(r.type===`number`||r.type===`range`){if(r.value===``){t[n]=null;return}let e=Number(r.value);t[n]=Number.isFinite(e)?e:r.value;return}t[n]=r.value;return}t[n]=e.value}}),t}var Ne=({details:e,state:t,setRows:n,formHostRef:r,taxRateField:i,formKey:a,onRowExpand:o,onAddViaForm:s})=>{let[c,l]=(0,q.useState)({}),u=(0,q.useRef)(``);(0,q.useEffect)(()=>{let e=r.current;if(!e)return;let t=()=>{let t=Me(e),n;try{n=JSON.stringify(t)}catch{n=String(Math.random())}n!==u.current&&(u.current=n,l(t))};t();let n=()=>t();e.addEventListener(`input`,n),e.addEventListener(`change`,n);let i=[120,360,800].map(e=>setTimeout(t,e));return()=>{e.removeEventListener(`input`,n),e.removeEventListener(`change`,n),i.forEach(clearTimeout)}},[r,a,e.length]);let d=c[i],f=d===void 0?null:Number.isFinite(Number(d))?Number(d):0,p=e.reduce((e,n,r)=>e+de(t[r]?.rows??[],n.amountField||`amount`),0),m=f!==null&&e.some(e=>!!e.amountField),h=f??0,g=h/100*p,_=p+g,v=e=>`¥${e.toLocaleString(void 0,{minimumFractionDigits:2,maximumFractionDigits:2})}`;return(0,J.jsxs)(J.Fragment,{children:[e.map((e,r)=>(0,J.jsxs)(`section`,{className:`space-y-2`,children:[(0,J.jsx)(`h3`,{className:`text-sm font-medium text-foreground`,children:e.title||`Line Items`}),e.columns?.length?(0,J.jsx)(j,{value:t[r]?.rows??[],onChange:e=>n(r,e),contextRecord:c,...e.inlineMode===`form`||(e.formFields?.length??0)>(e.columns?.length??0)?{onRowExpand:e=>o(r,e)}:{},displayMode:e.inlineMode===`form`?`list`:`grid`,...e.inlineMode===`form`?{onAdd:()=>s(r)}:{},field:{columns:e.columns,total_field:m?void 0:e.amountField||(e.totalField?`amount`:void 0),sort_field:e.sortField,min_rows:e.minRows,max_rows:e.maxRows,add_label:e.inlineMode===`form`?e.addLabel||`Add`:e.addLabel}}):(0,J.jsx)(`p`,{className:`py-4 text-sm text-muted-foreground`,children:`Loading columns…`})]},`${e.childObject}-${r}`)),m&&(0,J.jsx)(`div`,{className:`flex justify-end`,children:(0,J.jsxs)(`dl`,{className:`w-64 space-y-1.5 text-sm`,"data-testid":`md-totals`,children:[(0,J.jsxs)(`div`,{className:`flex items-center justify-between`,children:[(0,J.jsx)(`dt`,{className:`text-muted-foreground`,children:`Subtotal`}),(0,J.jsx)(`dd`,{className:`tabular-nums`,"data-testid":`md-subtotal`,children:v(p)})]}),(0,J.jsxs)(`div`,{className:`flex items-center justify-between`,children:[(0,J.jsxs)(`dt`,{className:`text-muted-foreground`,children:[`Tax (`,h,`%)`]}),(0,J.jsx)(`dd`,{className:`tabular-nums`,"data-testid":`md-tax`,children:v(g)})]}),(0,J.jsxs)(`div`,{className:`flex items-center justify-between border-t border-border pt-1.5 text-base font-semibold`,children:[(0,J.jsx)(`dt`,{children:`Total`}),(0,J.jsx)(`dd`,{className:`tabular-nums`,"data-testid":`md-grand-total`,children:v(_)})]})]})})]})},Z=({schema:e,dataSource:t,className:n})=>{let r=e.details||[],i=e.mode===`edit`&&!!e.recordId,a=r.some(e=>!e.relationshipField||!e.columns?.length),[o,s]=(0,q.useState)(a?null:r),c=o??r;(0,q.useEffect)(()=>{if(!a){s(r);return}if(!t||typeof t.getObjectSchema!=`function`)return;let n=!1;return(async()=>{let i=await Promise.all(r.map(async n=>{if(n.relationshipField&&n.columns?.length)return n;try{let r=await t.getObjectSchema(n.childObject),i=je(n.childObject,r,e.objectName,{relationshipField:n.relationshipField,columns:n.columns,amountField:n.amountField});return{...n,relationshipField:i.relationshipField,columns:i.columns,formFields:n.formFields??i.formFields,inlineMode:n.inlineMode??i.mode,amountField:n.amountField??i.amountField,sortField:n.sortField??i.sortField}}catch{return n}}));n||s(i)})(),()=>{n=!0}},[t,e.objectName,e.details]);let[l,u]=(0,q.useState)(()=>r.map(()=>({rows:[],original:[]}))),d=(0,q.useRef)(l);d.current=l;let[f,p]=(0,q.useState)(0),[m,h]=(0,q.useState)(!1),g=(0,q.useRef)(!1),_=(0,q.useRef)(null),y=(0,q.useCallback)(()=>{g.current=!1,h(!1),_.current&&=(clearTimeout(_.current),null)},[]);(0,q.useEffect)(()=>{let n=!1;if(!(!i||!t))return(async()=>{let r=await Promise.all(c.map(async n=>{if(!n.relationshipField)return{rows:[],original:[]};try{let r=(await t.find(n.childObject,{$filter:{[n.relationshipField]:e.recordId},$top:500}))?.data??[];return{rows:r.map(e=>({...e})),original:r.map(e=>({...e}))}}catch{return{rows:[],original:[]}}}));n||u(r)})(),()=>{n=!0}},[i,t,e.recordId,o]);let b=(0,q.useCallback)((e,t)=>{u(n=>n.map((n,r)=>r===e?{...n,rows:t}:n))},[]),x=e.taxRateField||`tax_rate`,[S,C]=(0,q.useState)(null),D=S?l[S.detailIdx]?.rows?.[S.rowIdx]:void 0,O=S?c[S.detailIdx]:void 0,k=(0,q.useCallback)((e,t,n)=>{u(r=>r.map((r,i)=>i===e?{...r,rows:r.rows.map((e,r)=>r===t?{...e,...n}:e)}:r))},[]),A=(0,q.useCallback)(e=>{u(t=>{let n=t.map((t,n)=>n===e?{...t,rows:[...t.rows,{}]}:t);return C({detailIdx:e,rowIdx:n[e].rows.length-1,isNew:!0}),n})},[]),j=(0,q.useCallback)(()=>{C(e=>(e?.isNew&&u(t=>t.map((t,n)=>n===e.detailIdx?{...t,rows:t.rows.filter((t,n)=>n!==e.rowIdx)}:t)),null))},[]),M=(0,q.useCallback)(async t=>{y(),ne.success(i?e.title?`${e.title} saved`:`Saved`:`Created`),i||(u(c.map(()=>({rows:[],original:[]}))),p(e=>e+1)),await e.onSuccess?.(t)},[i,e.onSuccess,e.title,c.length,y]),N=(0,q.useCallback)(t=>{y(),ne.error(t?.message||`Save failed`),e.onError?.(t)},[e,y]),F=(0,q.useCallback)(async n=>{let r=[];for(let a=0;a<c.length;a++){let o=c[a],{rows:s,original:l}=d.current[a];if(!o.relationshipField)continue;let{created:u}=await me(t,e.objectName,n,{childObject:o.childObject,relationshipField:o.relationshipField,rows:s,original:i?l:void 0,amountField:o.amountField,totalField:o.totalField});r.push(...u)}return r},[c,i,t,e.objectName]),I=(0,q.useCallback)(async n=>{let r=X(n)??e.recordId;if(!r)throw Error(`MasterDetailForm: parent record has no id after save`);if(!t)throw Error(`MasterDetailForm: dataSource is required`);let a=[];try{a=await F(r)}catch(n){throw i||await Promise.allSettled([...a.map(e=>t.delete(e.object,e.id)),t.delete(e.objectName,r)]),n}await M(n)},[t,i,F,e,M]),L=typeof t?.batchTransaction==`function`,R=(0,q.useCallback)(async n=>{let r=t,a={...n};c.forEach((e,t)=>{e.totalField&&(a[e.totalField]=de(d.current[t]?.rows??[],e.amountField||`amount`))});let o=i?le(e.objectName,e.recordId,a,c.filter(e=>e.relationshipField).map((e,t)=>({childObject:e.childObject,relationshipField:e.relationshipField,rows:d.current[t]?.rows??[],original:d.current[t]?.original??[]}))):ce(e.objectName,a,c.filter(e=>e.relationshipField).map((e,t)=>({childObject:e.childObject,relationshipField:e.relationshipField,rows:d.current[t]?.rows??[]})));return(await r.batchTransaction(o))?.results?.[0]??{...a,id:e.recordId}},[t,c,e.objectName,e.recordId,i]),z=(0,q.useMemo)(()=>({type:`object-form`,objectName:e.objectName,mode:e.mode??`create`,recordId:e.recordId,formType:e.formType,sections:e.sections,fields:e.fields,title:e.title,showSubmit:!1,showCancel:!1,...L?{submitHandler:R,onSuccess:M}:{onSuccess:I},onError:N}),[e,I,L,R,M,N]),B=(0,q.useRef)(null),V=e.submitText??(i?`Save`:`Create`),U=(0,q.useCallback)(()=>{g.current||B.current?.querySelector(`form`)&&(g.current=!0,h(!0),setTimeout(()=>{(B.current?.querySelector(`form`))?.requestSubmit()},0),_.current=setTimeout(()=>y(),1500))},[y]);return(0,q.useEffect)(()=>()=>{_.current&&clearTimeout(_.current)},[]),(0,J.jsxs)(`div`,{className:w(`space-y-6`,n,e.className),children:[(0,J.jsx)(`div`,{ref:B,children:(0,J.jsx)($,{schema:z,dataSource:t},f)}),(0,J.jsx)(Ne,{details:c,state:l,setRows:b,formHostRef:B,taxRateField:x,formKey:f,onRowExpand:(e,t)=>C({detailIdx:e,rowIdx:t}),onAddViaForm:A}),S&&O&&(0,J.jsxs)(v,{className:`border-primary/40 shadow-none ring-1 ring-primary/10`,"data-testid":`md-row-form`,children:[(0,J.jsxs)(E,{className:`pb-2 flex flex-row items-center justify-between gap-2 space-y-0`,children:[(0,J.jsxs)(T,{className:`text-sm font-medium`,children:[O.title||`Line item`,` — row `,S.rowIdx+1]}),(0,J.jsx)(P,{type:`button`,variant:`ghost`,size:`sm`,className:`h-7 text-xs text-muted-foreground`,onClick:j,children:`Close`})]}),(0,J.jsx)(H,{children:(0,J.jsx)($,{schema:{type:`object-form`,objectName:O.childObject,mode:`edit`,initialData:D??{},...O.formFields?.length?{fields:O.formFields}:{},submitText:`Apply`,submitHandler:async e=>e,onSuccess:e=>{k(S.detailIdx,S.rowIdx,e),C(null)},onCancel:j},dataSource:t},`row-${S.detailIdx}-${S.rowIdx}`)})]}),(0,J.jsxs)(`div`,{className:`flex items-center justify-end gap-2 border-t border-border pt-4`,children:[e.onCancel&&(0,J.jsx)(P,{type:`button`,variant:`outline`,onClick:e.onCancel,disabled:m,"data-testid":`md-form-cancel`,children:`Cancel`}),(0,J.jsx)(P,{type:`button`,onClick:U,disabled:m||a&&!o,"data-testid":`md-form-submit`,children:m?`Saving…`:V})]})]})},Pe=new Set([`field:formula`,`field:summary`,`field:auto_number`,`field:autonumber`]),Fe=new Set([`field:textarea`,`field:markdown`,`field:html`,`field:grid`,`field:rich-text`,`textarea`,`markdown`,`html`,`grid`,`rich-text`]),Ie=new Set([`formula`,`summary`,`auto_number`,`autonumber`]);function Le(e){return Fe.has(e)}function Re(e){return Ie.has(e)}function ze(e){return e<=3?1:2}function Be(e,t){return t<=1?e:e.map(e=>e.colSpan===void 0&&e.type&&Le(e.type)?{...e,colSpan:t}:e)}var Ve=new Set([`id`,`created_at`,`createdAt`,`updated_at`,`updatedAt`,`last_modified_at`,`lastModifiedAt`,`deleted_at`,`deletedAt`,`created_by`,`createdBy`,`updated_by`,`updatedBy`,`last_modified_by`,`lastModifiedBy`,`organization_id`,`organizationId`,`org_id`,`orgId`,`tenant_id`,`tenantId`,`owner`,`owner_id`,`_version`,`_rev`]);function He(e,t){return e.filter(e=>!(Ve.has(e.name)||t?.fields?.[e.name]?.readonly===!0))}function Ue(e,t){return e.filter(e=>{if(e.type&&Pe.has(e.type))return!1;let n=t?.fields?.[e.name];return n?!Re(n.type):!0})}function We(e){return e<=1?`default`:e===2?`xl`:`full`}function Q(e,t,n,r){let i=[...e];if(i=He(i,t),r===`create`&&(i=Ue(i,t)),n!==void 0)return i=Be(i,n),{fields:i,columns:n};let a=ze(i.length);return i=Be(i,a),{fields:i,columns:a}}var Ge=new Set([`id`,`_id`,`created_at`,`updated_at`,`createdAt`,`updatedAt`,`organization_id`,`organizationId`]),Ke=new Set([`formula`,`rollup`,`lookup_value`,`auto_number`,`autonumber`,`computed`]);function qe(e,t){if(!e||typeof e!=`object`)return e;let n={},r=t?.fields;for(let[t,i]of Object.entries(e))if(!Ge.has(t)){if(r){let e=r[t];if(!e)continue;let n=String(e.type||``).toLowerCase();if(Ke.has(n)||e.computed===!0||e.formula||e.readOnly===!0||e.readonly===!0)continue}n[t]=i}return n}var Je={1:void 0,2:`grid gap-4 grid-cols-1 @md:grid-cols-2`,3:`grid gap-4 grid-cols-1 @md:grid-cols-2 @2xl:grid-cols-3`,4:`grid gap-4 grid-cols-1 @md:grid-cols-2 @2xl:grid-cols-3 @4xl:grid-cols-4`},Ye=({schema:e,dataSource:t,className:n})=>{let{fieldLabel:r}=K(),[a,o]=(0,q.useState)(null),[s,c]=(0,q.useState)([]),[l,u]=(0,q.useState)({}),[d,f]=(0,q.useState)(!0),[p,h]=(0,q.useState)(null),_=e.open!==!1,v=e.drawerSide||`right`,[y,b]=(0,q.useState)(()=>{let t={};return e.sections?.forEach((e,n)=>{let r=e.name||String(n);e.collapsed&&(t[r]=!0)}),t});(0,q.useEffect)(()=>{(async()=>{if(!t){f(!1);return}try{o(await t.getObjectSchema(e.objectName))}catch(e){h(e),f(!1)}})()},[e.objectName,t]),(0,q.useEffect)(()=>{(a||!t)&&(async()=>{if(e.mode===`create`||!e.recordId){u(e.initialData||e.initialValues||{}),f(!1);return}if(!t){u(e.initialData||e.initialValues||{}),f(!1);return}try{u(await t.findOne(e.objectName,e.recordId)||{})}catch(e){h(e)}finally{f(!1)}})()},[a,e.mode,e.recordId,e.initialData,e.initialValues,t,e.objectName]);let T=(0,q.useCallback)(t=>{let n=[];for(let i of t.fields){let t=typeof i==`string`?i:i.name;if(typeof i==`object`)n.push(i);else if(a?.fields?.[t]){let i=a.fields[t];n.push({name:t,label:r(e.objectName,t,i.label||t),type:z(i.type),required:i.required||!1,disabled:e.readOnly||e.mode===`view`||i.readonly,placeholder:i.placeholder,description:i.help||i.description,validation:G(i),field:i,options:i.options,multiple:i.multiple})}else n.push({name:t,label:t,type:`input`})}return n},[a,e.readOnly,e.mode]);(0,q.useEffect)(()=>{if(!a&&t)return;if(e.customFields?.length){c(e.customFields),f(!1);return}if(e.sections?.length){f(!1);return}if(!a)return;let n=e.fields||Object.keys(a.fields||{}),i=[];for(let t of n){let n=typeof t==`string`?t:t.name;if(!n)continue;let o=a.fields?.[n];o&&i.push({name:n,label:r(e.objectName,n,o.label||n),type:z(o.type),required:o.required||!1,disabled:e.readOnly||e.mode===`view`||o.readonly,placeholder:o.placeholder,description:o.help||o.description,validation:G(o),field:o,options:o.options,multiple:o.multiple})}c(i),f(!1)},[a,e.fields,e.customFields,e.sections,e.readOnly,e.mode,t]);let E=(0,q.useCallback)(async n=>{if(!t)return e.onSuccess&&await e.onSuccess(n),e.onOpenChange?.(!1),n;try{let r,i=qe(n,a);return e.mode===`create`?r=await t.create(e.objectName,i):e.mode===`edit`&&e.recordId&&(r=await t.update(e.objectName,e.recordId,i)),e.onSuccess&&await e.onSuccess(r),e.onOpenChange?.(!1),r}catch(t){throw e.onError&&e.onError(t),t}},[e,t,a]),D=(0,q.useCallback)(()=>{e.onCancel&&e.onCancel(),e.onOpenChange?.(!1)},[e]),O=(0,q.useMemo)(()=>{if(e.drawerWidth)return v===`left`||v===`right`?{width:e.drawerWidth,maxWidth:e.drawerWidth}:{height:e.drawerWidth,maxHeight:e.drawerWidth}},[e.drawerWidth,v]),k={type:`form`,layout:e.layout===`vertical`||e.layout===`horizontal`?e.layout:`vertical`,defaultValues:l,submitLabel:e.submitText||(e.mode===`create`?`Create`:`Update`),cancelLabel:e.cancelText,showSubmit:e.showSubmit!==!1&&e.mode!==`view`,showCancel:e.showCancel!==!1,onSubmit:E,onCancel:D},A=()=>{if(p)return(0,J.jsxs)(`div`,{className:`p-4 border border-red-300 bg-red-50 rounded-md`,children:[(0,J.jsx)(`h3`,{className:`text-red-800 font-semibold`,children:`Error loading form`}),(0,J.jsx)(`p`,{className:`text-red-600 text-sm mt-1`,children:p.message})]});if(d)return(0,J.jsxs)(`div`,{className:`p-8 text-center`,children:[(0,J.jsx)(`div`,{className:`inline-block animate-spin rounded-full h-8 w-8 border-b-2 border-gray-900`}),(0,J.jsx)(`p`,{className:`mt-2 text-sm text-gray-600`,children:`Loading form...`})]});if(e.sections?.length){let t=[];return e.sections.forEach((e,n)=>{let r=e.name||String(n),i=y[r]??e.collapsed??!1;t.push({name:`__section_${r}`,label:e.label||``,type:`section-divider`,colSpan:4,collapsible:e.collapsible,collapsed:i,onToggle:e.collapsible?()=>b(e=>({...e,[r]:!i})):void 0});let a=T(e);i?t.push(...a.map(e=>({...e,hidden:!0}))):t.push(...a)}),(0,J.jsx)(i,{schema:{...k,fields:t}})}let t=Q(s,a,e.columns,e.mode),n=Je[t.columns||1];return(0,J.jsx)(i,{schema:{...k,fields:t.fields,columns:t.columns,...n?{fieldContainerClass:n}:{}}})},j=e.subforms,M=j?.length&&e.mode!==`view`?(0,J.jsx)(Z,{schema:{type:`object-master-detail-form`,objectName:e.objectName,mode:e.mode===`edit`?`edit`:`create`,recordId:e.recordId,fields:e.fields,sections:e.sections,submitText:e.submitText,details:j,onSuccess:async t=>{await e.onSuccess?.(t),e.onOpenChange?.(!1)},onError:e.onError,onCancel:()=>e.onOpenChange?.(!1)},dataSource:t}):A();return(0,J.jsx)(x,{open:_,onOpenChange:e.onOpenChange,children:(0,J.jsxs)(S,{side:v,className:w(`overflow-y-auto`,n,e.className),style:O,children:[(e.title||e.description)&&(0,J.jsxs)(m,{children:[e.title&&(0,J.jsx)(g,{children:e.title}),e.description?(0,J.jsx)(C,{children:e.description}):(0,J.jsx)(C,{className:`sr-only`,children:`Complete the form fields, then submit or cancel.`})]}),(0,J.jsx)(`div`,{className:`@container py-4`,children:M})]})})},Xe={sm:`sm:max-w-sm`,default:`sm:max-w-lg`,lg:`sm:max-w-2xl`,xl:`sm:max-w-5xl`,full:`sm:max-w-[95vw] sm:w-full`},Ze={1:void 0,2:`grid gap-4 grid-cols-1 @md:grid-cols-2`,3:`grid gap-4 grid-cols-1 @md:grid-cols-2 @2xl:grid-cols-3`,4:`grid gap-4 grid-cols-1 @md:grid-cols-2 @2xl:grid-cols-3 @4xl:grid-cols-4`},Qe=({schema:e,dataSource:t,className:n})=>{let{fieldLabel:a}=K(),o=r(),s=(0,q.useCallback)(t=>{if(!o?.isLoaded)return t;let n=[];for(let r of t){if(!r?.name){n.push(r);continue}o.checkField(e.objectName,r.name,`read`)&&(!o.checkField(e.objectName,r.name,`write`)&&e.mode!==`view`?n.push({...r,readOnly:!0,disabled:!0}):n.push(r))}return n},[o,e.objectName,e.mode]),[c,f]=(0,q.useState)(null),[m,h]=(0,q.useState)([]),[g,_]=(0,q.useState)({}),[v,y]=(0,q.useState)(!0),[x,S]=(0,q.useState)(null),[C,T]=(0,q.useState)(!1),E=e.open!==!1,D=(0,q.useId)(),O=(0,q.useMemo)(()=>e.sections?.length||e.customFields?.length?null:Q(m,c,e.columns,e.mode),[m,c,e.columns,e.mode,e.sections,e.customFields]),k=Xe[(0,q.useMemo)(()=>{if(e.modalSize)return e.modalSize;if(O?.columns&&O.columns>1)return We(O.columns);if(e.sections?.length){let t=Math.max(...e.sections.map(e=>Number(e.columns)||1));if(t>1)return We(t)}return`default`},[e.modalSize,O,e.sections])]||Xe.default;(0,q.useEffect)(()=>{(async()=>{if(!t){y(!1);return}try{f(await t.getObjectSchema(e.objectName))}catch(e){S(e),y(!1)}})()},[e.objectName,t]),(0,q.useEffect)(()=>{(c||!t)&&(async()=>{if(e.mode===`create`||!e.recordId){_(e.initialData||e.initialValues||{}),y(!1);return}if(!t){_(e.initialData||e.initialValues||{}),y(!1);return}try{_(await t.findOne(e.objectName,e.recordId)||{})}catch(e){S(e)}finally{y(!1)}})()},[c,e.mode,e.recordId,e.initialData,e.initialValues,t,e.objectName]);let A=(0,q.useCallback)(t=>{let n=[],r=(e,t)=>typeof t==`string`&&t.trim()?{...e,visible:e=>I(t,e)}:e;for(let i of t.fields){let t=typeof i==`string`?i:i.name;if(typeof i==`object`)n.push(r(i,i.visibleOn));else if(c?.fields?.[t]){let i=c.fields[t];n.push(r({name:t,label:a(e.objectName,t,i.label||t),type:z(i.type),required:i.required||!1,disabled:e.readOnly||e.mode===`view`||i.readonly,placeholder:i.placeholder,description:i.help||i.description,validation:G(i),field:i,options:i.options,multiple:i.multiple},i.visible_on??i.visibleOn))}else n.push({name:t,label:t,type:`input`})}return n},[c,e.readOnly,e.mode,e.objectName]);(0,q.useEffect)(()=>{if(!c&&t)return;if(e.customFields?.length){h(e.customFields),y(!1);return}if(e.sections?.length){y(!1);return}if(!c)return;let n=e.fields||Object.keys(c.fields||{}),r=[];for(let t of n){let n=typeof t==`string`?t:t.name;if(!n)continue;let i=c.fields?.[n];i&&r.push({name:n,label:a(e.objectName,n,i.label||n),type:z(i.type),required:i.required||!1,disabled:e.readOnly||e.mode===`view`||i.readonly,placeholder:i.placeholder,description:i.help||i.description,validation:G(i),field:i,options:i.options,multiple:i.multiple})}h(r),y(!1)},[c,e.fields,e.customFields,e.sections,e.readOnly,e.mode,t]);let j=(0,q.useCallback)(async n=>{T(!0);try{if(!t)return e.onSuccess&&await e.onSuccess(n),e.onOpenChange?.(!1),n;let r,i=qe(n,c);if(o?.isLoaded){let t={};for(let n of Object.keys(i))o.checkField(e.objectName,n,`write`)&&(t[n]=i[n]);i=t}return e.mode===`create`?r=await t.create(e.objectName,i):e.mode===`edit`&&e.recordId&&(r=await t.update(e.objectName,e.recordId,i)),e.onSuccess&&await e.onSuccess(r),e.onOpenChange?.(!1),r}catch(t){throw e.onError&&e.onError(t),t}finally{T(!1)}},[e,t,c,o]),M=(0,q.useCallback)(()=>{e.onCancel&&e.onCancel(),e.onOpenChange?.(!1)},[e]),F=e.layout===`vertical`||e.layout===`horizontal`?e.layout:`vertical`,L=e.showSubmit!==!1&&e.mode!==`view`,R=e.showCancel!==!1,B=e.submitText||(e.mode===`create`?`Create`:`Update`),V=e.cancelText||`Cancel`,H={type:`form`,layout:F,defaultValues:g,submitLabel:B,cancelLabel:V,showSubmit:L,showCancel:R,onSubmit:j,onCancel:M,showActions:!1,id:D},U=()=>{if(x)return(0,J.jsxs)(`div`,{className:`p-4 border border-red-300 bg-red-50 rounded-md`,children:[(0,J.jsx)(`h3`,{className:`text-red-800 font-semibold`,children:`Error loading form`}),(0,J.jsx)(`p`,{className:`text-red-600 text-sm mt-1`,children:x.message})]});if(v)return(0,J.jsx)(`div`,{className:`space-y-4`,"data-testid":`modal-form-skeleton`,children:[1,2,3].map(e=>(0,J.jsxs)(`div`,{className:`space-y-2`,children:[(0,J.jsx)(l,{className:`h-4 w-24`}),(0,J.jsx)(l,{className:`h-10 w-full`})]},e))});if(e.sections?.length)return(0,J.jsx)(`div`,{className:`space-y-6`,children:e.sections.map((e,t)=>{let n=e.columns||1;return(0,J.jsx)(Y,{label:e.label,description:e.description,columns:n,gridClassName:Ze[n],children:(0,J.jsx)(i,{schema:{...H,fields:s(A(e))}})},e.name||e.label||t)})});let t=O??Q(m,c,e.columns,e.mode),n=Ze[t.columns||1];return(0,J.jsx)(i,{schema:{...H,fields:s(t.fields),columns:t.columns,...n?{fieldContainerClass:n}:{}}})},W=e.subforms;if(W?.length&&e.mode!==`view`)return(0,J.jsx)(p,{open:E,onOpenChange:e.onOpenChange,children:(0,J.jsxs)(N,{className:w(k,`flex flex-col h-[100dvh] sm:h-auto sm:max-h-[90vh] overflow-hidden p-0`,n,e.className),children:[(e.title||e.description)&&(0,J.jsxs)(te,{className:`shrink-0 px-4 pt-4 sm:px-6 sm:pt-6 pb-2 border-b`,children:[e.title&&(0,J.jsx)(u,{children:e.title}),e.description?(0,J.jsx)(b,{children:e.description}):(0,J.jsx)(b,{className:`sr-only`,children:`Enter the record and its line items, then save.`})]}),(0,J.jsx)(`div`,{className:`@container flex-1 overflow-y-auto px-4 sm:px-6 py-4`,children:(0,J.jsx)(Z,{schema:{type:`object-master-detail-form`,objectName:e.objectName,mode:e.mode===`edit`?`edit`:`create`,recordId:e.recordId,fields:e.fields,sections:e.sections,submitText:B,details:W,onSuccess:async t=>{await e.onSuccess?.(t),e.onOpenChange?.(!1)},onError:e.onError,onCancel:()=>e.onOpenChange?.(!1)},dataSource:t})})]})});let ee=!v&&!x&&(L||R);return(0,J.jsx)(p,{open:E,onOpenChange:e.onOpenChange,children:(0,J.jsxs)(N,{className:w(k,`flex flex-col h-[100dvh] sm:h-auto sm:max-h-[90vh] overflow-hidden p-0`,n,e.className),children:[(e.title||e.description)&&(0,J.jsxs)(te,{className:`shrink-0 px-4 pt-4 sm:px-6 sm:pt-6 pb-2 border-b`,children:[e.title&&(0,J.jsx)(u,{children:e.title}),e.description?(0,J.jsx)(b,{children:e.description}):(0,J.jsx)(b,{className:`sr-only`,children:`Complete the form fields, then submit or cancel.`})]}),(0,J.jsx)(`div`,{className:`@container flex-1 overflow-y-auto px-4 sm:px-6 py-4`,children:U()}),ee&&(0,J.jsx)(`div`,{className:`shrink-0 border-t px-4 sm:px-6 py-3 bg-background`,"data-testid":`modal-form-footer`,children:(0,J.jsxs)(`div`,{className:`flex flex-col sm:flex-row gap-2 sm:justify-end`,children:[R&&(0,J.jsx)(P,{type:`button`,variant:`outline`,onClick:M,disabled:C,className:`w-full sm:w-auto`,children:V}),L&&(0,J.jsxs)(P,{type:`submit`,form:D,disabled:C,className:`w-full sm:w-auto`,children:[C&&(0,J.jsx)(d,{className:`mr-2 h-4 w-4 animate-spin`}),B]})]})})]})})},$=({schema:e,dataSource:t})=>{let n=r(),i=(0,q.useMemo)(()=>{if(!n?.isLoaded)return e;let t=t=>t?.name?n.checkField(e.objectName,t.name,`read`)?!n.checkField(e.objectName,t.name,`write`)&&e.mode!==`view`?{...t,readOnly:!0,disabled:!0}:t:null:t,r=e=>Array.isArray(e)?e.map(t).filter(Boolean):e;return{...e,fields:r(e.fields),sections:e.sections?.map(e=>({...e,fields:r(e.fields)}))}},[e,n]),{sectionLabel:a}=K(),o=e=>e?.name?a(i.objectName,e.name,e.label||e.name):e?.label;if(i.subforms?.length&&i.mode!==`view`&&i.formType!==`drawer`&&i.formType!==`modal`)return(0,J.jsx)(Z,{schema:{type:`object-master-detail-form`,objectName:i.objectName,mode:i.mode===`edit`?`edit`:`create`,recordId:i.recordId,formType:i.formType===`tabbed`?`tabbed`:`simple`,sections:i.sections,fields:i.fields,title:i.title,submitText:i.submitText,details:i.subforms,onSuccess:i.onSuccess,onError:i.onError,onCancel:i.onCancel,className:i.className},dataSource:t});if(i.formType===`tabbed`&&i.sections?.length)return(0,J.jsx)(re,{schema:{...i,formType:`tabbed`,sections:i.sections.map(e=>({name:e.name,label:o(e),description:e.description,columns:e.columns,fields:e.fields})),defaultTab:i.defaultTab,tabPosition:i.tabPosition},dataSource:t,className:i.className});if(i.formType===`wizard`&&i.sections?.length)return(0,J.jsx)(ie,{schema:{...i,formType:`wizard`,sections:i.sections.map(e=>({name:e.name,label:o(e),description:e.description,columns:e.columns,fields:e.fields})),allowSkip:i.allowSkip,showStepIndicator:i.showStepIndicator,nextText:i.nextText,prevText:i.prevText,onStepChange:i.onStepChange},dataSource:t,className:i.className});if(i.formType===`split`&&i.sections?.length)return(0,J.jsx)(ae,{schema:{...i,formType:`split`,sections:i.sections.map(e=>({name:e.name,label:o(e),description:e.description,columns:e.columns,fields:e.fields})),splitDirection:i.splitDirection,splitSize:i.splitSize,splitResizable:i.splitResizable},dataSource:t,className:i.className});if(i.formType===`drawer`){let{layout:e,...n}=i,r=i.layout===`vertical`||i.layout===`horizontal`?i.layout:void 0;return(0,J.jsx)(Ye,{schema:{...n,layout:r,formType:`drawer`,sections:i.sections?.map(e=>({name:e.name,label:o(e),description:e.description,columns:e.columns,fields:e.fields,collapsible:e.collapsible,collapsed:e.collapsed})),open:i.open,onOpenChange:i.onOpenChange,drawerSide:i.drawerSide,drawerWidth:i.drawerWidth},dataSource:t,className:i.className})}if(i.formType===`modal`){let{layout:e,...n}=i,r=i.layout===`vertical`||i.layout===`horizontal`?i.layout:void 0;return(0,J.jsx)(Qe,{schema:{...n,layout:r,formType:`modal`,sections:i.sections?.map(e=>({name:e.name,label:o(e),description:e.description,columns:e.columns,fields:e.fields})),open:i.open,onOpenChange:i.onOpenChange,modalSize:i.modalSize,modalCloseButton:i.modalCloseButton},dataSource:t,className:i.className})}return(0,J.jsx)($e,{schema:i,dataSource:t})},$e=({schema:e,dataSource:t})=>{let{fieldLabel:n,sectionLabel:a}=K(),o=M(),s=r(),c=(0,q.useCallback)(t=>{if(!s?.isLoaded)return t;let n=[];for(let r of t)s.checkField(e.objectName,r.name,`read`)&&(!s.checkField(e.objectName,r.name,`write`)&&e.mode!==`view`?n.push({...r,readOnly:!0,disabled:!0,description:r.description??`You do not have edit access to this field.`}):n.push(r));return n},[s,e.objectName,e.mode]),[l,u]=(0,q.useState)(null),[d,f]=(0,q.useState)([]),[p,m]=(0,q.useState)(null),[h,g]=(0,q.useState)(!0),[_,v]=(0,q.useState)(null),y=e.customFields&&e.customFields.length>0;(0,q.useEffect)(()=>{y&&(m(e.initialData||e.initialValues||{}),g(!1))},[y,e.initialData,e.initialValues]),(0,q.useEffect)(()=>{y?u({name:e.objectName,fields:{}}):e.objectName&&t?(async()=>{try{if(!t)throw Error(`DataSource is required when using ObjectQL schema fetching (inline fields not provided)`);let n=await t.getObjectSchema(e.objectName);if(!n)throw Error(`No schema found for object "${e.objectName}"`);u(n)}catch(e){v(e),g(!1)}})():y||g(!1)},[e.objectName,t,y]),(0,q.useEffect)(()=>{l&&!y&&(async()=>{if(!e.recordId||e.mode===`create`){m(e.initialData||e.initialValues||{}),g(!1);return}if(!y){if(!t){v(Error(`DataSource is required for fetching record data (inline data not provided)`)),g(!1);return}g(!0);try{m(await t.findOne(e.objectName,e.recordId))}catch(e){console.error(`Failed to fetch record:`,e),v(e)}finally{g(!1)}}})()},[e.objectName,e.recordId,e.mode,e.initialValues,e.initialData,t,l,y]);let b=(0,q.useCallback)(e=>{if(!e||typeof e.visible==`function`)return e;let t=e.visibleOn;return typeof t==`string`&&t.trim()?{...e,visible:e=>I(t,e)}:e},[]);(0,q.useEffect)(()=>{if(y&&e.customFields){f(e.customFields.map(b)),g(!1);return}if(!l)return;let r=[],i=e.fields||Object.keys(l.fields||{});(Array.isArray(i)?i:Object.keys(i)).forEach(t=>{let i=typeof t==`string`?t:t.name;if(!i)return;let a=l.fields?.[i];if(!a&&!y)return;let o=!a?.permissions||a?.permissions.write!==!1;if(e.mode!==`view`&&!o)return;let s=e.customFields?.find(e=>e.name===i);if(s)r.push(b(s));else if(a){let o={name:i,label:n(e.objectName,i,a.label||t),type:z(a.type),required:a.required||!1,disabled:e.readOnly||e.mode===`view`||a.readonly||l?.managedBy&&l.managedBy!==`platform`,placeholder:a.placeholder,description:a.help||a.description,validation:G(a),visibleWhen:a.visibleWhen,readonlyWhen:a.readonlyWhen,requiredWhen:a.requiredWhen??a.conditionalRequired,field:a};if((a.type===`select`||a.type===`lookup`||a.type===`master_detail`)&&(o.options=a.options||[],o.multiple=a.multiple),(a.type===`number`||a.type===`currency`||a.type===`percent`)&&(o.inputType=`number`,o.min=a.min,o.max=a.max,o.step=a.precision?10**-a.precision:void 0),a.type===`date`&&(o.inputType=`date`),a.type===`datetime`&&(o.inputType=`datetime-local`),(a.type===`text`||a.type===`textarea`||a.type===`markdown`||a.type===`html`)&&(o.maxLength=a.max_length,o.minLength=a.min_length),(a.type===`file`||a.type===`image`)&&(o.inputType=`file`,o.multiple=a.multiple,o.accept=a.accept?a.accept.join(`,`):void 0,a.max_size)){let e=`Max size: ${R(a.max_size)}`;o.description=o.description?`${o.description} (${e})`:e}a.type===`email`&&(o.inputType=`email`),a.type===`phone`&&(o.inputType=`tel`),a.type===`url`&&(o.inputType=`url`),a.type===`password`&&(o.inputType=`password`),a.type===`time`&&(o.inputType=`time`),(a.type===`formula`||a.type===`summary`||a.type===`auto_number`)&&(o.disabled=!0),a.visible_on&&(o.visible=e=>I(a.visible_on,e)),r.push(o)}}),f(r),!y&&e.recordId&&e.mode!==`create`&&t||g(!1)},[l,e.fields,e.customFields,e.readOnly,e.mode,y,e.recordId,t]);let x=(0,q.useCallback)(async(n,r)=>{if(n&&(n.nativeEvent||n._reactName===`onSubmit`)&&(console.warn(`ObjectForm: Received Event instead of data in handleSubmit! This suggests a Form renderer issue.`),r===void 0&&(r=n,n={})),y&&!t)return e.onSuccess&&await e.onSuccess(n),n;if(!t)throw Error(`DataSource is required for form submission (inline mode not configured)`);let i=n;if(s?.isLoaded&&n&&typeof n==`object`){i={};for(let t of Object.keys(n))s.checkField(e.objectName,t,`write`)&&(i[t]=n[t])}try{let n;if(e.submitHandler)n=await e.submitHandler(i);else if(e.mode===`create`)n=await t.create(e.objectName,i);else if(e.mode===`edit`&&e.recordId)n=await t.update(e.objectName,e.recordId,i);else throw Error(`Invalid form mode or missing record ID`);return e.onSuccess&&await e.onSuccess(n),n}catch(t){throw console.error(`Failed to submit form:`,t),e.onError&&e.onError(t),t}},[e,t,y,s]),S=(0,q.useCallback)(()=>{e.onCancel&&e.onCancel()},[e]),C={...q.useMemo(()=>{if(!l?.fields)return{};let e={};return Object.keys(l.fields).forEach(t=>{let n=l.fields[t];n.defaultValue!==void 0&&(e[t]=n.defaultValue)}),e},[l]),...p};if(_)return(0,J.jsxs)(`div`,{className:`p-3 sm:p-4 border border-red-300 bg-red-50 rounded-md`,children:[(0,J.jsx)(`h3`,{className:`text-red-800 font-semibold`,children:`Error loading form`}),(0,J.jsx)(`p`,{className:`text-red-600 text-sm mt-1`,children:_.message})]});if(h)return(0,J.jsxs)(`div`,{className:`p-4 sm:p-8 text-center`,children:[(0,J.jsx)(`div`,{className:`inline-block animate-spin rounded-full h-8 w-8 border-b-2 border-gray-900`}),(0,J.jsx)(`p`,{className:`mt-2 text-sm text-gray-600`,children:`Loading form...`})]});let w=e.layout===`vertical`||e.layout===`horizontal`?e.layout:`vertical`;if(e.sections?.length&&(!e.formType||e.formType===`simple`))return(0,J.jsx)(`div`,{className:`w-full space-y-6`,children:e.sections.map((t,n)=>{let r=t.fields.map(e=>typeof e==`string`?e:e.name),o=c(d.filter(e=>r.includes(e.name)));return(0,J.jsx)(Y,{label:t.name?a(e.objectName,t.name,t.label||t.name):t.label,description:t.description,collapsible:t.collapsible,collapsed:t.collapsed,columns:t.columns,showBorder:t.showBorder,children:(0,J.jsx)(i,{schema:{type:`form`,fields:o,layout:w,defaultValues:C,showSubmit:n===e.sections.length-1&&e.showSubmit!==!1&&e.mode!==`view`,showCancel:n===e.sections.length-1&&e.showCancel!==!1,submitLabel:e.submitText||(e.mode===`create`?`Create`:`Update`),cancelLabel:e.cancelText,onSubmit:x,onCancel:S}})},t.name||t.label||n)})});let T=e.sections?.length,E=c(d),D=T?{fields:E,columns:e.columns}:Q(E,l,e.columns,e.mode),O=e.mobile,k=O?.fullscreenLongText?D.fields.map(e=>{let t=e.type;return t===`textarea`||t===`field:textarea`||t===`string-multiline`||t===`field:markdown`||t===`field:html`?{...e,mobile_fullscreen:!0}:e}):D.fields,A=O?.stepper,j=O?.stepperMinFields??8,N=Math.max(1,O?.stepperFieldsPerStep??1);if(!e.formType&&!T&&k.length>=2&&(A===!0||A===`auto`&&o&&k.length>=j)){let n=k,r=[];for(let e=0;e<n.length;e+=N){let t=n.slice(e,e+N);r.push({name:`step-${Math.floor(e/N)+1}`,label:t[0]?.label||t[0]?.name||`Step ${Math.floor(e/N)+1}`,fields:t})}return(0,J.jsx)(ie,{schema:{...e,formType:`wizard`,sections:r,showStepIndicator:!0},dataSource:t,className:e.className})}let P={type:`form`,fields:k,layout:w,columns:D.columns,submitLabel:e.submitText||(e.mode===`create`?`Create`:`Update`),cancelLabel:e.cancelText,showSubmit:e.showSubmit!==!1&&e.mode!==`view`,showCancel:e.showCancel!==!1,resetOnSubmit:e.showReset,defaultValues:C,onSubmit:x,onCancel:S,className:e.className,mobileStickyActions:!!O?.stickyActions};return(0,J.jsx)(`div`,{className:O?.stickyActions?`w-full pb-20 md:pb-0`:`w-full`,"data-mobile-form":O?`true`:void 0,children:(0,J.jsx)(i,{schema:P})})},et={text:200,email:254,url:2048,phone:32,textarea:5e3,markdown:5e3,html:5e3},tt=`_company_website_2`,nt=1500;function rt(e,t=[]){try{let n=new URL(e,typeof window<`u`?window.location.href:`http://localhost`);return typeof window<`u`&&n.origin===window.location.origin?!0:n.protocol!==`https:`&&n.protocol!==`http:`?!1:t.some(e=>{if(e===n.host)return!0;if(e.startsWith(`*.`)){let t=e.slice(1);return n.host.endsWith(t)&&n.host.length>t.length}return!1})}catch{return!1}}function it(e){return e&&e.map(e=>{let t=et[String(e.type||``).toLowerCase()];return!t||(e.maxLength??e.max_length)?e:{...e,maxLength:t}})}var at=({config:e,dataSource:t,prefillParams:n,className:r})=>{let[i,a]=(0,q.useState)(!1),[o,s]=(0,q.useState)(!1),[c,l]=(0,q.useState)(null),[u,p]=(0,q.useState)(!(e.consent?.required??!!e.consent)),[m,h]=(0,q.useState)(null),g=(0,q.useRef)(null),_=(0,q.useRef)(Date.now());(0,q.useEffect)(()=>{i||(_.current=Date.now())},[i]);let v=e.honeypot===!1?null:e.honeypot||tt,y=e.minFillTime??nt,b=(0,q.useMemo)(()=>it(e.customFields),[e.customFields]),x=(0,q.useMemo)(()=>{if(!t||b&&b.length>0||!e.fields||e.fields.length===0)return;let n=async(e,t)=>t;return{...t,create:n,update:(e,t,n)=>Promise.resolve(n),delete:()=>Promise.reject(Error(`Not permitted on public form`))}},[t,b,e.fields]),S=(0,q.useMemo)(()=>{let t={};if(n)for(let[e,r]of Object.entries(n))t[e]=r;if(typeof window<`u`&&e.allowedPrefillFields?.length){let n=new Set(e.allowedPrefillFields);new URLSearchParams(window.location.search).forEach((e,r)=>{n.has(r)&&!(r in t)&&(t[r]=e)})}return Object.keys(t).length>0?t:void 0},[n,e.allowedPrefillFields]),C=(0,q.useCallback)(async n=>{if(e.consent?.required&&!u){h(e.texts?.consentRequired??`Please accept the privacy policy to continue.`);return}if(h(null),v&&g.current?.value){a(!0);return}if(y>0&&Date.now()-_.current<y){l(e.texts?.rateLimited??`Please take a moment to review your answers before submitting.`);return}let r={...n};v&&delete r[v],e.captchaToken&&(r._captcha=e.captchaToken),s(!0),l(null);try{t&&await t.create(e.objectName,r),a(!0);let n=e.thankYouPage?.redirectUrl;if(n)if(rt(n,e.allowedRedirectHosts)){let t=e.thankYouPage?.redirectDelay??3e3;setTimeout(()=>{window.location.href=n},t)}else console.warn(`[EmbeddableForm] Blocked unsafe redirect target:`,n),l(e.texts?.redirectBlocked??null)}catch(e){l(e instanceof Error?e.message:`Failed to submit form. Please try again.`)}finally{s(!1)}},[t,e,u,v,y]),w=(0,q.useCallback)(()=>{a(!1),l(null)},[]),T=(0,q.useMemo)(()=>{let t={};return e.branding?.backgroundColor&&(t.backgroundColor=e.branding.backgroundColor),t},[e.branding]);if(i){let t=e.thankYouPage,n=e.texts??{},i=Math.ceil((t?.redirectDelay??3e3)/1e3),a=(n.redirecting??`Redirecting in {{seconds}} seconds…`).replace(`{{seconds}}`,String(i));return(0,J.jsx)(`div`,{className:`min-h-screen flex items-center justify-center p-4 bg-gradient-to-b from-muted/40 via-background to-background ${r||``}`,style:T,children:(0,J.jsxs)(`div`,{className:`max-w-md w-full bg-card border rounded-xl shadow-sm p-8 text-center space-y-4`,children:[(0,J.jsx)(`div`,{className:`mx-auto inline-flex h-14 w-14 items-center justify-center rounded-full bg-emerald-50 text-emerald-600 dark:bg-emerald-500/10 dark:text-emerald-400`,children:(0,J.jsx)(D,{className:`h-8 w-8`,"aria-hidden":`true`})}),(0,J.jsx)(`h2`,{className:`text-xl font-semibold text-foreground`,children:t?.title||n.thankYouTitle||`Thank You!`}),(0,J.jsx)(`p`,{className:`text-sm text-muted-foreground`,children:t?.message||n.thankYouMessage||`Your submission has been received successfully.`}),e.allowMultiple&&(0,J.jsx)(P,{variant:`outline`,size:`sm`,onClick:w,className:`mt-2`,children:n.submitAnother??`Submit Another Response`}),t?.redirectUrl&&(0,J.jsx)(`p`,{className:`text-xs text-muted-foreground`,children:a})]})})}let E=e.texts??{},O=!!e.consent?.required,k=`${e.formId}-consent`;return(0,J.jsx)(`div`,{className:`min-h-screen flex items-center justify-center p-4 sm:p-6 bg-gradient-to-b from-muted/40 via-background to-background ${r||``}`,style:T,children:(0,J.jsxs)(`div`,{className:`max-w-2xl w-full bg-card border rounded-xl shadow-sm overflow-hidden`,children:[e.branding?.coverImage&&(0,J.jsx)(`div`,{className:`h-28 sm:h-32 w-full bg-cover bg-center`,style:{backgroundImage:`url(${JSON.stringify(e.branding.coverImage)})`},role:`presentation`}),(0,J.jsxs)(`div`,{className:`px-6 sm:px-8 pt-7 pb-5 border-b bg-muted/20`,style:e.branding?.primaryColor?{borderBottomColor:e.branding.primaryColor}:void 0,children:[e.branding?.logo&&(0,J.jsx)(`img`,{src:e.branding.logo,alt:``,className:`h-8 mb-4`}),e.title&&(0,J.jsx)(`h1`,{className:`text-2xl font-semibold tracking-tight text-foreground`,children:e.title}),e.description&&(0,J.jsx)(`p`,{className:`text-sm text-muted-foreground mt-2 leading-relaxed`,children:e.description}),(0,J.jsx)(`p`,{className:`text-[11px] text-muted-foreground/70 mt-3`,"aria-hidden":`true`,children:E.requiredHint??`* Required field`})]}),(0,J.jsxs)(`div`,{className:`relative px-6 sm:px-8 py-6`,children:[c&&(0,J.jsx)(`div`,{role:`alert`,"aria-live":`assertive`,className:`mb-4 p-3 bg-destructive/10 border border-destructive/30 rounded-md text-sm text-destructive`,children:c}),(0,J.jsx)($,{schema:{type:`object-form`,objectName:e.objectName,mode:`create`,fields:e.fields,customFields:b,initialData:S,onSuccess:C,submitText:o?E.submitting??`Submitting...`:E.submit??`Submit`},dataSource:x}),v&&(0,J.jsxs)(`div`,{"aria-hidden":`true`,className:`absolute left-[-10000px] top-auto h-px w-px overflow-hidden`,children:[(0,J.jsx)(`label`,{htmlFor:`${e.formId}-${v}`,children:`Do not fill this field`}),(0,J.jsx)(`input`,{ref:g,id:`${e.formId}-${v}`,type:`text`,name:v,tabIndex:-1,autoComplete:`off`,defaultValue:``})]}),e.consent&&(0,J.jsxs)(`div`,{className:`mt-4`,children:[(0,J.jsxs)(`label`,{className:`flex items-start gap-2 text-sm text-foreground cursor-pointer select-none`,children:[(0,J.jsx)(`input`,{type:`checkbox`,id:k,checked:u,onChange:e=>{p(e.target.checked),e.target.checked&&h(null)},"aria-required":O||void 0,"aria-invalid":m?`true`:void 0,"aria-describedby":m?`${k}-error`:void 0,className:`mt-0.5 h-4 w-4 rounded border-input accent-primary`}),(0,J.jsxs)(`span`,{className:`leading-relaxed`,children:[e.consent.label??E.consentLabelDefault??`I agree to the privacy policy.`,O&&(0,J.jsx)(`span`,{"aria-hidden":`true`,className:`text-destructive ml-0.5`,children:`*`}),e.consent.privacyUrl&&(0,J.jsxs)(J.Fragment,{children:[` `,(0,J.jsx)(`a`,{href:e.consent.privacyUrl,target:`_blank`,rel:`noopener noreferrer`,className:`underline text-primary hover:opacity-80`,children:E.consentLink??`Privacy policy`})]})]})]}),m&&(0,J.jsx)(`p`,{id:`${k}-error`,role:`alert`,className:`mt-1.5 text-xs text-destructive`,children:m})]}),o&&(0,J.jsxs)(`div`,{className:`mt-3 flex items-center gap-2 text-xs text-muted-foreground`,children:[(0,J.jsx)(d,{className:`h-3.5 w-3.5 animate-spin`,"aria-hidden":`true`}),(0,J.jsx)(`span`,{children:E.submitting??`Submitting…`})]})]}),(0,J.jsxs)(`div`,{className:`px-6 sm:px-8 py-4 border-t bg-muted/20 flex flex-col sm:flex-row items-center justify-between gap-2`,children:[(0,J.jsxs)(`p`,{className:`inline-flex items-center gap-1.5 text-xs text-muted-foreground`,children:[(0,J.jsx)(f,{className:`h-3 w-3`,"aria-hidden":`true`}),(0,J.jsx)(`span`,{children:E.secureNotice??`Your information is transmitted securely and only used to respond to your request.`})]}),(0,J.jsxs)(`div`,{className:`flex items-center gap-3 text-xs text-muted-foreground/80`,children:[e.privacyPolicyUrl&&(0,J.jsxs)(`a`,{href:e.privacyPolicyUrl,target:`_blank`,rel:`noopener noreferrer`,className:`inline-flex items-center gap-1 underline hover:text-foreground`,children:[(0,J.jsx)(B,{className:`h-3 w-3`,"aria-hidden":`true`}),E.consentLink??`Privacy policy`]}),(0,J.jsx)(`span`,{children:E.poweredBy??`Powered by ObjectStack`})]})]})]})})};function ot(e){return e<60?`${Math.round(e)}s`:e<3600?`${Math.floor(e/60)}m ${Math.round(e%60)}s`:`${Math.floor(e/3600)}h ${Math.floor(e%3600/60)}m`}var st=({formId:e,formTitle:t,metrics:n,className:r})=>{let i=(0,q.useMemo)(()=>{if(!n.abandonedSubmissions)return 100;let e=n.totalSubmissions+n.abandonedSubmissions;return e===0?0:Math.round(n.totalSubmissions/e*100)},[n.totalSubmissions,n.abandonedSubmissions]),a=(0,q.useMemo)(()=>n.dailySubmissions?.length?Math.max(...n.dailySubmissions.map(e=>e.count),1):1,[n.dailySubmissions]);return(0,J.jsxs)(`div`,{className:`space-y-4 ${r||``}`,children:[(0,J.jsxs)(`div`,{children:[(0,J.jsx)(`h2`,{className:`text-lg font-semibold text-foreground`,children:t||`Form Analytics`}),(0,J.jsxs)(`p`,{className:`text-sm text-muted-foreground`,children:[`Form ID: `,e]})]}),(0,J.jsxs)(`div`,{className:`grid grid-cols-1 sm:grid-cols-3 gap-4`,children:[(0,J.jsx)(v,{children:(0,J.jsxs)(E,{className:`pb-2`,children:[(0,J.jsx)(_,{children:`Total Submissions`}),(0,J.jsx)(T,{className:`text-2xl`,children:n.totalSubmissions.toLocaleString()})]})}),(0,J.jsxs)(v,{children:[(0,J.jsxs)(E,{className:`pb-2`,children:[(0,J.jsx)(_,{children:`Fill Rate`}),(0,J.jsxs)(T,{className:`text-2xl`,children:[i,`%`]})]}),(0,J.jsx)(H,{children:(0,J.jsx)(`div`,{className:`w-full bg-muted rounded-full h-2`,children:(0,J.jsx)(`div`,{className:`bg-primary rounded-full h-2 transition-all`,style:{width:`${i}%`}})})})]}),(0,J.jsx)(v,{children:(0,J.jsxs)(E,{className:`pb-2`,children:[(0,J.jsx)(_,{children:`Avg. Completion Time`}),(0,J.jsx)(T,{className:`text-2xl`,children:n.avgCompletionTime?ot(n.avgCompletionTime):`—`})]})})]}),n.dailySubmissions&&n.dailySubmissions.length>0&&(0,J.jsxs)(v,{children:[(0,J.jsx)(E,{children:(0,J.jsx)(T,{className:`text-sm font-medium`,children:`Daily Submissions`})}),(0,J.jsx)(H,{children:(0,J.jsx)(`div`,{className:`flex items-end gap-1 h-32`,children:n.dailySubmissions.map((e,t)=>(0,J.jsxs)(`div`,{className:`flex-1 flex flex-col items-center gap-1`,children:[(0,J.jsx)(`div`,{className:`w-full bg-primary/80 rounded-t transition-all hover:bg-primary`,style:{height:`${e.count/a*100}%`,minHeight:e.count>0?`4px`:`0`},title:`${e.date}: ${e.count} submissions`}),(0,J.jsx)(`span`,{className:`text-[10px] text-muted-foreground truncate w-full text-center`,children:e.date.slice(-5)})]},t))})})]}),n.fieldDropOff&&n.fieldDropOff.length>0&&(0,J.jsxs)(v,{children:[(0,J.jsxs)(E,{children:[(0,J.jsx)(T,{className:`text-sm font-medium`,children:`Field Completion Rates`}),(0,J.jsx)(_,{children:`Percentage of users who completed each field`})]}),(0,J.jsx)(H,{children:(0,J.jsx)(`div`,{className:`space-y-3`,children:n.fieldDropOff.map((e,t)=>(0,J.jsxs)(`div`,{className:`space-y-1`,children:[(0,J.jsxs)(`div`,{className:`flex justify-between text-sm`,children:[(0,J.jsx)(`span`,{className:`text-foreground`,children:e.label}),(0,J.jsxs)(`span`,{className:`text-muted-foreground`,children:[e.completionRate,`%`]})]}),(0,J.jsx)(`div`,{className:`w-full bg-muted rounded-full h-1.5`,children:(0,J.jsx)(`div`,{className:`rounded-full h-1.5 transition-all ${e.completionRate>=80?`bg-green-500`:e.completionRate>=50?`bg-yellow-500`:`bg-destructive`}`,style:{width:`${e.completionRate}%`}})})]},t))})})]})]})},ct=({schema:e})=>{let t=a()?.dataSource,n;try{n=s()}catch{n=void 0}let r=e.parentObject||n?.objectName,i=e.parentId||e.recordId||n?.recordId,[o,c]=(0,q.useState)([]),[l,u]=(0,q.useState)([]),[d,f]=(0,q.useState)(!0),[p,m]=(0,q.useState)(!1),[h,g]=(0,q.useState)(!1),[_,y]=(0,q.useState)(null),b=(0,q.useCallback)(async()=>{if(!t||!i){f(!1);return}f(!0);try{let n=(await t.find(e.childObject,{$filter:{[e.relationshipField]:i},$top:500}))?.data??[];c(n.map(e=>({...e}))),u(n.map(e=>({...e}))),g(!1)}catch(e){y(e?.message||`Failed to load line items`)}finally{f(!1)}},[t,i,e.childObject,e.relationshipField]);(0,q.useEffect)(()=>{b()},[b]);let x=(0,q.useCallback)(e=>{c(e),g(!0)},[]),S=(0,q.useCallback)(async()=>{if(!(!t||!i)){m(!0),y(null);try{await me(t,r||e.childObject,i,{childObject:e.childObject,relationshipField:e.relationshipField,rows:o,original:l,amountField:e.amountField,totalField:r?e.totalField:void 0}),await b()}catch(e){y(e?.message||`Failed to save line items`)}finally{m(!1)}}},[t,i,o,l,e,r,b]),C=(0,q.useMemo)(()=>({columns:e.columns,total_field:e.totalField?e.amountField||`amount`:void 0,min_rows:e.minRows,max_rows:e.maxRows,allow_add:!e.readonly,allow_delete:!e.readonly}),[e]);return(0,J.jsxs)(v,{className:w(`shadow-none`),children:[(0,J.jsxs)(E,{className:`flex-row items-center justify-between gap-2 pb-2`,children:[(0,J.jsx)(T,{className:`text-sm font-medium`,children:e.title||`Line Items`}),!e.readonly&&(0,J.jsx)(P,{type:`button`,size:`sm`,onClick:S,disabled:p||d||!h||!i,children:p?`Saving…`:`Save`})]}),(0,J.jsxs)(H,{children:[_&&(0,J.jsx)(`p`,{className:`mb-2 text-sm text-destructive`,children:_}),d?(0,J.jsx)(`p`,{className:`py-6 text-center text-sm text-muted-foreground`,children:`Loading…`}):i?(0,J.jsx)(j,{value:o,onChange:x,field:C,readonly:e.readonly}):(0,J.jsx)(`p`,{className:`py-6 text-center text-sm text-muted-foreground`,children:`Save the record first to add line items.`})]})]})},lt=({schema:e})=>(0,J.jsx)($,{schema:e,dataSource:(0,q.useContext)(o)?.dataSource??void 0});c.register(`object-form`,lt,{namespace:`plugin-form`,label:`Object Form`,category:`plugin`,inputs:[{name:`objectName`,type:`string`,label:`Object Name`,required:!0},{name:`fields`,type:`array`,label:`Fields`},{name:`mode`,type:`enum`,label:`Mode`,enum:[`create`,`edit`,`view`]},{name:`formType`,type:`enum`,label:`Form Type`,enum:[`simple`,`tabbed`,`wizard`,`split`,`drawer`,`modal`]},{name:`sections`,type:`array`,label:`Sections`},{name:`title`,type:`string`,label:`Title`},{name:`description`,type:`string`,label:`Description`},{name:`layout`,type:`enum`,label:`Layout`,enum:[`vertical`,`horizontal`,`inline`,`grid`]},{name:`columns`,type:`number`,label:`Columns`},{name:`defaultTab`,type:`string`,label:`Default Tab`},{name:`tabPosition`,type:`enum`,label:`Tab Position`,enum:[`top`,`bottom`,`left`,`right`]},{name:`allowSkip`,type:`boolean`,label:`Allow Skip Steps`},{name:`showStepIndicator`,type:`boolean`,label:`Show Step Indicator`},{name:`splitDirection`,type:`enum`,label:`Split Direction`,enum:[`horizontal`,`vertical`]},{name:`splitSize`,type:`number`,label:`Split Panel Size (%)`},{name:`splitResizable`,type:`boolean`,label:`Split Resizable`},{name:`drawerSide`,type:`enum`,label:`Drawer Side`,enum:[`top`,`bottom`,`left`,`right`]},{name:`drawerWidth`,type:`string`,label:`Drawer Width`},{name:`modalSize`,type:`enum`,label:`Modal Size`,enum:[`sm`,`default`,`lg`,`xl`,`full`]}]}),c.register(`form`,lt,{namespace:`view`,skipFallback:!0,label:`Data Form View`,category:`view`,inputs:[{name:`objectName`,type:`string`,label:`Object Name`,required:!0},{name:`fields`,type:`array`,label:`Fields`},{name:`mode`,type:`enum`,label:`Mode`,enum:[`create`,`edit`,`view`]}]}),c.register(`embeddable-form`,({schema:e})=>(0,J.jsx)(at,{config:e}),{namespace:`plugin-form`,label:`Embeddable Form`,category:`plugin`,inputs:[{name:`formId`,type:`string`,label:`Form ID`,required:!0},{name:`objectName`,type:`string`,label:`Object Name`,required:!0},{name:`title`,type:`string`,label:`Form Title`},{name:`description`,type:`string`,label:`Description`},{name:`fields`,type:`array`,label:`Fields`},{name:`allowMultiple`,type:`boolean`,label:`Allow Multiple Submissions`}]}),c.register(`form-analytics`,({schema:e})=>(0,J.jsx)(st,{formId:e.formId,formTitle:e.formTitle,metrics:e.metrics||{totalSubmissions:0}}),{namespace:`plugin-form`,label:`Form Analytics`,category:`plugin`,inputs:[{name:`formId`,type:`string`,label:`Form ID`,required:!0},{name:`formTitle`,type:`string`,label:`Form Title`},{name:`metrics`,type:`object`,label:`Submission Metrics`}]}),c.register(`object-master-detail-form`,({schema:e})=>(0,J.jsx)(Z,{schema:e,dataSource:(0,q.useContext)(o)?.dataSource??void 0}),{namespace:`plugin-form`,label:`Master-Detail Form`,category:`plugin`,inputs:[{name:`objectName`,type:`string`,label:`Parent Object`,required:!0},{name:`mode`,type:`enum`,label:`Mode`,enum:[`create`,`edit`]},{name:`sections`,type:`array`,label:`Parent Sections`},{name:`details`,type:`array`,label:`Detail Collections`,required:!0}]}),c.register(`line_items`,({schema:e})=>(0,J.jsx)(ct,{schema:e}),{namespace:`record`,skipFallback:!0,label:`Line Items`,category:`record`,inputs:[{name:`childObject`,type:`string`,label:`Child Object`,required:!0},{name:`relationshipField`,type:`string`,label:`Relationship Field`,required:!0},{name:`columns`,type:`array`,label:`Columns`,required:!0},{name:`totalField`,type:`string`,label:`Parent Total Field`},{name:`amountField`,type:`string`,label:`Amount Column`}]});export{Oe as i,Qe as n,Ye as r,$ as t};
|
|
1
|
+
import{o as e}from"./rolldown-runtime-CMxvf4Kt.js";import{_ as t,t as n}from"./vendor-react-DR370ovB.js";import{n as r}from"./infrastructure-CxityHZF.js";import{B as i,G as a,H as o,g as s,mt as c}from"./framework-CHiz03zG.js";import{At as l,Bn as u,Ea as d,Fn as f,Ft as p,Gn as m,Gt as h,It as g,Jn as _,Kn as v,Kt as y,Ln as b,Mt as x,Nt as S,Pt as C,Ta as w,Yn as T,Zs as E,ds as D,ec as O,et as k,fi as A,fs as j,h as M,is as N,jt as P,k as F,n as I,nt as L,o as R,p as z,pr as B,ps as V,qn as H,qt as U,rt as ee,t as W,tt as G,us as K,zn as te}from"./ui-components-BbPoD3rS.js";import{r as ne}from"./vendor-ui-utils-rQOI1gAo.js";var q=e(t(),1),J=n(),Y=({label:e,description:t,collapsible:n=!1,collapsed:r=!1,columns:i=1,children:a,className:o,gridClassName:s,showBorder:c})=>{let[l,u]=(0,q.useState)(r),d={1:`grid-cols-1`,2:`grid-cols-1 md:grid-cols-2`,3:`grid-cols-1 md:grid-cols-2 lg:grid-cols-3`,4:`grid-cols-1 md:grid-cols-2 lg:grid-cols-4`},f=()=>{n&&u(!l)},p=c??!!e,h=e||t?(0,J.jsxs)(`div`,{className:E(`flex items-start gap-2`,!p&&`mb-4`,n&&`cursor-pointer select-none`),onClick:f,role:n?`button`:void 0,"aria-expanded":n?!l:void 0,children:[n&&(0,J.jsx)(`span`,{className:`mt-0.5 text-muted-foreground`,children:l?(0,J.jsx)(K,{className:`h-4 w-4`}):(0,J.jsx)(j,{className:`h-4 w-4`})}),(0,J.jsxs)(`div`,{className:`flex-1`,children:[e&&(0,J.jsx)(`h3`,{className:`text-base font-semibold text-foreground`,children:e}),t&&(0,J.jsx)(`p`,{className:`text-sm text-muted-foreground mt-0.5`,children:t})]})]}):null,g=l?null:(0,J.jsx)(`div`,{className:E(`grid gap-4`,s||d[i]),children:a});return p?(0,J.jsxs)(m,{className:E(`form-section`,o),children:[h&&(0,J.jsxs)(_,{className:`pb-3`,children:[e&&(0,J.jsx)(T,{className:`text-base`,children:e}),t&&(0,J.jsx)(H,{children:t}),n&&(0,J.jsx)(`button`,{type:`button`,className:`absolute right-4 top-4 text-muted-foreground`,onClick:f,"aria-expanded":!l,"aria-label":l?`Expand section`:`Collapse section`,children:l?(0,J.jsx)(K,{className:`h-4 w-4`}):(0,J.jsx)(j,{className:`h-4 w-4`})})]}),g&&(0,J.jsx)(v,{children:g})]}):(0,J.jsxs)(`div`,{className:E(`form-section`,o),children:[h,g]})},re=({schema:e,dataSource:t,className:n})=>{let{fieldLabel:r}=O(),[a,o]=(0,q.useState)(null),[s,c]=(0,q.useState)({}),[l,u]=(0,q.useState)(!0),[d,f]=(0,q.useState)(null),[p,m]=(0,q.useState)(e.defaultTab||e.sections[0]?.name||e.sections[0]?.label||`tab-0`);q.useEffect(()=>{(async()=>{if(!t){u(!1);return}try{o(await t.getObjectSchema(e.objectName))}catch(e){f(e)}})()},[e.objectName,t]),q.useEffect(()=>{(a||!t)&&(async()=>{if(e.mode===`create`||!e.recordId||!t){c(e.initialData||e.initialValues||{}),u(!1);return}try{c(await t.findOne(e.objectName,e.recordId)||{})}catch(e){f(e)}finally{u(!1)}})()},[a,e.mode,e.recordId,e.initialData,e.initialValues,t,e.objectName]);let h=(0,q.useCallback)(t=>{let n=[];for(let i of t.fields){let t=typeof i==`string`?i:i.name;if(typeof i==`object`)n.push(i);else if(a?.fields?.[t]){let i=a.fields[t];n.push({name:t,label:r(e.objectName,t,i.label||t),type:z(i.type),required:i.required||!1,disabled:e.readOnly||e.mode===`view`||i.readonly,placeholder:i.placeholder,description:i.help||i.description,validation:W(i),field:i,options:i.options,multiple:i.multiple})}else n.push({name:t,label:t,type:`input`})}return n},[a,e.readOnly,e.mode]),g=(0,q.useCallback)(async n=>{if(!t)return e.onSuccess&&await e.onSuccess(n),n;try{let r;return e.mode===`create`?r=await t.create(e.objectName,n):e.mode===`edit`&&e.recordId&&(r=await t.update(e.objectName,e.recordId,n)),e.onSuccess&&await e.onSuccess(r),r}catch(t){throw e.onError&&e.onError(t),t}},[e,t]),_=(0,q.useCallback)(()=>{e.onCancel&&e.onCancel()},[e]),v=(e,t)=>e.name||e.label||`tab-${t}`;if(d)return(0,J.jsxs)(`div`,{className:`p-4 border border-red-300 bg-red-50 rounded-md`,children:[(0,J.jsx)(`h3`,{className:`text-red-800 font-semibold`,children:`Error loading form`}),(0,J.jsx)(`p`,{className:`text-red-600 text-sm mt-1`,children:d.message})]});if(l)return(0,J.jsxs)(`div`,{className:`p-8 text-center`,children:[(0,J.jsx)(`div`,{className:`inline-block animate-spin rounded-full h-8 w-8 border-b-2 border-gray-900`}),(0,J.jsx)(`p`,{className:`mt-2 text-sm text-gray-600`,children:`Loading form...`})]});let y={type:`form`,fields:e.sections.flatMap(e=>h(e)),layout:`vertical`,defaultValues:s,submitLabel:e.submitText||(e.mode===`create`?`Create`:`Update`),cancelLabel:e.cancelText,showSubmit:e.showSubmit!==!1&&e.mode!==`view`,showCancel:e.showCancel!==!1,onSubmit:g,onCancel:_},b=e.tabPosition===`left`||e.tabPosition===`right`;return(0,J.jsx)(`div`,{className:E(`w-full`,n,e.className),children:(0,J.jsxs)(k,{value:p,onValueChange:m,orientation:b?`vertical`:`horizontal`,className:E(b&&`flex gap-4`),children:[(0,J.jsx)(L,{className:E(b?`flex-col h-auto`:``,e.tabPosition===`bottom`&&`order-last`,e.tabPosition===`right`&&`order-last`),children:e.sections.map((e,t)=>(0,J.jsx)(ee,{value:v(e,t),className:b?`w-full justify-start`:``,children:e.label||`Tab ${t+1}`},v(e,t)))}),(0,J.jsx)(`div`,{className:`flex-1`,children:e.sections.map((t,n)=>(0,J.jsx)(G,{value:v(t,n),className:`mt-0`,children:(0,J.jsx)(Y,{description:t.description,columns:t.columns||1,children:(0,J.jsx)(i,{schema:{...y,fields:h(t),showSubmit:e.showSubmit!==!1&&e.mode!==`view`,showCancel:e.showCancel!==!1}})})},v(t,n)))})]})})},ie=({schema:e,dataSource:t,className:n})=>{let{fieldLabel:r}=O(),[a,o]=(0,q.useState)(null),[s,c]=(0,q.useState)({}),[l,u]=(0,q.useState)(!0),[f,p]=(0,q.useState)(null),[m,h]=(0,q.useState)(0),[g,_]=(0,q.useState)(new Set),[v,y]=(0,q.useState)(!1),b=e.sections.length,x=m===0,S=m===b-1;q.useEffect(()=>{(async()=>{if(!t){u(!1);return}try{o(await t.getObjectSchema(e.objectName))}catch(e){p(e)}})()},[e.objectName,t]),q.useEffect(()=>{(a||!t)&&(async()=>{if(e.mode===`create`||!e.recordId||!t){c(e.initialData||e.initialValues||{}),u(!1);return}try{c(await t.findOne(e.objectName,e.recordId)||{})}catch(e){p(e)}finally{u(!1)}})()},[a,e.mode,e.recordId,e.initialData,e.initialValues,t,e.objectName]);let C=(0,q.useCallback)(t=>{let n=[],i=(e,t)=>typeof t==`string`&&t.trim()?{...e,visible:e=>I(t,e)}:e;for(let o of t.fields){let t=typeof o==`string`?o:o.name;if(typeof o==`object`)n.push(i(o,o.visibleOn));else if(a?.fields?.[t]){let o=a.fields[t];n.push(i({name:t,label:r(e.objectName,t,o.label||t),type:z(o.type),required:o.required||!1,disabled:e.readOnly||e.mode===`view`||o.readonly,placeholder:o.placeholder,description:o.help||o.description,validation:W(o),field:o,options:o.options,multiple:o.multiple},o.visible_on??o.visibleOn))}else n.push({name:t,label:t,type:`input`})}return n},[a,e.readOnly,e.mode,e.objectName]),w=(0,q.useMemo)(()=>m>=0&&m<b?C(e.sections[m]):[],[m,b,e.sections,C]),T=(0,q.useCallback)(async n=>{let r={...s,...n};if(c(r),_(e=>new Set(e).add(m)),S){y(!0);try{if(!t)return e.onSuccess&&await e.onSuccess(r),r;let n;return e.mode===`create`?n=await t.create(e.objectName,r):e.mode===`edit`&&e.recordId&&(n=await t.update(e.objectName,e.recordId,r)),e.onSuccess&&await e.onSuccess(n),n}catch(t){throw e.onError&&e.onError(t),t}finally{y(!1)}}else k(m+1)},[s,m,S,e,t]),k=(0,q.useCallback)(t=>{t>=0&&t<b&&(h(t),e.onStepChange&&e.onStepChange(t))},[b,e]),A=(0,q.useCallback)(()=>{k(m-1)},[m,k]),j=(0,q.useCallback)(()=>{e.onCancel&&e.onCancel()},[e]),M=(0,q.useCallback)(t=>{(e.allowSkip||g.has(t)||t<=m)&&k(t)},[e.allowSkip,g,m,k]);if(f)return(0,J.jsxs)(`div`,{className:`p-4 border border-red-300 bg-red-50 rounded-md`,children:[(0,J.jsx)(`h3`,{className:`text-red-800 font-semibold`,children:`Error loading form`}),(0,J.jsx)(`p`,{className:`text-red-600 text-sm mt-1`,children:f.message})]});if(l)return(0,J.jsxs)(`div`,{className:`p-8 text-center`,children:[(0,J.jsx)(`div`,{className:`inline-block animate-spin rounded-full h-8 w-8 border-b-2 border-gray-900`}),(0,J.jsx)(`p`,{className:`mt-2 text-sm text-gray-600`,children:`Loading form...`})]});let N=e.sections[m];return(0,J.jsxs)(`div`,{className:E(`w-full`,n,e.className),children:[e.showStepIndicator!==!1&&(0,J.jsx)(`nav`,{"aria-label":`Progress`,className:`mb-8`,children:(0,J.jsx)(`ol`,{className:`flex items-center`,children:e.sections.map((t,n)=>{let r=n===m,i=g.has(n),a=e.allowSkip||i||n<=m;return(0,J.jsxs)(`li`,{className:E(`relative flex-1`,n!==b-1&&`pr-8 sm:pr-12`),children:[n!==b-1&&(0,J.jsx)(`div`,{className:`absolute top-3 sm:top-4 left-6 -right-4 sm:left-10 sm:-right-2 h-0.5`,"aria-hidden":`true`,children:(0,J.jsx)(`div`,{className:E(`h-full`,i?`bg-primary`:`bg-muted`)})}),(0,J.jsxs)(`button`,{type:`button`,className:E(`group relative flex items-center`,a?`cursor-pointer`:`cursor-not-allowed`),onClick:()=>M(n),disabled:!a,children:[(0,J.jsx)(`span`,{className:E(`flex h-6 w-6 sm:h-8 sm:w-8 items-center justify-center rounded-full text-xs sm:text-sm font-medium transition-colors`,i&&`bg-primary text-primary-foreground`,r&&!i&&`border-2 border-primary bg-background text-primary`,!r&&!i&&`border-2 border-muted bg-background text-muted-foreground`),children:i?(0,J.jsx)(V,{className:`h-3 w-3 sm:h-4 sm:w-4`}):n+1}),(0,J.jsx)(`span`,{className:`ml-2 sm:ml-3 text-xs sm:text-sm font-medium hidden sm:block`,children:(0,J.jsx)(`span`,{className:E(r?`text-foreground`:`text-muted-foreground`),children:t.label||`Step ${n+1}`})})]})]},n)})})}),(0,J.jsx)(`div`,{className:`min-h-[200px]`,children:N&&(0,J.jsx)(Y,{label:N.label,description:N.description,columns:N.columns||1,children:w.length>0?(0,J.jsx)(i,{schema:{type:`form`,fields:w,layout:`vertical`,defaultValues:s,showSubmit:!1,showCancel:!1,onSubmit:T}}):(0,J.jsx)(`div`,{className:`text-center py-8 text-muted-foreground`,children:`No fields configured for this step`})})}),(0,J.jsxs)(`div`,{className:`flex items-center justify-between mt-6 pt-4 border-t`,children:[(0,J.jsx)(`div`,{children:e.showCancel!==!1&&(0,J.jsx)(B,{variant:`ghost`,onClick:j,children:e.cancelText||`Cancel`})}),(0,J.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,J.jsxs)(`span`,{className:`text-sm text-muted-foreground mr-2`,children:[`Step `,m+1,` of `,b]}),!x&&(0,J.jsxs)(B,{variant:`outline`,onClick:A,children:[(0,J.jsx)(D,{className:`h-4 w-4 mr-1`}),e.prevText||`Back`]}),S?(0,J.jsxs)(B,{onClick:()=>T(s),disabled:v||e.mode===`view`,children:[v&&(0,J.jsx)(d,{className:`mr-2 h-4 w-4 animate-spin`,"aria-hidden":!0}),v?`Submitting...`:e.submitText||(e.mode===`create`?`Create`:`Update`)]}):(0,J.jsxs)(B,{onClick:()=>T(s),children:[e.nextText||`Next`,(0,J.jsx)(K,{className:`h-4 w-4 ml-1`})]})]})]})]})},ae=({schema:e,dataSource:t,className:n})=>{let{fieldLabel:r}=O(),[a,o]=(0,q.useState)(null),[s,c]=(0,q.useState)({}),[l,u]=(0,q.useState)(!0),[d,f]=(0,q.useState)(null);(0,q.useEffect)(()=>{(async()=>{if(!t){u(!1);return}try{o(await t.getObjectSchema(e.objectName))}catch(e){f(e),u(!1)}})()},[e.objectName,t]),(0,q.useEffect)(()=>{(a||!t)&&(async()=>{if(e.mode===`create`||!e.recordId){c(e.initialData||e.initialValues||{}),u(!1);return}if(!t){c(e.initialData||e.initialValues||{}),u(!1);return}try{c(await t.findOne(e.objectName,e.recordId)||{})}catch(e){f(e)}finally{u(!1)}})()},[a,e.mode,e.recordId,e.initialData,e.initialValues,t,e.objectName]);let p=(0,q.useCallback)(t=>{let n=[],i=(e,t)=>typeof t==`string`&&t.trim()?{...e,visible:e=>I(t,e)}:e;for(let o of t.fields){let t=typeof o==`string`?o:o.name;if(typeof o==`object`)n.push(i(o,o.visibleOn));else if(a?.fields?.[t]){let o=a.fields[t];n.push(i({name:t,label:r(e.objectName,t,o.label||t),type:z(o.type),required:o.required||!1,disabled:e.readOnly||e.mode===`view`||o.readonly,placeholder:o.placeholder,description:o.help||o.description,validation:W(o),field:o,options:o.options,multiple:o.multiple},o.visible_on??o.visibleOn))}else n.push({name:t,label:t,type:`input`})}return n},[a,e.readOnly,e.mode,e.objectName]),m=(0,q.useCallback)(async n=>{if(!t)return e.onSuccess&&await e.onSuccess(n),n;try{let r;return e.mode===`create`?r=await t.create(e.objectName,n):e.mode===`edit`&&e.recordId&&(r=await t.update(e.objectName,e.recordId,n)),e.onSuccess&&await e.onSuccess(r),r}catch(t){throw e.onError&&e.onError(t),t}},[e,t]),g=(0,q.useCallback)(()=>{e.onCancel&&e.onCancel()},[e]),_=(0,q.useMemo)(()=>e.sections.slice(0,1),[e.sections]),v=(0,q.useMemo)(()=>e.sections.slice(1),[e.sections]);(0,q.useMemo)(()=>e.sections.flatMap(e=>p(e)),[e.sections,p]);let b=e.splitDirection||`horizontal`,x=e.splitSize||50;if(d)return(0,J.jsxs)(`div`,{className:`p-4 border border-red-300 bg-red-50 rounded-md`,children:[(0,J.jsx)(`h3`,{className:`text-red-800 font-semibold`,children:`Error loading form`}),(0,J.jsx)(`p`,{className:`text-red-600 text-sm mt-1`,children:d.message})]});if(l)return(0,J.jsxs)(`div`,{className:`p-8 text-center`,children:[(0,J.jsx)(`div`,{className:`inline-block animate-spin rounded-full h-8 w-8 border-b-2 border-gray-900`}),(0,J.jsx)(`p`,{className:`mt-2 text-sm text-gray-600`,children:`Loading form...`})]});let S={type:`form`,layout:`vertical`,defaultValues:s,onSubmit:m,onCancel:g},C=(t,n)=>(0,J.jsx)(`div`,{className:`space-y-4 p-4`,children:t.map((t,r)=>(0,J.jsx)(Y,{label:t.label,description:t.description,columns:t.columns||1,children:(0,J.jsx)(i,{schema:{...S,fields:p(t),showSubmit:n&&e.showSubmit!==!1&&e.mode!==`view`,showCancel:n&&e.showCancel!==!1,submitLabel:e.submitText||(e.mode===`create`?`Create`:`Update`),cancelLabel:e.cancelText}})},t.name||t.label||r))});return(0,J.jsx)(`div`,{className:E(`w-full`,n,e.className),children:(0,J.jsxs)(U,{orientation:b,className:`min-h-[300px] rounded-lg border`,children:[(0,J.jsx)(y,{defaultSize:x,minSize:20,children:C(_,v.length===0)}),v.length>0&&(0,J.jsxs)(J.Fragment,{children:[(0,J.jsx)(h,{withHandle:e.splitResizable!==!1}),(0,J.jsx)(y,{defaultSize:100-x,minSize:20,children:C(v,!0)})]})]})})},X=e=>e==null?void 0:e.id??e._id??e.recordId,oe=new Set([`id`,`_id`,`recordId`]);function se(e,t){return e?Object.entries(e).every(([e,n])=>e===t||oe.has(e)?!0:n==null||n===``):!0}function ce(e,t,n){let r=[{object:e,action:`create`,data:t}];for(let e of n)for(let t of e.rows)se(t,e.relationshipField)||r.push({object:e.childObject,action:`create`,data:{...t,[e.relationshipField]:{$ref:0}}});return r}function le(e,t,n,r){let i=[{object:e,action:`update`,id:t,data:n}];for(let e of r){let n=(e.rows||[]).map(n=>({...n,[e.relationshipField]:t})),{toCreate:r,toUpdate:a,toDelete:o}=ue(e.original||[],n);for(let t of r){if(se(t,e.relationshipField))continue;let{id:n,_id:r,recordId:a,...o}=t;i.push({object:e.childObject,action:`create`,data:o})}for(let t of a)i.push({object:e.childObject,action:`update`,id:X(t),data:t});for(let t of o)i.push({object:e.childObject,action:`delete`,id:t})}return i}function ue(e,t){let n=new Set(t.map(X).filter(Boolean));return{toCreate:t.filter(e=>!X(e)),toUpdate:t.filter(e=>X(e)),toDelete:(e||[]).map(X).filter(e=>!!e&&!n.has(e))}}function de(e,t){return Array.isArray(e)?e.reduce((e,n)=>{let r=Number(n?.[t]);return e+(Number.isFinite(r)?r:0)},0):0}function fe(e){return Array.isArray(e)?e:e&&Array.isArray(e.records)?e.records:e&&Array.isArray(e.data)?e.data:e&&(e.id||e._id)?[e]:[]}async function pe(e,t,n){return n.length===0?[]:typeof e.bulk==`function`?fe(await e.bulk(t,`create`,n)):fe(await Promise.all(n.map(n=>e.create(t,n))))}async function me(e,t,n,r){let i=[],a=r.rows.filter(e=>X(e)||!se(e,r.relationshipField)).map(e=>({...e,[r.relationshipField]:n}));if(r.original!==void 0){let{toCreate:t,toUpdate:n,toDelete:o}=ue(r.original,a),s=await pe(e,r.childObject,t);for(let e of s){let t=X(e);t&&i.push({object:r.childObject,id:t})}await Promise.all(n.map(t=>e.update(r.childObject,X(t),t))),await Promise.all(o.map(t=>e.delete(r.childObject,t)))}else{let t=await pe(e,r.childObject,a);for(let e of t){let t=X(e);t&&i.push({object:r.childObject,id:t})}}if(r.totalField){let i=de(r.rows,r.amountField||`amount`);await e.update(t,n,{[r.totalField]:i})}return{created:i}}var he=new Set([`id`,`_id`,`recordId`,`created_at`,`updated_at`,`created_by`,`updated_by`,`createdAt`,`updatedAt`,`createdBy`,`updatedBy`,`organization_id`,`tenant_id`,`space`,`owner`]),ge=new Set([`position`,`sort_order`,`sequence`,`line_no`,`line_number`,`sort`]),_e=new Set([`formula`,`summary`,`rollup`,`autonumber`,`auto_number`,`file`,`image`,`avatar`,`json`,`object`,`grid`,`table`,`location`,`vector`,`html`,`markdown`,`richtext`]);function ve(e){switch(e){case`number`:case`percent`:case`rating`:case`slider`:return`number`;case`currency`:return`currency`;case`date`:case`datetime`:case`time`:return`date`;case`select`:case`picklist`:case`radio`:case`boolean`:case`toggle`:return`select`;case`lookup`:case`master_detail`:return`lookup`;default:return`text`}}function ye(e){if(e?.type===`boolean`||e?.type===`toggle`)return[{label:`Yes`,value:`true`},{label:`No`,value:`false`}];let t=e?.options;if(Array.isArray(t))return t.map(e=>typeof e==`object`&&e?{label:String(e.label??e.value),value:String(e.value)}:{label:String(e),value:String(e)})}function be(e,t){let n=e?.fields;if(!n||typeof n!=`object`)return;let r;for(let[e,i]of Object.entries(n)){let n=i;if(n?.reference===t){if(n?.type===`master_detail`)return e;n?.type===`lookup`&&!r&&(r=e)}}return r}var xe=[`name`,`title`,`subject`,`label`,`full_name`,`display_name`,`code`],Se={select:0,currency:1,number:1,lookup:2,date:3,text:4};function Ce(e,t){if(t<=0||e.length<=t)return e;let n=new Set,r=e.find(e=>xe.includes(e.field))??e[0];r&&n.add(r.field);for(let t of e)t.required&&n.add(t.field);let i=e.map((e,t)=>({c:e,i:t})).filter(({c:e})=>!n.has(e.field)).sort((e,t)=>(Se[e.c.type]??5)-(Se[t.c.type]??5)||e.i-t.i);for(let{c:e}of i){if(n.size>=t)break;n.add(e.field)}return e.map(e=>n.has(e.field)?e:{...e,defaultHidden:!0})}function we(e,t={}){let n=e?.fields;if(!n||typeof n!=`object`)return[];let r=new Set([...t.exclude??[],...t.relationshipField?[t.relationshipField]:[]]),i=[];for(let[e,t]of Object.entries(n)){let n=t;if(he.has(e)||r.has(e)||ge.has(e)||n?.system||n?.readonly||n?.hidden||_e.has(n?.type))continue;let a={field:e,label:n?.label||e,type:ve(n?.type),required:!!n?.required},o=ye(n);a.type===`select`&&o&&(a.options=o),a.type===`lookup`&&(a.reference=n?.reference,a.displayField=n?.display_field||n?.reference_field),n?.readonlyWhen&&(a.readonlyWhen=n.readonlyWhen),(n?.requiredWhen??n?.conditionalRequired)&&(a.requiredWhen=n.requiredWhen??n.conditionalRequired);let s=typeof n?.expression==`string`?n.expression:n?.expression?.source;s&&typeof s==`string`&&(a.computed=!0,a.expr=s,a.required=!1,typeof n?.scale==`number`&&(a.scale=n.scale)),i.push(a)}return Ce(i,t.maxColumns??6)}var Te=new Set([`formula`,`summary`,`rollup`,`autonumber`,`auto_number`]);function Ee(e,t={}){let n=e?.fields;if(!n||typeof n!=`object`)return[];let r=new Set([...t.exclude??[],...t.relationshipField?[t.relationshipField]:[]]),i=[];for(let[e,t]of Object.entries(n)){let n=t;he.has(e)||r.has(e)||ge.has(e)||n?.system||n?.hidden||Te.has(n?.type)||i.push(e)}return i}var De=new Set([`textarea`,`richtext`,`html`,`markdown`,`rich-text`,`file`,`image`,`avatar`,`attachment`,`json`,`location`,`address`]);function Oe(e,t,n={}){if(t===`grid`||t===`form`)return t;let r=e?.fields??{},i=Ee(e,{relationshipField:n.relationshipField});return i.some(e=>De.has(r[e]?.type))||i.length>8?`form`:`grid`}var ke=[`amount`,`total`,`subtotal`,`line_total`,`line_amount`,`net_amount`];function Ae(e){let t=e.filter(e=>e.type===`number`||e.type===`currency`);if(t.length===0)return;let n=t.find(e=>e.computed);if(n)return n.field;let r=t.find(e=>ke.includes(e.field));if(r)return r.field;let i=[...t].reverse().find(e=>e.type===`currency`);return i?i.field:t[t.length-1].field}function je(e,t,n,r={}){let i=r.relationshipField||be(t,n);if(!i)throw Error(`MasterDetailForm: could not find a lookup/master_detail field on "${e}" referencing "${n}". Set relationshipField explicitly.`);let a=r.columns?.length?r.columns:we(t,{relationshipField:i}),o=r.amountField||Ae(a);return{childObject:e,relationshipField:i,columns:a,formFields:Ee(t,{relationshipField:i}),mode:Oe(t,r.inlineEdit??(t?.fields)?.[i]?.inlineEdit,{relationshipField:i}),amountField:o,sortField:Object.keys(t?.fields??{}).find(e=>ge.has(e))}}function Me(e){if(!e)return{};let t={};return e.querySelectorAll(`[name]`).forEach(e=>{let n=e.getAttribute(`name`);if(n){if(e.tagName===`INPUT`){let r=e;if(r.type===`checkbox`){t[n]=r.checked;return}if(r.type===`radio`){r.checked&&(t[n]=r.value);return}if(r.type===`number`||r.type===`range`){if(r.value===``){t[n]=null;return}let e=Number(r.value);t[n]=Number.isFinite(e)?e:r.value;return}t[n]=r.value;return}t[n]=e.value}}),t}var Ne=({details:e,state:t,setRows:n,formHostRef:r,taxRateField:i,formKey:a,onRowExpand:o,onAddViaForm:s})=>{let[c,l]=(0,q.useState)({}),u=(0,q.useRef)(``);(0,q.useEffect)(()=>{let e=r.current;if(!e)return;let t=()=>{let t=Me(e),n;try{n=JSON.stringify(t)}catch{n=String(Math.random())}n!==u.current&&(u.current=n,l(t))};t();let n=()=>t();e.addEventListener(`input`,n),e.addEventListener(`change`,n);let i=[120,360,800].map(e=>setTimeout(t,e));return()=>{e.removeEventListener(`input`,n),e.removeEventListener(`change`,n),i.forEach(clearTimeout)}},[r,a,e.length]);let d=c[i],f=d===void 0?null:Number.isFinite(Number(d))?Number(d):0,p=e.reduce((e,n,r)=>e+de(t[r]?.rows??[],n.amountField||`amount`),0),m=f!==null&&e.some(e=>!!e.amountField),h=f??0,g=h/100*p,_=p+g,v=e=>`¥${e.toLocaleString(void 0,{minimumFractionDigits:2,maximumFractionDigits:2})}`;return(0,J.jsxs)(J.Fragment,{children:[e.map((e,r)=>(0,J.jsxs)(`section`,{className:`space-y-2`,children:[(0,J.jsx)(`h3`,{className:`text-sm font-medium text-foreground`,children:e.title||`Line Items`}),e.columns?.length?(0,J.jsx)(M,{value:t[r]?.rows??[],onChange:e=>n(r,e),contextRecord:c,...e.inlineMode===`form`||(e.formFields?.length??0)>(e.columns?.length??0)?{onRowExpand:e=>o(r,e)}:{},displayMode:e.inlineMode===`form`?`list`:`grid`,...e.inlineMode===`form`?{onAdd:()=>s(r)}:{},field:{columns:e.columns,total_field:m?void 0:e.amountField||(e.totalField?`amount`:void 0),sort_field:e.sortField,min_rows:e.minRows,max_rows:e.maxRows,add_label:e.inlineMode===`form`?e.addLabel||`Add`:e.addLabel}}):(0,J.jsx)(`p`,{className:`py-4 text-sm text-muted-foreground`,children:`Loading columns…`})]},`${e.childObject}-${r}`)),m&&(0,J.jsx)(`div`,{className:`flex justify-end`,children:(0,J.jsxs)(`dl`,{className:`w-64 space-y-1.5 text-sm`,"data-testid":`md-totals`,children:[(0,J.jsxs)(`div`,{className:`flex items-center justify-between`,children:[(0,J.jsx)(`dt`,{className:`text-muted-foreground`,children:`Subtotal`}),(0,J.jsx)(`dd`,{className:`tabular-nums`,"data-testid":`md-subtotal`,children:v(p)})]}),(0,J.jsxs)(`div`,{className:`flex items-center justify-between`,children:[(0,J.jsxs)(`dt`,{className:`text-muted-foreground`,children:[`Tax (`,h,`%)`]}),(0,J.jsx)(`dd`,{className:`tabular-nums`,"data-testid":`md-tax`,children:v(g)})]}),(0,J.jsxs)(`div`,{className:`flex items-center justify-between border-t border-border pt-1.5 text-base font-semibold`,children:[(0,J.jsx)(`dt`,{children:`Total`}),(0,J.jsx)(`dd`,{className:`tabular-nums`,"data-testid":`md-grand-total`,children:v(_)})]})]})})]})},Z=({schema:e,dataSource:t,className:n})=>{let r=e.details||[],i=e.mode===`edit`&&!!e.recordId,a=r.some(e=>!e.relationshipField||!e.columns?.length),[o,s]=(0,q.useState)(a?null:r),c=o??r;(0,q.useEffect)(()=>{if(!a){s(r);return}if(!t||typeof t.getObjectSchema!=`function`)return;let n=!1;return(async()=>{let i=await Promise.all(r.map(async n=>{if(n.relationshipField&&n.columns?.length)return n;try{let r=await t.getObjectSchema(n.childObject),i=je(n.childObject,r,e.objectName,{relationshipField:n.relationshipField,columns:n.columns,amountField:n.amountField});return{...n,relationshipField:i.relationshipField,columns:i.columns,formFields:n.formFields??i.formFields,inlineMode:n.inlineMode??i.mode,amountField:n.amountField??i.amountField,sortField:n.sortField??i.sortField}}catch{return n}}));n||s(i)})(),()=>{n=!0}},[t,e.objectName,e.details]);let[l,u]=(0,q.useState)(()=>r.map(()=>({rows:[],original:[]}))),d=(0,q.useRef)(l);d.current=l;let[f,p]=(0,q.useState)(0),[h,g]=(0,q.useState)(!1),y=(0,q.useRef)(!1),b=(0,q.useRef)(null),x=(0,q.useCallback)(()=>{y.current=!1,g(!1),b.current&&=(clearTimeout(b.current),null)},[]);(0,q.useEffect)(()=>{let n=!1;if(!(!i||!t))return(async()=>{let r=await Promise.all(c.map(async n=>{if(!n.relationshipField)return{rows:[],original:[]};try{let r=(await t.find(n.childObject,{$filter:{[n.relationshipField]:e.recordId},$top:500}))?.data??[];return{rows:r.map(e=>({...e})),original:r.map(e=>({...e}))}}catch{return{rows:[],original:[]}}}));n||u(r)})(),()=>{n=!0}},[i,t,e.recordId,o]);let S=(0,q.useCallback)((e,t)=>{u(n=>n.map((n,r)=>r===e?{...n,rows:t}:n))},[]),C=e.taxRateField||`tax_rate`,[w,D]=(0,q.useState)(null),O=w?l[w.detailIdx]?.rows?.[w.rowIdx]:void 0,k=w?c[w.detailIdx]:void 0,A=(0,q.useCallback)((e,t,n)=>{u(r=>r.map((r,i)=>i===e?{...r,rows:r.rows.map((e,r)=>r===t?{...e,...n}:e)}:r))},[]),j=(0,q.useCallback)(e=>{u(t=>{let n=t.map((t,n)=>n===e?{...t,rows:[...t.rows,{}]}:t);return D({detailIdx:e,rowIdx:n[e].rows.length-1,isNew:!0}),n})},[]),M=(0,q.useCallback)(()=>{D(e=>(e?.isNew&&u(t=>t.map((t,n)=>n===e.detailIdx?{...t,rows:t.rows.filter((t,n)=>n!==e.rowIdx)}:t)),null))},[]),N=(0,q.useCallback)(async t=>{x(),ne.success(i?e.title?`${e.title} saved`:`Saved`:`Created`),i||(u(c.map(()=>({rows:[],original:[]}))),p(e=>e+1)),await e.onSuccess?.(t)},[i,e.onSuccess,e.title,c.length,x]),P=(0,q.useCallback)(t=>{x(),ne.error(t?.message||`Save failed`),e.onError?.(t)},[e,x]),F=(0,q.useCallback)(async n=>{let r=[];for(let a=0;a<c.length;a++){let o=c[a],{rows:s,original:l}=d.current[a];if(!o.relationshipField)continue;let{created:u}=await me(t,e.objectName,n,{childObject:o.childObject,relationshipField:o.relationshipField,rows:s,original:i?l:void 0,amountField:o.amountField,totalField:o.totalField});r.push(...u)}return r},[c,i,t,e.objectName]),I=(0,q.useCallback)(async n=>{let r=X(n)??e.recordId;if(!r)throw Error(`MasterDetailForm: parent record has no id after save`);if(!t)throw Error(`MasterDetailForm: dataSource is required`);let a=[];try{a=await F(r)}catch(n){throw i||await Promise.allSettled([...a.map(e=>t.delete(e.object,e.id)),t.delete(e.objectName,r)]),n}await N(n)},[t,i,F,e,N]),L=typeof t?.batchTransaction==`function`,R=(0,q.useCallback)(async n=>{let r=t,a={...n};c.forEach((e,t)=>{e.totalField&&(a[e.totalField]=de(d.current[t]?.rows??[],e.amountField||`amount`))});let o=i?le(e.objectName,e.recordId,a,c.filter(e=>e.relationshipField).map((e,t)=>({childObject:e.childObject,relationshipField:e.relationshipField,rows:d.current[t]?.rows??[],original:d.current[t]?.original??[]}))):ce(e.objectName,a,c.filter(e=>e.relationshipField).map((e,t)=>({childObject:e.childObject,relationshipField:e.relationshipField,rows:d.current[t]?.rows??[]})));return(await r.batchTransaction(o))?.results?.[0]??{...a,id:e.recordId}},[t,c,e.objectName,e.recordId,i]),z=(0,q.useMemo)(()=>({type:`object-form`,objectName:e.objectName,mode:e.mode??`create`,recordId:e.recordId,formType:e.formType,sections:e.sections,fields:e.fields,title:e.title,showSubmit:!1,showCancel:!1,...L?{submitHandler:R,onSuccess:N}:{onSuccess:I},onError:P}),[e,I,L,R,N,P]),V=(0,q.useRef)(null),H=e.submitText??(i?`Save`:`Create`),U=(0,q.useCallback)(()=>{y.current||V.current?.querySelector(`form`)&&(y.current=!0,g(!0),setTimeout(()=>{(V.current?.querySelector(`form`))?.requestSubmit()},0),b.current=setTimeout(()=>x(),1500))},[x]);return(0,q.useEffect)(()=>()=>{b.current&&clearTimeout(b.current)},[]),(0,J.jsxs)(`div`,{className:E(`space-y-6`,n,e.className),children:[(0,J.jsx)(`div`,{ref:V,children:(0,J.jsx)($,{schema:z,dataSource:t},f)}),(0,J.jsx)(Ne,{details:c,state:l,setRows:S,formHostRef:V,taxRateField:C,formKey:f,onRowExpand:(e,t)=>D({detailIdx:e,rowIdx:t}),onAddViaForm:j}),w&&k&&(0,J.jsxs)(m,{className:`border-primary/40 shadow-none ring-1 ring-primary/10`,"data-testid":`md-row-form`,children:[(0,J.jsxs)(_,{className:`pb-2 flex flex-row items-center justify-between gap-2 space-y-0`,children:[(0,J.jsxs)(T,{className:`text-sm font-medium`,children:[k.title||`Line item`,` — row `,w.rowIdx+1]}),(0,J.jsx)(B,{type:`button`,variant:`ghost`,size:`sm`,className:`h-7 text-xs text-muted-foreground`,onClick:M,children:`Close`})]}),(0,J.jsx)(v,{children:(0,J.jsx)($,{schema:{type:`object-form`,objectName:k.childObject,mode:`edit`,initialData:O??{},...k.formFields?.length?{fields:k.formFields}:{},submitText:`Apply`,submitHandler:async e=>e,onSuccess:e=>{A(w.detailIdx,w.rowIdx,e),D(null)},onCancel:M},dataSource:t},`row-${w.detailIdx}-${w.rowIdx}`)})]}),(0,J.jsxs)(`div`,{className:`flex items-center justify-end gap-2 border-t border-border pt-4`,children:[e.onCancel&&(0,J.jsx)(B,{type:`button`,variant:`outline`,onClick:e.onCancel,disabled:h,"data-testid":`md-form-cancel`,children:`Cancel`}),(0,J.jsx)(B,{type:`button`,onClick:U,disabled:h||a&&!o,"data-testid":`md-form-submit`,children:h?`Saving…`:H})]})]})},Pe=new Set([`field:formula`,`field:summary`,`field:auto_number`,`field:autonumber`]),Fe=new Set([`field:textarea`,`field:markdown`,`field:html`,`field:grid`,`field:rich-text`,`textarea`,`markdown`,`html`,`grid`,`rich-text`]),Ie=new Set([`formula`,`summary`,`auto_number`,`autonumber`]);function Le(e){return Fe.has(e)}function Re(e){return Ie.has(e)}function ze(e){return e<=3?1:2}function Be(e,t){return t<=1?e:e.map(e=>e.colSpan===void 0&&e.type&&Le(e.type)?{...e,colSpan:t}:e)}var Ve=new Set([`id`,`created_at`,`createdAt`,`updated_at`,`updatedAt`,`last_modified_at`,`lastModifiedAt`,`deleted_at`,`deletedAt`,`created_by`,`createdBy`,`updated_by`,`updatedBy`,`last_modified_by`,`lastModifiedBy`,`organization_id`,`organizationId`,`org_id`,`orgId`,`tenant_id`,`tenantId`,`owner`,`owner_id`,`_version`,`_rev`]);function He(e,t){return e.filter(e=>!(Ve.has(e.name)||t?.fields?.[e.name]?.readonly===!0))}function Ue(e,t){return e.filter(e=>{if(e.type&&Pe.has(e.type))return!1;let n=t?.fields?.[e.name];return n?!Re(n.type):!0})}function We(e){return e<=1?`default`:e===2?`xl`:`full`}function Q(e,t,n,r){let i=[...e];if(i=He(i,t),r===`create`&&(i=Ue(i,t)),n!==void 0)return i=Be(i,n),{fields:i,columns:n};let a=ze(i.length);return i=Be(i,a),{fields:i,columns:a}}var Ge=new Set([`id`,`_id`,`created_at`,`updated_at`,`createdAt`,`updatedAt`,`organization_id`,`organizationId`]),Ke=new Set([`formula`,`rollup`,`lookup_value`,`auto_number`,`autonumber`,`computed`]);function qe(e,t){if(!e||typeof e!=`object`)return e;let n={},r=t?.fields;for(let[t,i]of Object.entries(e))if(!Ge.has(t)){if(r){let e=r[t];if(!e)continue;let n=String(e.type||``).toLowerCase();if(Ke.has(n)||e.computed===!0||e.formula||e.readOnly===!0||e.readonly===!0)continue}n[t]=i}return n}var Je={1:void 0,2:`grid gap-4 grid-cols-1 @md:grid-cols-2`,3:`grid gap-4 grid-cols-1 @md:grid-cols-2 @2xl:grid-cols-3`,4:`grid gap-4 grid-cols-1 @md:grid-cols-2 @2xl:grid-cols-3 @4xl:grid-cols-4`},Ye=({schema:e,dataSource:t,className:n})=>{let{fieldLabel:r}=O(),[a,o]=(0,q.useState)(null),[s,c]=(0,q.useState)([]),[l,u]=(0,q.useState)({}),[d,f]=(0,q.useState)(!0),[m,h]=(0,q.useState)(null),_=e.open!==!1,v=e.drawerSide||`right`,[y,b]=(0,q.useState)(()=>{let t={};return e.sections?.forEach((e,n)=>{let r=e.name||String(n);e.collapsed&&(t[r]=!0)}),t});(0,q.useEffect)(()=>{(async()=>{if(!t){f(!1);return}try{o(await t.getObjectSchema(e.objectName))}catch(e){h(e),f(!1)}})()},[e.objectName,t]),(0,q.useEffect)(()=>{(a||!t)&&(async()=>{if(e.mode===`create`||!e.recordId){u(e.initialData||e.initialValues||{}),f(!1);return}if(!t){u(e.initialData||e.initialValues||{}),f(!1);return}try{u(await t.findOne(e.objectName,e.recordId)||{})}catch(e){h(e)}finally{f(!1)}})()},[a,e.mode,e.recordId,e.initialData,e.initialValues,t,e.objectName]);let w=(0,q.useCallback)(t=>{let n=[];for(let i of t.fields){let t=typeof i==`string`?i:i.name;if(typeof i==`object`)n.push(i);else if(a?.fields?.[t]){let i=a.fields[t];n.push({name:t,label:r(e.objectName,t,i.label||t),type:z(i.type),required:i.required||!1,disabled:e.readOnly||e.mode===`view`||i.readonly,placeholder:i.placeholder,description:i.help||i.description,validation:W(i),field:i,options:i.options,multiple:i.multiple})}else n.push({name:t,label:t,type:`input`})}return n},[a,e.readOnly,e.mode]);(0,q.useEffect)(()=>{if(!a&&t)return;if(e.customFields?.length){c(e.customFields),f(!1);return}if(e.sections?.length){f(!1);return}if(!a)return;let n=e.fields||Object.keys(a.fields||{}),i=[];for(let t of n){let n=typeof t==`string`?t:t.name;if(!n)continue;let o=a.fields?.[n];o&&i.push({name:n,label:r(e.objectName,n,o.label||n),type:z(o.type),required:o.required||!1,disabled:e.readOnly||e.mode===`view`||o.readonly,placeholder:o.placeholder,description:o.help||o.description,validation:W(o),field:o,options:o.options,multiple:o.multiple})}c(i),f(!1)},[a,e.fields,e.customFields,e.sections,e.readOnly,e.mode,t]);let T=(0,q.useCallback)(async n=>{if(!t)return e.onSuccess&&await e.onSuccess(n),e.onOpenChange?.(!1),n;try{let r,i=qe(n,a);return e.mode===`create`?r=await t.create(e.objectName,i):e.mode===`edit`&&e.recordId&&(r=await t.update(e.objectName,e.recordId,i)),e.onSuccess&&await e.onSuccess(r),e.onOpenChange?.(!1),r}catch(t){throw e.onError&&e.onError(t),t}},[e,t,a]),D=(0,q.useCallback)(()=>{e.onCancel&&e.onCancel(),e.onOpenChange?.(!1)},[e]),k=(0,q.useMemo)(()=>{if(e.drawerWidth)return v===`left`||v===`right`?{width:e.drawerWidth,maxWidth:e.drawerWidth}:{height:e.drawerWidth,maxHeight:e.drawerWidth}},[e.drawerWidth,v]),A={type:`form`,layout:e.layout===`vertical`||e.layout===`horizontal`?e.layout:`vertical`,defaultValues:l,submitLabel:e.submitText||(e.mode===`create`?`Create`:`Update`),cancelLabel:e.cancelText,showSubmit:e.showSubmit!==!1&&e.mode!==`view`,showCancel:e.showCancel!==!1,onSubmit:T,onCancel:D},j=()=>{if(m)return(0,J.jsxs)(`div`,{className:`p-4 border border-red-300 bg-red-50 rounded-md`,children:[(0,J.jsx)(`h3`,{className:`text-red-800 font-semibold`,children:`Error loading form`}),(0,J.jsx)(`p`,{className:`text-red-600 text-sm mt-1`,children:m.message})]});if(d)return(0,J.jsxs)(`div`,{className:`p-8 text-center`,children:[(0,J.jsx)(`div`,{className:`inline-block animate-spin rounded-full h-8 w-8 border-b-2 border-gray-900`}),(0,J.jsx)(`p`,{className:`mt-2 text-sm text-gray-600`,children:`Loading form...`})]});if(e.sections?.length){let t=[];return e.sections.forEach((e,n)=>{let r=e.name||String(n),i=y[r]??e.collapsed??!1;t.push({name:`__section_${r}`,label:e.label||``,type:`section-divider`,colSpan:4,collapsible:e.collapsible,collapsed:i,onToggle:e.collapsible?()=>b(e=>({...e,[r]:!i})):void 0});let a=w(e);i?t.push(...a.map(e=>({...e,hidden:!0}))):t.push(...a)}),(0,J.jsx)(i,{schema:{...A,fields:t}})}let t=Q(s,a,e.columns,e.mode),n=Je[t.columns||1];return(0,J.jsx)(i,{schema:{...A,fields:t.fields,columns:t.columns,...n?{fieldContainerClass:n}:{}}})},M=e.subforms,N=M?.length&&e.mode!==`view`?(0,J.jsx)(Z,{schema:{type:`object-master-detail-form`,objectName:e.objectName,mode:e.mode===`edit`?`edit`:`create`,recordId:e.recordId,fields:e.fields,sections:e.sections,submitText:e.submitText,details:M,onSuccess:async t=>{await e.onSuccess?.(t),e.onOpenChange?.(!1)},onError:e.onError,onCancel:()=>e.onOpenChange?.(!1)},dataSource:t}):j();return(0,J.jsx)(x,{open:_,onOpenChange:e.onOpenChange,children:(0,J.jsxs)(S,{side:v,className:E(`overflow-y-auto`,n,e.className),style:k,children:[(e.title||e.description)&&(0,J.jsxs)(p,{children:[e.title&&(0,J.jsx)(g,{children:e.title}),e.description?(0,J.jsx)(C,{children:e.description}):(0,J.jsx)(C,{className:`sr-only`,children:`Complete the form fields, then submit or cancel.`})]}),(0,J.jsx)(`div`,{className:`@container py-4`,children:N})]})})},Xe={sm:`sm:max-w-sm`,default:`sm:max-w-lg`,lg:`sm:max-w-2xl`,xl:`sm:max-w-5xl`,full:`sm:max-w-[95vw] sm:w-full`},Ze={1:void 0,2:`grid gap-4 grid-cols-1 @md:grid-cols-2`,3:`grid gap-4 grid-cols-1 @md:grid-cols-2 @2xl:grid-cols-3`,4:`grid gap-4 grid-cols-1 @md:grid-cols-2 @2xl:grid-cols-3 @4xl:grid-cols-4`},Qe=({schema:e,dataSource:t,className:n})=>{let{fieldLabel:a}=O(),o=r(),s=(0,q.useCallback)(t=>{if(!o?.isLoaded)return t;let n=[];for(let r of t){if(!r?.name){n.push(r);continue}o.checkField(e.objectName,r.name,`read`)&&(!o.checkField(e.objectName,r.name,`write`)&&e.mode!==`view`?n.push({...r,readOnly:!0,disabled:!0}):n.push(r))}return n},[o,e.objectName,e.mode]),[c,p]=(0,q.useState)(null),[m,h]=(0,q.useState)([]),[g,_]=(0,q.useState)({}),[v,y]=(0,q.useState)(!0),[x,S]=(0,q.useState)(null),[C,w]=(0,q.useState)(!1),T=e.open!==!1,D=(0,q.useId)(),k=(0,q.useMemo)(()=>e.sections?.length||e.customFields?.length?null:Q(m,c,e.columns,e.mode),[m,c,e.columns,e.mode,e.sections,e.customFields]),A=Xe[(0,q.useMemo)(()=>{if(e.modalSize)return e.modalSize;if(k?.columns&&k.columns>1)return We(k.columns);if(e.sections?.length){let t=Math.max(...e.sections.map(e=>Number(e.columns)||1));if(t>1)return We(t)}return`default`},[e.modalSize,k,e.sections])]||Xe.default;(0,q.useEffect)(()=>{(async()=>{if(!t){y(!1);return}try{p(await t.getObjectSchema(e.objectName))}catch(e){S(e),y(!1)}})()},[e.objectName,t]),(0,q.useEffect)(()=>{(c||!t)&&(async()=>{if(e.mode===`create`||!e.recordId){_(e.initialData||e.initialValues||{}),y(!1);return}if(!t){_(e.initialData||e.initialValues||{}),y(!1);return}try{_(await t.findOne(e.objectName,e.recordId)||{})}catch(e){S(e)}finally{y(!1)}})()},[c,e.mode,e.recordId,e.initialData,e.initialValues,t,e.objectName]);let j=(0,q.useCallback)(t=>{let n=[],r=(e,t)=>typeof t==`string`&&t.trim()?{...e,visible:e=>I(t,e)}:e;for(let i of t.fields){let t=typeof i==`string`?i:i.name;if(typeof i==`object`)n.push(r(i,i.visibleOn));else if(c?.fields?.[t]){let i=c.fields[t];n.push(r({name:t,label:a(e.objectName,t,i.label||t),type:z(i.type),required:i.required||!1,disabled:e.readOnly||e.mode===`view`||i.readonly,placeholder:i.placeholder,description:i.help||i.description,validation:W(i),field:i,options:i.options,multiple:i.multiple},i.visible_on??i.visibleOn))}else n.push({name:t,label:t,type:`input`})}return n},[c,e.readOnly,e.mode,e.objectName]);(0,q.useEffect)(()=>{if(!c&&t)return;if(e.customFields?.length){h(e.customFields),y(!1);return}if(e.sections?.length){y(!1);return}if(!c)return;let n=e.fields||Object.keys(c.fields||{}),r=[];for(let t of n){let n=typeof t==`string`?t:t.name;if(!n)continue;let i=c.fields?.[n];i&&r.push({name:n,label:a(e.objectName,n,i.label||n),type:z(i.type),required:i.required||!1,disabled:e.readOnly||e.mode===`view`||i.readonly,placeholder:i.placeholder,description:i.help||i.description,validation:W(i),field:i,options:i.options,multiple:i.multiple})}h(r),y(!1)},[c,e.fields,e.customFields,e.sections,e.readOnly,e.mode,t]);let M=(0,q.useCallback)(async n=>{w(!0);try{if(!t)return e.onSuccess&&await e.onSuccess(n),e.onOpenChange?.(!1),n;let r,i=qe(n,c);if(o?.isLoaded){let t={};for(let n of Object.keys(i))o.checkField(e.objectName,n,`write`)&&(t[n]=i[n]);i=t}return e.mode===`create`?r=await t.create(e.objectName,i):e.mode===`edit`&&e.recordId&&(r=await t.update(e.objectName,e.recordId,i)),e.onSuccess&&await e.onSuccess(r),e.onOpenChange?.(!1),r}catch(t){throw e.onError&&e.onError(t),t}finally{w(!1)}},[e,t,c,o]),N=(0,q.useCallback)(()=>{e.onCancel&&e.onCancel(),e.onOpenChange?.(!1)},[e]),P=e.layout===`vertical`||e.layout===`horizontal`?e.layout:`vertical`,L=e.showSubmit!==!1&&e.mode!==`view`,R=e.showCancel!==!1,V=e.submitText||(e.mode===`create`?`Create`:`Update`),H=e.cancelText||`Cancel`,U={type:`form`,layout:P,defaultValues:g,submitLabel:V,cancelLabel:H,showSubmit:L,showCancel:R,onSubmit:M,onCancel:N,showActions:!1,id:D},ee=()=>{if(x)return(0,J.jsxs)(`div`,{className:`p-4 border border-red-300 bg-red-50 rounded-md`,children:[(0,J.jsx)(`h3`,{className:`text-red-800 font-semibold`,children:`Error loading form`}),(0,J.jsx)(`p`,{className:`text-red-600 text-sm mt-1`,children:x.message})]});if(v)return(0,J.jsx)(`div`,{className:`space-y-4`,"data-testid":`modal-form-skeleton`,children:[1,2,3].map(e=>(0,J.jsxs)(`div`,{className:`space-y-2`,children:[(0,J.jsx)(l,{className:`h-4 w-24`}),(0,J.jsx)(l,{className:`h-10 w-full`})]},e))});if(e.sections?.length)return(0,J.jsx)(`div`,{className:`space-y-6`,children:e.sections.map((e,t)=>{let n=e.columns||1;return(0,J.jsx)(Y,{label:e.label,description:e.description,columns:n,gridClassName:Ze[n],children:(0,J.jsx)(i,{schema:{...U,fields:s(j(e))}})},e.name||e.label||t)})});let t=k??Q(m,c,e.columns,e.mode),n=Ze[t.columns||1];return(0,J.jsx)(i,{schema:{...U,fields:s(t.fields),columns:t.columns,...n?{fieldContainerClass:n}:{}}})},G=e.subforms;if(G?.length&&e.mode!==`view`)return(0,J.jsx)(f,{open:T,onOpenChange:e.onOpenChange,children:(0,J.jsxs)(F,{className:E(A,`flex flex-col h-[100dvh] sm:h-auto sm:max-h-[90vh] overflow-hidden p-0`,n,e.className),children:[(e.title||e.description)&&(0,J.jsxs)(te,{className:`shrink-0 px-4 pt-4 sm:px-6 sm:pt-6 pb-2 border-b`,children:[e.title&&(0,J.jsx)(u,{children:e.title}),e.description?(0,J.jsx)(b,{children:e.description}):(0,J.jsx)(b,{className:`sr-only`,children:`Enter the record and its line items, then save.`})]}),(0,J.jsx)(`div`,{className:`@container flex-1 overflow-y-auto px-4 sm:px-6 py-4`,children:(0,J.jsx)(Z,{schema:{type:`object-master-detail-form`,objectName:e.objectName,mode:e.mode===`edit`?`edit`:`create`,recordId:e.recordId,fields:e.fields,sections:e.sections,submitText:V,details:G,onSuccess:async t=>{await e.onSuccess?.(t),e.onOpenChange?.(!1)},onError:e.onError,onCancel:()=>e.onOpenChange?.(!1)},dataSource:t})})]})});let K=!v&&!x&&(L||R);return(0,J.jsx)(f,{open:T,onOpenChange:e.onOpenChange,children:(0,J.jsxs)(F,{className:E(A,`flex flex-col h-[100dvh] sm:h-auto sm:max-h-[90vh] overflow-hidden p-0`,n,e.className),children:[(e.title||e.description)&&(0,J.jsxs)(te,{className:`shrink-0 px-4 pt-4 sm:px-6 sm:pt-6 pb-2 border-b`,children:[e.title&&(0,J.jsx)(u,{children:e.title}),e.description?(0,J.jsx)(b,{children:e.description}):(0,J.jsx)(b,{className:`sr-only`,children:`Complete the form fields, then submit or cancel.`})]}),(0,J.jsx)(`div`,{className:`@container flex-1 overflow-y-auto px-4 sm:px-6 py-4`,children:ee()}),K&&(0,J.jsx)(`div`,{className:`shrink-0 border-t px-4 sm:px-6 py-3 bg-background`,"data-testid":`modal-form-footer`,children:(0,J.jsxs)(`div`,{className:`flex flex-col sm:flex-row gap-2 sm:justify-end`,children:[R&&(0,J.jsx)(B,{type:`button`,variant:`outline`,onClick:N,disabled:C,className:`w-full sm:w-auto`,children:H}),L&&(0,J.jsxs)(B,{type:`submit`,form:D,disabled:C,className:`w-full sm:w-auto`,children:[C&&(0,J.jsx)(d,{className:`mr-2 h-4 w-4 animate-spin`}),V]})]})})]})})},$=({schema:e,dataSource:t})=>{let n=r(),i=(0,q.useMemo)(()=>{if(!n?.isLoaded)return e;let t=t=>t?.name?n.checkField(e.objectName,t.name,`read`)?!n.checkField(e.objectName,t.name,`write`)&&e.mode!==`view`?{...t,readOnly:!0,disabled:!0}:t:null:t,r=e=>Array.isArray(e)?e.map(t).filter(Boolean):e;return{...e,fields:r(e.fields),sections:e.sections?.map(e=>({...e,fields:r(e.fields)}))}},[e,n]),{sectionLabel:a}=O(),o=e=>e?.name?a(i.objectName,e.name,e.label||e.name):e?.label;if(i.subforms?.length&&i.mode!==`view`&&i.formType!==`drawer`&&i.formType!==`modal`)return(0,J.jsx)(Z,{schema:{type:`object-master-detail-form`,objectName:i.objectName,mode:i.mode===`edit`?`edit`:`create`,recordId:i.recordId,formType:i.formType===`tabbed`?`tabbed`:`simple`,sections:i.sections,fields:i.fields,title:i.title,submitText:i.submitText,details:i.subforms,onSuccess:i.onSuccess,onError:i.onError,onCancel:i.onCancel,className:i.className},dataSource:t});if(i.formType===`tabbed`&&i.sections?.length)return(0,J.jsx)(re,{schema:{...i,formType:`tabbed`,sections:i.sections.map(e=>({name:e.name,label:o(e),description:e.description,columns:e.columns,fields:e.fields})),defaultTab:i.defaultTab,tabPosition:i.tabPosition},dataSource:t,className:i.className});if(i.formType===`wizard`&&i.sections?.length)return(0,J.jsx)(ie,{schema:{...i,formType:`wizard`,sections:i.sections.map(e=>({name:e.name,label:o(e),description:e.description,columns:e.columns,fields:e.fields})),allowSkip:i.allowSkip,showStepIndicator:i.showStepIndicator,nextText:i.nextText,prevText:i.prevText,onStepChange:i.onStepChange},dataSource:t,className:i.className});if(i.formType===`split`&&i.sections?.length)return(0,J.jsx)(ae,{schema:{...i,formType:`split`,sections:i.sections.map(e=>({name:e.name,label:o(e),description:e.description,columns:e.columns,fields:e.fields})),splitDirection:i.splitDirection,splitSize:i.splitSize,splitResizable:i.splitResizable},dataSource:t,className:i.className});if(i.formType===`drawer`){let{layout:e,...n}=i,r=i.layout===`vertical`||i.layout===`horizontal`?i.layout:void 0;return(0,J.jsx)(Ye,{schema:{...n,layout:r,formType:`drawer`,sections:i.sections?.map(e=>({name:e.name,label:o(e),description:e.description,columns:e.columns,fields:e.fields,collapsible:e.collapsible,collapsed:e.collapsed})),open:i.open,onOpenChange:i.onOpenChange,drawerSide:i.drawerSide,drawerWidth:i.drawerWidth},dataSource:t,className:i.className})}if(i.formType===`modal`){let{layout:e,...n}=i,r=i.layout===`vertical`||i.layout===`horizontal`?i.layout:void 0;return(0,J.jsx)(Qe,{schema:{...n,layout:r,formType:`modal`,sections:i.sections?.map(e=>({name:e.name,label:o(e),description:e.description,columns:e.columns,fields:e.fields})),open:i.open,onOpenChange:i.onOpenChange,modalSize:i.modalSize,modalCloseButton:i.modalCloseButton},dataSource:t,className:i.className})}return(0,J.jsx)($e,{schema:i,dataSource:t})},$e=({schema:e,dataSource:t})=>{let{fieldLabel:n,sectionLabel:a}=O(),o=P(),s=r(),c=(0,q.useCallback)(t=>{if(!s?.isLoaded)return t;let n=[];for(let r of t)s.checkField(e.objectName,r.name,`read`)&&(!s.checkField(e.objectName,r.name,`write`)&&e.mode!==`view`?n.push({...r,readOnly:!0,disabled:!0,description:r.description??`You do not have edit access to this field.`}):n.push(r));return n},[s,e.objectName,e.mode]),[l,u]=(0,q.useState)(null),[d,f]=(0,q.useState)([]),[p,m]=(0,q.useState)(null),[h,g]=(0,q.useState)(!0),[_,v]=(0,q.useState)(null),y=e.customFields&&e.customFields.length>0;(0,q.useEffect)(()=>{y&&(m(e.initialData||e.initialValues||{}),g(!1))},[y,e.initialData,e.initialValues]),(0,q.useEffect)(()=>{y?u({name:e.objectName,fields:{}}):e.objectName&&t?(async()=>{try{if(!t)throw Error(`DataSource is required when using ObjectQL schema fetching (inline fields not provided)`);let n=await t.getObjectSchema(e.objectName);if(!n)throw Error(`No schema found for object "${e.objectName}"`);u(n)}catch(e){v(e),g(!1)}})():y||g(!1)},[e.objectName,t,y]),(0,q.useEffect)(()=>{l&&!y&&(async()=>{if(!e.recordId||e.mode===`create`){m(e.initialData||e.initialValues||{}),g(!1);return}if(!y){if(!t){v(Error(`DataSource is required for fetching record data (inline data not provided)`)),g(!1);return}g(!0);try{m(await t.findOne(e.objectName,e.recordId))}catch(e){console.error(`Failed to fetch record:`,e),v(e)}finally{g(!1)}}})()},[e.objectName,e.recordId,e.mode,e.initialValues,e.initialData,t,l,y]);let b=(0,q.useCallback)(e=>{if(!e||typeof e.visible==`function`)return e;let t=e.visibleOn;return typeof t==`string`&&t.trim()?{...e,visible:e=>I(t,e)}:e},[]);(0,q.useEffect)(()=>{if(y&&e.customFields){f(e.customFields.map(b)),g(!1);return}if(!l)return;let r=[],i=e.fields||Object.keys(l.fields||{});(Array.isArray(i)?i:Object.keys(i)).forEach(t=>{let i=typeof t==`string`?t:t.name;if(!i)return;let a=l.fields?.[i];if(!a&&!y)return;let o=!a?.permissions||a?.permissions.write!==!1;if(e.mode!==`view`&&!o)return;let s=e.customFields?.find(e=>e.name===i);if(s)r.push(b(s));else if(a){let o={name:i,label:n(e.objectName,i,a.label||t),type:z(a.type),required:a.required||!1,disabled:e.readOnly||e.mode===`view`||a.readonly||l?.managedBy&&l.managedBy!==`platform`,placeholder:a.placeholder,description:a.help||a.description,validation:W(a),visibleWhen:a.visibleWhen,readonlyWhen:a.readonlyWhen,requiredWhen:a.requiredWhen??a.conditionalRequired,field:a};if((a.type===`select`||a.type===`lookup`||a.type===`master_detail`)&&(o.options=a.options||[],o.multiple=a.multiple),(a.type===`number`||a.type===`currency`||a.type===`percent`)&&(o.inputType=`number`,o.min=a.min,o.max=a.max,o.step=a.precision?10**-a.precision:void 0),a.type===`date`&&(o.inputType=`date`),a.type===`datetime`&&(o.inputType=`datetime-local`),(a.type===`text`||a.type===`textarea`||a.type===`markdown`||a.type===`html`)&&(o.maxLength=a.max_length,o.minLength=a.min_length),(a.type===`file`||a.type===`image`)&&(o.inputType=`file`,o.multiple=a.multiple,o.accept=a.accept?a.accept.join(`,`):void 0,a.max_size)){let e=`Max size: ${R(a.max_size)}`;o.description=o.description?`${o.description} (${e})`:e}a.type===`email`&&(o.inputType=`email`),a.type===`phone`&&(o.inputType=`tel`),a.type===`url`&&(o.inputType=`url`),a.type===`password`&&(o.inputType=`password`),a.type===`time`&&(o.inputType=`time`),(a.type===`formula`||a.type===`summary`||a.type===`auto_number`)&&(o.disabled=!0),a.visible_on&&(o.visible=e=>I(a.visible_on,e)),r.push(o)}}),f(r),!y&&e.recordId&&e.mode!==`create`&&t||g(!1)},[l,e.fields,e.customFields,e.readOnly,e.mode,y,e.recordId,t]);let x=(0,q.useCallback)(async(n,r)=>{if(n&&(n.nativeEvent||n._reactName===`onSubmit`)&&(console.warn(`ObjectForm: Received Event instead of data in handleSubmit! This suggests a Form renderer issue.`),r===void 0&&(r=n,n={})),y&&!t)return e.onSuccess&&await e.onSuccess(n),n;if(!t)throw Error(`DataSource is required for form submission (inline mode not configured)`);let i=n;if(s?.isLoaded&&n&&typeof n==`object`){i={};for(let t of Object.keys(n))s.checkField(e.objectName,t,`write`)&&(i[t]=n[t])}try{let n;if(e.submitHandler)n=await e.submitHandler(i);else if(e.mode===`create`)n=await t.create(e.objectName,i);else if(e.mode===`edit`&&e.recordId)n=await t.update(e.objectName,e.recordId,i);else throw Error(`Invalid form mode or missing record ID`);return e.onSuccess&&await e.onSuccess(n),n}catch(t){throw console.error(`Failed to submit form:`,t),e.onError&&e.onError(t),t}},[e,t,y,s]),S=(0,q.useCallback)(()=>{e.onCancel&&e.onCancel()},[e]),C={...q.useMemo(()=>{if(!l?.fields)return{};let e={};return Object.keys(l.fields).forEach(t=>{let n=l.fields[t];n.defaultValue!==void 0&&(e[t]=n.defaultValue)}),e},[l]),...p};if(_)return(0,J.jsxs)(`div`,{className:`p-3 sm:p-4 border border-red-300 bg-red-50 rounded-md`,children:[(0,J.jsx)(`h3`,{className:`text-red-800 font-semibold`,children:`Error loading form`}),(0,J.jsx)(`p`,{className:`text-red-600 text-sm mt-1`,children:_.message})]});if(h)return(0,J.jsxs)(`div`,{className:`p-4 sm:p-8 text-center`,children:[(0,J.jsx)(`div`,{className:`inline-block animate-spin rounded-full h-8 w-8 border-b-2 border-gray-900`}),(0,J.jsx)(`p`,{className:`mt-2 text-sm text-gray-600`,children:`Loading form...`})]});let w=e.layout===`vertical`||e.layout===`horizontal`?e.layout:`vertical`;if(e.sections?.length&&(!e.formType||e.formType===`simple`))return(0,J.jsx)(`div`,{className:`w-full space-y-6`,children:e.sections.map((t,n)=>{let r=t.fields.map(e=>typeof e==`string`?e:e.name),o=c(d.filter(e=>r.includes(e.name)));return(0,J.jsx)(Y,{label:t.name?a(e.objectName,t.name,t.label||t.name):t.label,description:t.description,collapsible:t.collapsible,collapsed:t.collapsed,columns:t.columns,showBorder:t.showBorder,children:(0,J.jsx)(i,{schema:{type:`form`,fields:o,layout:w,defaultValues:C,showSubmit:n===e.sections.length-1&&e.showSubmit!==!1&&e.mode!==`view`,showCancel:n===e.sections.length-1&&e.showCancel!==!1,submitLabel:e.submitText||(e.mode===`create`?`Create`:`Update`),cancelLabel:e.cancelText,onSubmit:x,onCancel:S}})},t.name||t.label||n)})});let T=e.sections?.length,E=c(d),D=T?{fields:E,columns:e.columns}:Q(E,l,e.columns,e.mode),k=e.mobile,A=k?.fullscreenLongText?D.fields.map(e=>{let t=e.type;return t===`textarea`||t===`field:textarea`||t===`string-multiline`||t===`field:markdown`||t===`field:html`?{...e,mobile_fullscreen:!0}:e}):D.fields,j=k?.stepper,M=k?.stepperMinFields??8,N=Math.max(1,k?.stepperFieldsPerStep??1);if(!e.formType&&!T&&A.length>=2&&(j===!0||j===`auto`&&o&&A.length>=M)){let n=A,r=[];for(let e=0;e<n.length;e+=N){let t=n.slice(e,e+N);r.push({name:`step-${Math.floor(e/N)+1}`,label:t[0]?.label||t[0]?.name||`Step ${Math.floor(e/N)+1}`,fields:t})}return(0,J.jsx)(ie,{schema:{...e,formType:`wizard`,sections:r,showStepIndicator:!0},dataSource:t,className:e.className})}let F={type:`form`,fields:A,layout:w,columns:D.columns,submitLabel:e.submitText||(e.mode===`create`?`Create`:`Update`),cancelLabel:e.cancelText,showSubmit:e.showSubmit!==!1&&e.mode!==`view`,showCancel:e.showCancel!==!1,resetOnSubmit:e.showReset,defaultValues:C,onSubmit:x,onCancel:S,className:e.className,mobileStickyActions:!!k?.stickyActions};return(0,J.jsx)(`div`,{className:k?.stickyActions?`w-full pb-20 md:pb-0`:`w-full`,"data-mobile-form":k?`true`:void 0,children:(0,J.jsx)(i,{schema:F})})},et={text:200,email:254,url:2048,phone:32,textarea:5e3,markdown:5e3,html:5e3},tt=`_company_website_2`,nt=1500;function rt(e,t=[]){try{let n=new URL(e,typeof window<`u`?window.location.href:`http://localhost`);return typeof window<`u`&&n.origin===window.location.origin?!0:n.protocol!==`https:`&&n.protocol!==`http:`?!1:t.some(e=>{if(e===n.host)return!0;if(e.startsWith(`*.`)){let t=e.slice(1);return n.host.endsWith(t)&&n.host.length>t.length}return!1})}catch{return!1}}function it(e){return e&&e.map(e=>{let t=et[String(e.type||``).toLowerCase()];return!t||(e.maxLength??e.max_length)?e:{...e,maxLength:t}})}var at=({config:e,dataSource:t,prefillParams:n,className:r})=>{let[i,a]=(0,q.useState)(!1),[o,s]=(0,q.useState)(!1),[c,l]=(0,q.useState)(null),[u,f]=(0,q.useState)(!(e.consent?.required??!!e.consent)),[p,m]=(0,q.useState)(null),h=(0,q.useRef)(null),g=(0,q.useRef)(Date.now());(0,q.useEffect)(()=>{i||(g.current=Date.now())},[i]);let _=e.honeypot===!1?null:e.honeypot||tt,v=e.minFillTime??nt,y=(0,q.useMemo)(()=>it(e.customFields),[e.customFields]),b=(0,q.useMemo)(()=>{if(!t||y&&y.length>0||!e.fields||e.fields.length===0)return;let n=async(e,t)=>t;return{...t,create:n,update:(e,t,n)=>Promise.resolve(n),delete:()=>Promise.reject(Error(`Not permitted on public form`))}},[t,y,e.fields]),x=(0,q.useMemo)(()=>{let t={};if(n)for(let[e,r]of Object.entries(n))t[e]=r;if(typeof window<`u`&&e.allowedPrefillFields?.length){let n=new Set(e.allowedPrefillFields);new URLSearchParams(window.location.search).forEach((e,r)=>{n.has(r)&&!(r in t)&&(t[r]=e)})}return Object.keys(t).length>0?t:void 0},[n,e.allowedPrefillFields]),S=(0,q.useCallback)(async n=>{if(e.consent?.required&&!u){m(e.texts?.consentRequired??`Please accept the privacy policy to continue.`);return}if(m(null),_&&h.current?.value){a(!0);return}if(v>0&&Date.now()-g.current<v){l(e.texts?.rateLimited??`Please take a moment to review your answers before submitting.`);return}let r={...n};_&&delete r[_],e.captchaToken&&(r._captcha=e.captchaToken),s(!0),l(null);try{t&&await t.create(e.objectName,r),a(!0);let n=e.thankYouPage?.redirectUrl;if(n)if(rt(n,e.allowedRedirectHosts)){let t=e.thankYouPage?.redirectDelay??3e3;setTimeout(()=>{window.location.href=n},t)}else console.warn(`[EmbeddableForm] Blocked unsafe redirect target:`,n),l(e.texts?.redirectBlocked??null)}catch(e){l(e instanceof Error?e.message:`Failed to submit form. Please try again.`)}finally{s(!1)}},[t,e,u,_,v]),C=(0,q.useCallback)(()=>{a(!1),l(null)},[]),T=(0,q.useMemo)(()=>{let t={};return e.branding?.backgroundColor&&(t.backgroundColor=e.branding.backgroundColor),t},[e.branding]);if(i){let t=e.thankYouPage,n=e.texts??{},i=Math.ceil((t?.redirectDelay??3e3)/1e3),a=(n.redirecting??`Redirecting in {{seconds}} seconds…`).replace(`{{seconds}}`,String(i));return(0,J.jsx)(`div`,{className:`min-h-screen flex items-center justify-center p-4 bg-gradient-to-b from-muted/40 via-background to-background ${r||``}`,style:T,children:(0,J.jsxs)(`div`,{className:`max-w-md w-full bg-card border rounded-xl shadow-sm p-8 text-center space-y-4`,children:[(0,J.jsx)(`div`,{className:`mx-auto inline-flex h-14 w-14 items-center justify-center rounded-full bg-emerald-50 text-emerald-600 dark:bg-emerald-500/10 dark:text-emerald-400`,children:(0,J.jsx)(N,{className:`h-8 w-8`,"aria-hidden":`true`})}),(0,J.jsx)(`h2`,{className:`text-xl font-semibold text-foreground`,children:t?.title||n.thankYouTitle||`Thank You!`}),(0,J.jsx)(`p`,{className:`text-sm text-muted-foreground`,children:t?.message||n.thankYouMessage||`Your submission has been received successfully.`}),e.allowMultiple&&(0,J.jsx)(B,{variant:`outline`,size:`sm`,onClick:C,className:`mt-2`,children:n.submitAnother??`Submit Another Response`}),t?.redirectUrl&&(0,J.jsx)(`p`,{className:`text-xs text-muted-foreground`,children:a})]})})}let E=e.texts??{},D=!!e.consent?.required,O=`${e.formId}-consent`;return(0,J.jsx)(`div`,{className:`min-h-screen flex items-center justify-center p-4 sm:p-6 bg-gradient-to-b from-muted/40 via-background to-background ${r||``}`,style:T,children:(0,J.jsxs)(`div`,{className:`max-w-2xl w-full bg-card border rounded-xl shadow-sm overflow-hidden`,children:[e.branding?.coverImage&&(0,J.jsx)(`div`,{className:`h-28 sm:h-32 w-full bg-cover bg-center`,style:{backgroundImage:`url(${JSON.stringify(e.branding.coverImage)})`},role:`presentation`}),(0,J.jsxs)(`div`,{className:`px-6 sm:px-8 pt-7 pb-5 border-b bg-muted/20`,style:e.branding?.primaryColor?{borderBottomColor:e.branding.primaryColor}:void 0,children:[e.branding?.logo&&(0,J.jsx)(`img`,{src:e.branding.logo,alt:``,className:`h-8 mb-4`}),e.title&&(0,J.jsx)(`h1`,{className:`text-2xl font-semibold tracking-tight text-foreground`,children:e.title}),e.description&&(0,J.jsx)(`p`,{className:`text-sm text-muted-foreground mt-2 leading-relaxed`,children:e.description}),(0,J.jsx)(`p`,{className:`text-[11px] text-muted-foreground/70 mt-3`,"aria-hidden":`true`,children:E.requiredHint??`* Required field`})]}),(0,J.jsxs)(`div`,{className:`relative px-6 sm:px-8 py-6`,children:[c&&(0,J.jsx)(`div`,{role:`alert`,"aria-live":`assertive`,className:`mb-4 p-3 bg-destructive/10 border border-destructive/30 rounded-md text-sm text-destructive`,children:c}),(0,J.jsx)($,{schema:{type:`object-form`,objectName:e.objectName,mode:`create`,fields:e.fields,customFields:y,initialData:x,onSuccess:S,submitText:o?E.submitting??`Submitting...`:E.submit??`Submit`},dataSource:b}),_&&(0,J.jsxs)(`div`,{"aria-hidden":`true`,className:`absolute left-[-10000px] top-auto h-px w-px overflow-hidden`,children:[(0,J.jsx)(`label`,{htmlFor:`${e.formId}-${_}`,children:`Do not fill this field`}),(0,J.jsx)(`input`,{ref:h,id:`${e.formId}-${_}`,type:`text`,name:_,tabIndex:-1,autoComplete:`off`,defaultValue:``})]}),e.consent&&(0,J.jsxs)(`div`,{className:`mt-4`,children:[(0,J.jsxs)(`label`,{className:`flex items-start gap-2 text-sm text-foreground cursor-pointer select-none`,children:[(0,J.jsx)(`input`,{type:`checkbox`,id:O,checked:u,onChange:e=>{f(e.target.checked),e.target.checked&&m(null)},"aria-required":D||void 0,"aria-invalid":p?`true`:void 0,"aria-describedby":p?`${O}-error`:void 0,className:`mt-0.5 h-4 w-4 rounded border-input accent-primary`}),(0,J.jsxs)(`span`,{className:`leading-relaxed`,children:[e.consent.label??E.consentLabelDefault??`I agree to the privacy policy.`,D&&(0,J.jsx)(`span`,{"aria-hidden":`true`,className:`text-destructive ml-0.5`,children:`*`}),e.consent.privacyUrl&&(0,J.jsxs)(J.Fragment,{children:[` `,(0,J.jsx)(`a`,{href:e.consent.privacyUrl,target:`_blank`,rel:`noopener noreferrer`,className:`underline text-primary hover:opacity-80`,children:E.consentLink??`Privacy policy`})]})]})]}),p&&(0,J.jsx)(`p`,{id:`${O}-error`,role:`alert`,className:`mt-1.5 text-xs text-destructive`,children:p})]}),o&&(0,J.jsxs)(`div`,{className:`mt-3 flex items-center gap-2 text-xs text-muted-foreground`,children:[(0,J.jsx)(d,{className:`h-3.5 w-3.5 animate-spin`,"aria-hidden":`true`}),(0,J.jsx)(`span`,{children:E.submitting??`Submitting…`})]})]}),(0,J.jsxs)(`div`,{className:`px-6 sm:px-8 py-4 border-t bg-muted/20 flex flex-col sm:flex-row items-center justify-between gap-2`,children:[(0,J.jsxs)(`p`,{className:`inline-flex items-center gap-1.5 text-xs text-muted-foreground`,children:[(0,J.jsx)(w,{className:`h-3 w-3`,"aria-hidden":`true`}),(0,J.jsx)(`span`,{children:E.secureNotice??`Your information is transmitted securely and only used to respond to your request.`})]}),(0,J.jsxs)(`div`,{className:`flex items-center gap-3 text-xs text-muted-foreground/80`,children:[e.privacyPolicyUrl&&(0,J.jsxs)(`a`,{href:e.privacyPolicyUrl,target:`_blank`,rel:`noopener noreferrer`,className:`inline-flex items-center gap-1 underline hover:text-foreground`,children:[(0,J.jsx)(A,{className:`h-3 w-3`,"aria-hidden":`true`}),E.consentLink??`Privacy policy`]}),(0,J.jsx)(`span`,{children:E.poweredBy??`Powered by ObjectStack`})]})]})]})})};function ot(e){return e<60?`${Math.round(e)}s`:e<3600?`${Math.floor(e/60)}m ${Math.round(e%60)}s`:`${Math.floor(e/3600)}h ${Math.floor(e%3600/60)}m`}var st=({formId:e,formTitle:t,metrics:n,className:r})=>{let i=(0,q.useMemo)(()=>{if(!n.abandonedSubmissions)return 100;let e=n.totalSubmissions+n.abandonedSubmissions;return e===0?0:Math.round(n.totalSubmissions/e*100)},[n.totalSubmissions,n.abandonedSubmissions]),a=(0,q.useMemo)(()=>n.dailySubmissions?.length?Math.max(...n.dailySubmissions.map(e=>e.count),1):1,[n.dailySubmissions]);return(0,J.jsxs)(`div`,{className:`space-y-4 ${r||``}`,children:[(0,J.jsxs)(`div`,{children:[(0,J.jsx)(`h2`,{className:`text-lg font-semibold text-foreground`,children:t||`Form Analytics`}),(0,J.jsxs)(`p`,{className:`text-sm text-muted-foreground`,children:[`Form ID: `,e]})]}),(0,J.jsxs)(`div`,{className:`grid grid-cols-1 sm:grid-cols-3 gap-4`,children:[(0,J.jsx)(m,{children:(0,J.jsxs)(_,{className:`pb-2`,children:[(0,J.jsx)(H,{children:`Total Submissions`}),(0,J.jsx)(T,{className:`text-2xl`,children:n.totalSubmissions.toLocaleString()})]})}),(0,J.jsxs)(m,{children:[(0,J.jsxs)(_,{className:`pb-2`,children:[(0,J.jsx)(H,{children:`Fill Rate`}),(0,J.jsxs)(T,{className:`text-2xl`,children:[i,`%`]})]}),(0,J.jsx)(v,{children:(0,J.jsx)(`div`,{className:`w-full bg-muted rounded-full h-2`,children:(0,J.jsx)(`div`,{className:`bg-primary rounded-full h-2 transition-all`,style:{width:`${i}%`}})})})]}),(0,J.jsx)(m,{children:(0,J.jsxs)(_,{className:`pb-2`,children:[(0,J.jsx)(H,{children:`Avg. Completion Time`}),(0,J.jsx)(T,{className:`text-2xl`,children:n.avgCompletionTime?ot(n.avgCompletionTime):`—`})]})})]}),n.dailySubmissions&&n.dailySubmissions.length>0&&(0,J.jsxs)(m,{children:[(0,J.jsx)(_,{children:(0,J.jsx)(T,{className:`text-sm font-medium`,children:`Daily Submissions`})}),(0,J.jsx)(v,{children:(0,J.jsx)(`div`,{className:`flex items-end gap-1 h-32`,children:n.dailySubmissions.map((e,t)=>(0,J.jsxs)(`div`,{className:`flex-1 flex flex-col items-center gap-1`,children:[(0,J.jsx)(`div`,{className:`w-full bg-primary/80 rounded-t transition-all hover:bg-primary`,style:{height:`${e.count/a*100}%`,minHeight:e.count>0?`4px`:`0`},title:`${e.date}: ${e.count} submissions`}),(0,J.jsx)(`span`,{className:`text-[10px] text-muted-foreground truncate w-full text-center`,children:e.date.slice(-5)})]},t))})})]}),n.fieldDropOff&&n.fieldDropOff.length>0&&(0,J.jsxs)(m,{children:[(0,J.jsxs)(_,{children:[(0,J.jsx)(T,{className:`text-sm font-medium`,children:`Field Completion Rates`}),(0,J.jsx)(H,{children:`Percentage of users who completed each field`})]}),(0,J.jsx)(v,{children:(0,J.jsx)(`div`,{className:`space-y-3`,children:n.fieldDropOff.map((e,t)=>(0,J.jsxs)(`div`,{className:`space-y-1`,children:[(0,J.jsxs)(`div`,{className:`flex justify-between text-sm`,children:[(0,J.jsx)(`span`,{className:`text-foreground`,children:e.label}),(0,J.jsxs)(`span`,{className:`text-muted-foreground`,children:[e.completionRate,`%`]})]}),(0,J.jsx)(`div`,{className:`w-full bg-muted rounded-full h-1.5`,children:(0,J.jsx)(`div`,{className:`rounded-full h-1.5 transition-all ${e.completionRate>=80?`bg-green-500`:e.completionRate>=50?`bg-yellow-500`:`bg-destructive`}`,style:{width:`${e.completionRate}%`}})})]},t))})})]})]})},ct=({schema:e})=>{let t=a()?.dataSource,n;try{n=s()}catch{n=void 0}let r=e.parentObject||n?.objectName,i=e.parentId||e.recordId||n?.recordId,[o,c]=(0,q.useState)([]),[l,u]=(0,q.useState)([]),[d,f]=(0,q.useState)(!0),[p,h]=(0,q.useState)(!1),[g,y]=(0,q.useState)(!1),[b,x]=(0,q.useState)(null),S=(0,q.useCallback)(async()=>{if(!t||!i){f(!1);return}f(!0);try{let n=(await t.find(e.childObject,{$filter:{[e.relationshipField]:i},$top:500}))?.data??[];c(n.map(e=>({...e}))),u(n.map(e=>({...e}))),y(!1)}catch(e){x(e?.message||`Failed to load line items`)}finally{f(!1)}},[t,i,e.childObject,e.relationshipField]);(0,q.useEffect)(()=>{S()},[S]);let C=(0,q.useCallback)(e=>{c(e),y(!0)},[]),w=(0,q.useCallback)(async()=>{if(!(!t||!i)){h(!0),x(null);try{await me(t,r||e.childObject,i,{childObject:e.childObject,relationshipField:e.relationshipField,rows:o,original:l,amountField:e.amountField,totalField:r?e.totalField:void 0}),await S()}catch(e){x(e?.message||`Failed to save line items`)}finally{h(!1)}}},[t,i,o,l,e,r,S]),D=(0,q.useMemo)(()=>({columns:e.columns,total_field:e.totalField?e.amountField||`amount`:void 0,min_rows:e.minRows,max_rows:e.maxRows,allow_add:!e.readonly,allow_delete:!e.readonly}),[e]);return(0,J.jsxs)(m,{className:E(`shadow-none`),children:[(0,J.jsxs)(_,{className:`flex-row items-center justify-between gap-2 pb-2`,children:[(0,J.jsx)(T,{className:`text-sm font-medium`,children:e.title||`Line Items`}),!e.readonly&&(0,J.jsx)(B,{type:`button`,size:`sm`,onClick:w,disabled:p||d||!g||!i,children:p?`Saving…`:`Save`})]}),(0,J.jsxs)(v,{children:[b&&(0,J.jsx)(`p`,{className:`mb-2 text-sm text-destructive`,children:b}),d?(0,J.jsx)(`p`,{className:`py-6 text-center text-sm text-muted-foreground`,children:`Loading…`}):i?(0,J.jsx)(M,{value:o,onChange:C,field:D,readonly:e.readonly}):(0,J.jsx)(`p`,{className:`py-6 text-center text-sm text-muted-foreground`,children:`Save the record first to add line items.`})]})]})},lt=({schema:e})=>(0,J.jsx)($,{schema:e,dataSource:(0,q.useContext)(o)?.dataSource??void 0});c.register(`object-form`,lt,{namespace:`plugin-form`,label:`Object Form`,category:`plugin`,inputs:[{name:`objectName`,type:`string`,label:`Object Name`,required:!0},{name:`fields`,type:`array`,label:`Fields`},{name:`mode`,type:`enum`,label:`Mode`,enum:[`create`,`edit`,`view`]},{name:`formType`,type:`enum`,label:`Form Type`,enum:[`simple`,`tabbed`,`wizard`,`split`,`drawer`,`modal`]},{name:`sections`,type:`array`,label:`Sections`},{name:`title`,type:`string`,label:`Title`},{name:`description`,type:`string`,label:`Description`},{name:`layout`,type:`enum`,label:`Layout`,enum:[`vertical`,`horizontal`,`inline`,`grid`]},{name:`columns`,type:`number`,label:`Columns`},{name:`defaultTab`,type:`string`,label:`Default Tab`},{name:`tabPosition`,type:`enum`,label:`Tab Position`,enum:[`top`,`bottom`,`left`,`right`]},{name:`allowSkip`,type:`boolean`,label:`Allow Skip Steps`},{name:`showStepIndicator`,type:`boolean`,label:`Show Step Indicator`},{name:`splitDirection`,type:`enum`,label:`Split Direction`,enum:[`horizontal`,`vertical`]},{name:`splitSize`,type:`number`,label:`Split Panel Size (%)`},{name:`splitResizable`,type:`boolean`,label:`Split Resizable`},{name:`drawerSide`,type:`enum`,label:`Drawer Side`,enum:[`top`,`bottom`,`left`,`right`]},{name:`drawerWidth`,type:`string`,label:`Drawer Width`},{name:`modalSize`,type:`enum`,label:`Modal Size`,enum:[`sm`,`default`,`lg`,`xl`,`full`]}]}),c.register(`form`,lt,{namespace:`view`,skipFallback:!0,label:`Data Form View`,category:`view`,inputs:[{name:`objectName`,type:`string`,label:`Object Name`,required:!0},{name:`fields`,type:`array`,label:`Fields`},{name:`mode`,type:`enum`,label:`Mode`,enum:[`create`,`edit`,`view`]}]}),c.register(`embeddable-form`,({schema:e})=>(0,J.jsx)(at,{config:e}),{namespace:`plugin-form`,label:`Embeddable Form`,category:`plugin`,inputs:[{name:`formId`,type:`string`,label:`Form ID`,required:!0},{name:`objectName`,type:`string`,label:`Object Name`,required:!0},{name:`title`,type:`string`,label:`Form Title`},{name:`description`,type:`string`,label:`Description`},{name:`fields`,type:`array`,label:`Fields`},{name:`allowMultiple`,type:`boolean`,label:`Allow Multiple Submissions`}]}),c.register(`form-analytics`,({schema:e})=>(0,J.jsx)(st,{formId:e.formId,formTitle:e.formTitle,metrics:e.metrics||{totalSubmissions:0}}),{namespace:`plugin-form`,label:`Form Analytics`,category:`plugin`,inputs:[{name:`formId`,type:`string`,label:`Form ID`,required:!0},{name:`formTitle`,type:`string`,label:`Form Title`},{name:`metrics`,type:`object`,label:`Submission Metrics`}]}),c.register(`object-master-detail-form`,({schema:e})=>(0,J.jsx)(Z,{schema:e,dataSource:(0,q.useContext)(o)?.dataSource??void 0}),{namespace:`plugin-form`,label:`Master-Detail Form`,category:`plugin`,inputs:[{name:`objectName`,type:`string`,label:`Parent Object`,required:!0},{name:`mode`,type:`enum`,label:`Mode`,enum:[`create`,`edit`]},{name:`sections`,type:`array`,label:`Parent Sections`},{name:`details`,type:`array`,label:`Detail Collections`,required:!0}]}),c.register(`line_items`,({schema:e})=>(0,J.jsx)(ct,{schema:e}),{namespace:`record`,skipFallback:!0,label:`Line Items`,category:`record`,inputs:[{name:`childObject`,type:`string`,label:`Child Object`,required:!0},{name:`relationshipField`,type:`string`,label:`Relationship Field`,required:!0},{name:`columns`,type:`array`,label:`Columns`,required:!0},{name:`totalField`,type:`string`,label:`Parent Total Field`},{name:`amountField`,type:`string`,label:`Amount Column`}]});export{Oe as i,Qe as n,Ye as r,$ as t};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{o as e,r as t}from"./rolldown-runtime-CMxvf4Kt.js";import{_ as n,t as r}from"./vendor-react-DR370ovB.js";import{F as i,G as a,lt as o,mt as s,r as c,ut as l}from"./framework-CHiz03zG.js";import{u}from"./vendor-objectstack-RB3xvhcR.js";import{Cs as d,Qi as f,Zs as p,_r as ee,ar as te,cr as m,d as h,dr as g,ds as _,ea as ne,gr as v,ir as y,lr as b,or as x,pr as S,sr as C,u as w,ur as T,us as E,w as D}from"./ui-components-BbPoD3rS.js";import{a as O,i as k}from"./plugins-views-DH1gGZh6.js";var A=e(n(),1),j={"gantt.column.taskName":`Task Name`,"gantt.column.start":`Start`,"gantt.column.end":`End`,"gantt.toolbar.prevPeriod":`Previous period`,"gantt.toolbar.nextPeriod":`Next period`,"gantt.toolbar.zoomIn":`Zoom in`,"gantt.toolbar.zoomOut":`Zoom out`,"gantt.toolbar.jumpToToday":`Jump to today`,"gantt.toolbar.today":`Today`,"gantt.toolbar.showTaskList":`Show task list`,"gantt.toolbar.hideTaskList":`Hide task list`},M=`gantt.column.taskName`;function N(e,t){let n=j[e]||e;if(t)for(let[e,r]of Object.entries(t))n=n.replace(`{{${e}}}`,String(r));return n}function P(){try{let e=c();return e.t(M)===M?{t:N}:{t:e.t}}catch{return{t:N}}}var F=r();function re(e){return e<640?35:e<1024?50:60}function I(e){return e<640?140:e<1024?220:320}function L(e){return e>=280}function R(e){return e<640?32:40}function z({tasks:e,startDate:t,endDate:n,onTaskClick:r,onTaskUpdate:i,className:a,inlineEdit:o=!1}){let{t:s}=P(),[c,l]=A.useState(new Date),u=A.useRef(null),{width:te}=D(u),m=te||(typeof window<`u`?window.innerWidth:1024),h=m<640,g=R(m),y=re(m),[b,x]=A.useState(null),C=b??y,[w,T]=A.useState(!1),O=A.useRef(!1);A.useEffect(()=>{!O.current&&h&&(T(!0),O.current=!0)},[h]);let k=w?0:I(m),j=L(k),[M,N]=A.useState(null),[z,B]=A.useState({}),[V,H]=A.useState(null),U=A.useRef(null);A.useEffect(()=>{U.current=V},[V]);let W=A.useRef(!1),G=A.useCallback(e=>{let t=1e3*60*60*24,n=t,r=e.dayDelta*t,i=new Date(e.originStart),a=new Date(e.originEnd);return e.mode===`move`?(i=new Date(e.originStart.getTime()+r),a=new Date(e.originEnd.getTime()+r)):e.mode===`resize-left`?(i=new Date(e.originStart.getTime()+r),a.getTime()-i.getTime()<n&&(i=new Date(a.getTime()-n))):e.mode===`resize-right`&&(a=new Date(e.originEnd.getTime()+r),a.getTime()-i.getTime()<n&&(a=new Date(i.getTime()+n))),{start:i,end:a}},[]);A.useEffect(()=>{if(!V)return;let t=e=>{let t=U.current;if(!t)return;let n=Math.round((e.clientX-t.originClientX)/Math.max(C,1));n!==t.dayDelta&&H({...t,dayDelta:n})},n=()=>{let t=U.current;if(t){if(t.dayDelta!==0){let n=e.find(e=>e.id===t.taskId);if(n&&i){let{start:e,end:r}=G(t);i(n,{start:e,end:r})}W.current=!0,window.setTimeout(()=>{W.current=!1},0)}H(null)}};return window.addEventListener(`pointermove`,t),window.addEventListener(`pointerup`,n),window.addEventListener(`pointercancel`,n),()=>{window.removeEventListener(`pointermove`,t),window.removeEventListener(`pointerup`,n),window.removeEventListener(`pointercancel`,n)}},[V,C,e,i,G]);let K=A.useCallback((e,t,n)=>{i&&(n.stopPropagation(),n.preventDefault(),H({taskId:e.id,mode:t,originStart:new Date(e.start),originEnd:new Date(e.end),originClientX:n.clientX,dayDelta:0}))},[i]),q=A.useMemo(()=>{let r=t?new Date(t):new Date,i=n?new Date(n):new Date;return!t&&e.length>0&&(r=new Date(Math.min(...e.map(e=>e.start.getTime()))),r.setDate(r.getDate()-7)),!n&&e.length>0&&(i=new Date(Math.max(...e.map(e=>e.end.getTime()))),i.setDate(i.getDate()+14)),r.setHours(0,0,0,0),i.setHours(23,59,59,999),{start:r,end:i}},[t,n,e]),J=A.useMemo(()=>{let e=[],t=new Date(q.start);for(;t<=q.end;)e.push({date:new Date(t),label:t.getDate().toString(),isWeekend:t.getDay()===0||t.getDay()===6}),t.setDate(t.getDate()+1);return e},[q]),Y=A.useRef(null),X=A.useRef(null),ie=A.useRef(null),Z=A.useRef(null),Q=A.useRef(null),ae=A.useCallback(e=>{if(e.touches.length===2){let t=e.touches[0].clientX-e.touches[1].clientX,n=e.touches[0].clientY-e.touches[1].clientY;Q.current={baseDistance:Math.max(1,Math.sqrt(t*t+n*n)),baseColumn:C}}},[C]),oe=A.useCallback(e=>{if(e.touches.length!==2||!Q.current)return;let t=e.touches[0].clientX-e.touches[1].clientX,n=e.touches[0].clientY-e.touches[1].clientY,r=Math.max(1,Math.sqrt(t*t+n*n))/Q.current.baseDistance;x(Math.max(15,Math.min(120,Math.round(Q.current.baseColumn*r))))},[]),se=A.useCallback(()=>{Q.current=null},[]),$=A.useMemo(()=>{let e=new Date;if(e<q.start||e>q.end)return null;let t=(e.getTime()-q.start.getTime())/(1440*60*1e3);return Math.round(t*C)},[q,C]),ce=A.useCallback(()=>{if($==null||!Z.current)return;let e=Math.max(0,$-Z.current.clientWidth/2);Z.current.scrollTo({left:e,behavior:`smooth`})},[$]),le=e=>{Y.current&&(Y.current.scrollLeft=e.currentTarget.scrollLeft),X.current&&(X.current.scrollTop=e.currentTarget.scrollTop)},ue=e=>{q.end.getTime()-q.start.getTime();let t=C,n=1e3*60*60*24,r=e.start.getTime()-q.start.getTime(),i=e.end.getTime()-e.start.getTime();return{left:r/n*t,width:Math.max(i/n*t,t)}};return(0,F.jsxs)(`div`,{ref:u,className:p(`flex flex-col h-full bg-background overflow-hidden min-w-0`,a),children:[(0,F.jsxs)(`div`,{className:`flex flex-col sm:flex-row items-start sm:items-center justify-between gap-2 p-2 border-b bg-card`,children:[(0,F.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,F.jsx)(S,{variant:`ghost`,size:`icon`,className:`h-8 w-8`,"aria-label":s(`gantt.toolbar.prevPeriod`),children:(0,F.jsx)(_,{className:`h-4 w-4`})}),(0,F.jsx)(S,{variant:`ghost`,size:`icon`,className:`h-8 w-8`,"aria-label":s(`gantt.toolbar.nextPeriod`),children:(0,F.jsx)(E,{className:`h-4 w-4`})}),(0,F.jsx)(`span`,{className:`font-semibold text-xs sm:text-sm`,children:q.start.toLocaleDateString(void 0,{month:`long`,year:`numeric`})})]}),(0,F.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,F.jsxs)(`div`,{className:`flex bg-muted rounded-md p-1`,children:[(0,F.jsx)(S,{variant:`ghost`,size:`icon`,className:`h-6 w-6`,onClick:()=>x(Math.max(15,C-10)),"aria-label":s(`gantt.toolbar.zoomOut`),children:(0,F.jsx)(v,{className:`h-3 w-3`})}),(0,F.jsx)(S,{variant:`ghost`,size:`icon`,className:`h-6 w-6`,onClick:()=>x(Math.min(120,C+10)),"aria-label":s(`gantt.toolbar.zoomIn`),children:(0,F.jsx)(ee,{className:`h-3 w-3`})})]}),(0,F.jsx)(S,{variant:`ghost`,size:`icon`,className:`h-8 w-8`,onClick:()=>T(e=>!e),"aria-label":s(w?`gantt.toolbar.showTaskList`:`gantt.toolbar.hideTaskList`),"aria-pressed":w,"data-testid":`gantt-toggle-task-list`,children:w?(0,F.jsx)(f,{className:`h-4 w-4`}):(0,F.jsx)(ne,{className:`h-4 w-4`})}),(0,F.jsx)(S,{variant:`ghost`,size:`icon`,className:`h-8 w-8`,onClick:ce,disabled:$==null,"aria-label":s(`gantt.toolbar.jumpToToday`),"data-testid":`gantt-jump-today`,children:(0,F.jsx)(d,{className:`h-4 w-4`})})]})]}),(0,F.jsxs)(`div`,{className:`flex flex-col flex-1 overflow-hidden`,children:[(0,F.jsxs)(`div`,{className:`flex border-b bg-muted/30 shrink-0 h-10 sm:h-[50px]`,children:[(0,F.jsxs)(`div`,{className:`flex items-center font-medium text-xs text-muted-foreground px-2 sm:px-4 border-r bg-card z-20 shadow-sm`,style:{width:k,minWidth:k},children:[(0,F.jsx)(`div`,{className:`flex-1 truncate`,children:s(`gantt.column.taskName`)}),j&&(0,F.jsxs)(F.Fragment,{children:[(0,F.jsx)(`div`,{className:`w-16 sm:w-20 text-right`,children:s(`gantt.column.start`)}),(0,F.jsx)(`div`,{className:`w-16 sm:w-20 text-right`,children:s(`gantt.column.end`)})]})]}),(0,F.jsx)(`div`,{className:`flex-1 overflow-hidden`,ref:Y,children:(0,F.jsx)(`div`,{className:`flex h-full`,style:{width:J.length*C},children:J.map((e,t)=>(0,F.jsxs)(`div`,{className:p(`flex flex-col items-center justify-center border-r text-xs text-muted-foreground h-full`,e.isWeekend&&`bg-muted/50`),style:{width:C,minWidth:C},children:[(0,F.jsx)(`span`,{className:`font-medium text-foreground`,children:e.label}),(0,F.jsx)(`span`,{className:`text-[10px] opacity-70`,children:e.date.toLocaleDateString(void 0,{weekday:`narrow`})})]},t))})})]}),(0,F.jsxs)(`div`,{className:`flex flex-1 overflow-hidden`,children:[(0,F.jsx)(`div`,{className:`overflow-hidden border-r bg-card z-10 shadow-sm`,ref:X,style:{width:k,minWidth:k},children:e.map(e=>{let t=o&&M===e.id;return(0,F.jsxs)(`div`,{className:`group/task-row flex items-center border-b px-2 sm:px-4 hover:bg-accent/50 cursor-pointer transition-colors touch-manipulation`,style:{height:g},onClick:()=>!t&&r?.(e),onDoubleClick:()=>{o&&i&&(N(e.id),B({title:e.title,start:e.start.toLocaleDateString(`en-CA`),end:e.end.toLocaleDateString(`en-CA`),progress:String(e.progress)}))},children:[(0,F.jsxs)(`div`,{className:`flex-1 truncate font-medium text-xs sm:text-sm flex items-center gap-2`,children:[(0,F.jsx)(`div`,{className:`w-2 h-2 rounded-full shrink-0`,style:{backgroundColor:e.color||`#3b82f6`}}),t?(0,F.jsx)(`input`,{className:`border rounded px-1 py-0.5 text-xs w-full bg-background`,value:z.title||``,onChange:e=>B(t=>({...t,title:e.target.value})),onKeyDown:t=>{t.key===`Enter`?(i?.(e,{title:z.title,start:new Date(z.start),end:new Date(z.end),progress:Number(z.progress)||0}),N(null)):t.key===`Escape`&&N(null)},onClick:e=>e.stopPropagation(),autoFocus:!0}):(0,F.jsxs)(`span`,{className:`flex flex-col min-w-0`,children:[(0,F.jsx)(`span`,{className:`truncate`,children:e.title}),(0,F.jsxs)(`span`,{className:`text-[10px] text-muted-foreground sm:hidden`,children:[e.start.toLocaleDateString(void 0,{month:`numeric`,day:`numeric`}),` → `,e.end.toLocaleDateString(void 0,{month:`numeric`,day:`numeric`})]})]})]}),(0,F.jsx)(`div`,{className:`w-16 sm:w-20 text-right text-xs text-muted-foreground hidden sm:block`,hidden:!j,style:j?void 0:{display:`none`},children:t?(0,F.jsx)(`input`,{type:`date`,className:`border rounded px-1 py-0.5 text-xs w-full bg-background`,value:z.start||``,onChange:e=>B(t=>({...t,start:e.target.value})),onClick:e=>e.stopPropagation()}):e.start.toLocaleDateString(void 0,{month:`numeric`,day:`numeric`})}),(0,F.jsx)(`div`,{className:`w-16 sm:w-20 text-right text-xs text-muted-foreground hidden sm:block`,hidden:!j,style:j?void 0:{display:`none`},children:t?(0,F.jsx)(`input`,{type:`date`,className:`border rounded px-1 py-0.5 text-xs w-full bg-background`,value:z.end||``,onChange:e=>B(t=>({...t,end:e.target.value})),onClick:e=>e.stopPropagation()}):e.end.toLocaleDateString(void 0,{month:`numeric`,day:`numeric`})})]},e.id)})}),(0,F.jsx)(`div`,{className:`flex-1 overflow-auto bg-background/50 relative [-webkit-overflow-scrolling:touch]`,ref:e=>{ie.current=e,Z.current=e},onScroll:le,onTouchStart:ae,onTouchMove:oe,onTouchEnd:se,"data-testid":`gantt-timeline`,children:(0,F.jsxs)(`div`,{className:`relative`,style:{width:J.length*C},children:[$!=null&&(0,F.jsx)(`div`,{className:`absolute top-0 bottom-0 w-px bg-red-500/80 z-20 pointer-events-none`,style:{left:$},"data-testid":`gantt-today-marker`,"aria-label":s(`gantt.toolbar.today`),children:(0,F.jsx)(`div`,{className:`absolute -top-2 -translate-x-1/2 left-0 text-[10px] font-semibold text-white bg-red-500 rounded-sm px-1 py-0.5 whitespace-nowrap`,children:s(`gantt.toolbar.today`)})}),(0,F.jsxs)(`div`,{className:`relative`,children:[(0,F.jsx)(`div`,{className:`absolute inset-0 flex pointer-events-none z-0`,children:J.map((e,t)=>(0,F.jsx)(`div`,{className:p(`border-r h-full`,e.isWeekend&&`bg-muted/20`),style:{width:C,minWidth:C}},t))}),e.map(e=>{let t=ue(e),n=V?.taskId===e.id,a=t;if(n&&V){let t=G(V);a=ue({...e,start:t.start,end:t.end})}let o=!!i;return(0,F.jsxs)(`div`,{className:`relative border-b hover:bg-black/5`,style:{height:g},children:[n&&(0,F.jsx)(`div`,{className:`absolute top-1 sm:top-2 h-[calc(100%-8px)] sm:h-[calc(100%-16px)] rounded-sm border border-dashed border-primary/60 pointer-events-none`,style:{left:t.left,width:t.width,opacity:.35},"aria-hidden":`true`}),(0,F.jsxs)(`div`,{className:p(`absolute top-1 sm:top-2 h-[calc(100%-8px)] sm:h-[calc(100%-16px)] rounded-sm bg-primary border border-primary-foreground/20 shadow-sm hover:brightness-110 flex items-center px-2 group select-none`,o?`cursor-grab active:cursor-grabbing`:`cursor-pointer`,n&&`ring-2 ring-primary/60 brightness-110 z-10`),style:{left:a.left,width:a.width,backgroundColor:e.color||`#3b82f6`},"data-testid":`gantt-task-bar-${e.id}`,onClick:()=>{W.current||r?.(e)},onPointerDown:o?t=>{t.button===0&&K(e,`move`,t)}:void 0,children:[o&&a.width>=14&&(0,F.jsxs)(F.Fragment,{children:[(0,F.jsx)(`div`,{className:`absolute left-0 top-0 bottom-0 w-1.5 cursor-ew-resize hover:bg-white/40`,"data-testid":`gantt-task-resize-left-${e.id}`,onPointerDown:t=>{t.button===0&&K(e,`resize-left`,t)},onClick:e=>e.stopPropagation()}),(0,F.jsx)(`div`,{className:`absolute right-0 top-0 bottom-0 w-1.5 cursor-ew-resize hover:bg-white/40`,"data-testid":`gantt-task-resize-right-${e.id}`,onPointerDown:t=>{t.button===0&&K(e,`resize-right`,t)},onClick:e=>e.stopPropagation()})]}),e.progress>0&&(0,F.jsx)(`div`,{className:`absolute left-0 top-0 bottom-0 bg-black/20 rounded-l-sm pointer-events-none`,style:{width:`${e.progress}%`}}),(0,F.jsx)(`span`,{className:`text-[10px] text-white font-medium truncate opacity-0 group-hover:opacity-100 transition-opacity pointer-events-none`,children:n?`${G(V).start.toLocaleDateString(void 0,{month:`numeric`,day:`numeric`})} → ${G(V).end.toLocaleDateString(void 0,{month:`numeric`,day:`numeric`})}`:`${Math.round(e.progress)}%`})]})]},e.id)}),(0,F.jsx)(`div`,{className:`absolute top-0 bottom-0 w-px bg-red-500 z-20 pointer-events-none`,style:{left:(new Date().getTime()-q.start.getTime())/(1e3*60*60*24)*C},children:(0,F.jsx)(`div`,{className:`w-2 h-2 rounded-full bg-red-500 -ml-[3px]`})})]})]})})]})]})]})}function B(e){return e.data?e.data:e.staticData?{provider:`value`,items:e.staticData}:e.objectName?{provider:`object`,object:e.objectName}:null}function V(e){if(e){if(typeof e==`string`){let t=e.split(` `),n=t[0],r=t[1]?.toLowerCase()===`desc`?`desc`:`asc`;return{[n]:r}}if(Array.isArray(e))return e.reduce((e,t)=>(t.field&&t.order&&(e[t.field]=t.order),e),{})}}function H(e){let t=null;if(e.startDateField&&e.endDateField)return t={startDateField:e.startDateField,endDateField:e.endDateField,titleField:e.titleField||`name`,progressField:e.progressField,dependenciesField:e.dependenciesField||e.dependencyField,colorField:e.colorField},t;if(e.gantt&&(t=e.gantt),t){let e=u.safeParse(t);return e.success||console.warn(`[ObjectGantt] Invalid gantt configuration:`,e.error.format()),t}return null}var U=({schema:e,dataSource:t,className:n,onTaskClick:r,onRowClick:a,...s})=>{let[c,u]=(0,A.useState)([]),[d,f]=(0,A.useState)(!0),[p,ee]=(0,A.useState)(null),[_,ne]=(0,A.useState)(null),v=B(e),S=(0,A.useMemo)(()=>v,[JSON.stringify(v)]),E=H(e),D=S?.provider===`value`;(0,A.useEffect)(()=>{(async()=>{try{if(f(!0),s.data&&Array.isArray(s.data)){u(s.data),f(!1);return}if(D&&S?.provider===`value`){u(S.items),f(!1);return}if(!t||typeof t.find!=`function`)throw Error(`DataSource required for object/api providers`);if(S?.provider===`object`){let n=S.object,r=o(_?.fields);u(l(await t.find(n,{$filter:e.filter,$orderby:V(e.sort),...r.length>0?{$expand:r}:{}})))}else S?.provider===`api`&&(console.warn(`API provider not yet implemented for ObjectGantt`),u([]));f(!1)}catch(e){ee(e),f(!1)}})()},[S,t,D,e.filter,e.sort,_]),(0,A.useEffect)(()=>{!D&&t&&(async()=>{try{if(!t)return;let n=S?.provider===`object`?S.object:e.objectName;if(!n)return;ne(await t.getObjectSchema(n))}catch(e){console.error(`Failed to fetch object schema:`,e)}})()},[e.objectName,t,D,S]);let j=(0,A.useMemo)(()=>{if(!E||!c.length)return[];let{startDateField:e,endDateField:t,titleField:n,progressField:r,dependenciesField:i,colorField:a}=E,o=(e,t)=>{if(!t)return;let n=t.split(`.`),r=e;for(let e of n){if(r==null)return;r=r[e]}return r},s=e=>{let t=[o(e,n),e?.name,e?.title,e?.subject,e?.label,e?.account?.name,e?.opportunity?.name,e?.contact&&[e.contact.first_name,e.contact.last_name].filter(Boolean).join(` `),e?.id,e?._id];for(let e of t)if(e!=null&&String(e).trim()!==``)return String(e);return`Untitled`};return c.map((n,o)=>{let c=n[e],l=n[t],u=s(n),d=r?n[r]:0,f=i?n[i]:[],p=a?n[a]:void 0;if(!p){let e=n.status??n.state??n.priority??n.severity;if(e!=null&&e!==``){let t=w(void 0,e);t&&(p=h(t))}}return{id:n.id||n._id||`task-${o}`,title:u,start:c?new Date(c):new Date,end:l?new Date(l):new Date,progress:Math.min(100,Math.max(0,d||0)),dependencies:Array.isArray(f)?f:[],color:p,data:n}}).filter(e=>!isNaN(e.start.getTime())&&!isNaN(e.end.getTime()))},[c,E]),M=e.navigation??{mode:`drawer`,width:`min(960px, 60vw)`},N=M.mode===`drawer`||M.mode===`modal`||M.mode===`split`||M.mode===`popover`,P=i({navigation:M,objectName:e.objectName,onRowClick:N?void 0:a}),re=(0,A.useCallback)(async(n,r)=>{if(!E)return;let i=S?.provider===`object`?S.object:e.objectName;if(!i||!t||typeof t.update!=`function`)return;let{startDateField:a,endDateField:o,titleField:s,progressField:l}=E,d={};if(r.start instanceof Date&&(d[a]=r.start.toISOString()),r.end instanceof Date&&(d[o]=r.end.toISOString()),typeof r.title==`string`&&s&&(d[s]=r.title),typeof r.progress==`number`&&l&&(d[l]=r.progress),Object.keys(d).length===0)return;let f=n.data?.id??n.data?._id??n.id;if(f==null)return;let p=c;u(e=>e.map(e=>String(e.id??e._id)===String(f)?{...e,...d}:e));try{await t.update(i,String(f),d)}catch(e){console.error(`[ObjectGantt] Failed to persist task update:`,e),u(p)}},[E,S,t,e.objectName,c]),[I,L]=(0,A.useState)(null),[R,U]=(0,A.useState)(!1),W=(0,A.useCallback)(e=>{L(e)},[]),G=(0,A.useCallback)(async()=>{if(!I)return;let n=S?.provider===`object`?S.object:e.objectName;if(!n||!t?.delete){L(null);return}let r=I.data?.id??I.data?._id??I.id;if(r==null){L(null);return}U(!0);let i=c;u(e=>e.filter(e=>String(e.id??e._id)!==String(r)));try{await t.delete(n,String(r)),L(null)}catch(e){console.error(`[ObjectGantt] Failed to delete:`,e),u(i),L(null)}finally{U(!1)}},[I,S,t,e.objectName,c]);return d?(0,F.jsx)(`div`,{className:n,children:(0,F.jsx)(`div`,{className:`flex items-center justify-center h-96`,children:(0,F.jsx)(`div`,{className:`text-muted-foreground`,children:`Loading Gantt chart...`})})}):p?(0,F.jsx)(`div`,{className:n,children:(0,F.jsx)(`div`,{className:`flex items-center justify-center h-96`,children:(0,F.jsxs)(`div`,{className:`text-destructive`,children:[`Error: `,p.message]})})}):E?(0,F.jsxs)(`div`,{className:n,children:[(0,F.jsx)(`div`,{className:`h-[calc(100vh-200px)] min-h-[600px]`,children:(0,F.jsx)(z,{tasks:j,onTaskClick:e=>{P.handleClick(e.data),r?.(e.data)},onTaskUpdate:re,onTaskDelete:W,inlineEdit:!0})}),P.isOverlay&&P.isOpen&&P.selectedRecord&&(()=>{let n=S?.provider===`object`?S.object:e.objectName,r=P.selectedRecord,i=r.id??r._id;if(!n||i==null)return null;let a=E?.titleField?String(r[E.titleField]??`Task Details`):`Task Details`;return(0,F.jsx)(k,{open:!0,onClose:P.close,title:a,record:r,objectName:n,recordId:i,dataSource:t,objectSchema:_,width:P.width,fullPageHref:O(n,i)??void 0,onFieldSave:async(e,r)=>{t?.update&&(await t.update(n,String(i),{[e]:r}),u(t=>t.map(t=>String(t.id??t._id)===String(i)?{...t,[e]:r}:t)))},onDelete:async()=>{t?.delete&&(await t.delete(n,String(i)),u(e=>e.filter(e=>String(e.id??e._id)!==String(i))))}})})(),(0,F.jsx)(y,{open:!!I,onOpenChange:e=>{!e&&!R&&L(null)},children:(0,F.jsxs)(C,{children:[(0,F.jsxs)(T,{children:[(0,F.jsx)(g,{children:`Delete this task?`}),(0,F.jsx)(m,{children:I?(0,F.jsxs)(F.Fragment,{children:[`"`,I.title,`" will be permanently removed. This action cannot be undone.`]}):null})]}),(0,F.jsxs)(b,{children:[(0,F.jsx)(x,{disabled:R,children:`Cancel`}),(0,F.jsx)(te,{onClick:e=>{e.preventDefault(),G()},disabled:R,"data-testid":`gantt-delete-confirm`,className:`bg-destructive text-destructive-foreground hover:bg-destructive/90`,children:R?`Deleting…`:`Delete`})]})]})})]}):(0,F.jsx)(`div`,{className:n,children:(0,F.jsx)(`div`,{className:`flex items-center justify-center h-96`,children:(0,F.jsx)(`div`,{className:`text-muted-foreground`,children:`Gantt configuration required. Please specify startDateField, endDateField, and titleField.`})})})},W=t({GanttView:()=>z,ObjectGantt:()=>U,ObjectGanttRenderer:()=>G}),G=({schema:e})=>{let{dataSource:t}=a()||{};return(0,F.jsx)(U,{schema:e,dataSource:t})};s.register(`object-gantt`,G,{namespace:`plugin-gantt`,label:`Object Gantt`,category:`view`,inputs:[{name:`objectName`,type:`string`,label:`Object Name`,required:!0},{name:`gantt`,type:`object`,label:`Gantt Config`,description:`startDateField, endDateField, titleField, progressField, percentageField, colorField, dependenciesField`}]}),s.register(`gantt`,G,{namespace:`view`,label:`Gantt View`,category:`view`,inputs:[{name:`objectName`,type:`string`,label:`Object Name`,required:!0},{name:`gantt`,type:`object`,label:`Gantt Config`,description:`startDateField, endDateField, titleField, progressField, percentageField, colorField, dependenciesField`}]});export{W as t};
|