import{y as defineStore,t,r as ref,c as computed,n as nextTick}from"./index-DOFLUERq.js";import{o as onKeyDown,a as useDebouncedRefHistory,b as useActiveElement}from"./index-CRx3oDcD.js";const ALL_FIELDTYPES=["Autocomplete","Attach","Attach Image","Barcode","Button","Check","Code","Color","Content","Cron Editor","Currency","Data","Date","Datetime","Duration","Dynamic Link","Float","Geolocation","Heading","HTML","HTML Editor","Icon","Image","Int","Jeditor","JSON","Link","Long Text","Markdown Editor","Password","Percent","Phone","Rating","Read Only","Select","Signature","Small Text","Table","Table MultiSelect","Text","Text Editor","Time"],LAYOUT_FIELDS=["Tab Break","Section Break","Column Break"],NO_VALUE_TYPES=["Button","Column Break","Heading","HTML","Image","Section Break","Tab Break"],TABLE_FIELDS=["Table","Table MultiSelect"],RESTRICTED_FIELDS=["name","creation","modified","modified_by","owner","pagestatus","idx","parent","parenttype","parentfield"],DEFAULT_PAGEFIELD_PROPERTIES=[{fieldname:"label",label:t("Label"),fieldtype:"Data"},{fieldname:"fieldtype",label:t("Field Type"),fieldtype:"Select",options:[...ALL_FIELDTYPES,...LAYOUT_FIELDS].join(`
`)},{fieldname:"fieldname",label:t("Fieldname"),fieldtype:"Data"},{fieldname:"precision",label:t("Precision"),fieldtype:"Select",options:`
0
1
2
3
4
5
6
7
8
9`,depends_on:"eval:in_list(['Float', 'Currency', 'Percent'], pg.fieldtype)"},{fieldname:"length",label:t("Length"),fieldtype:"Int",depends_on:"eval:in_list(['Data', 'Link', 'Dynamic Link', 'Password', 'Select', 'Read Only', 'Attach', 'Attach Image', 'Int'], pg.fieldtype)"},{fieldname:"non_negative",label:t("Non Negative"),fieldtype:"Check",depends_on:"eval:in_list(['Int', 'Float', 'Currency'], pg.fieldtype)"},{fieldname:"hide_days",label:t("Hide Days"),fieldtype:"Check",depends_on:"eval:pg.fieldtype=='Duration'"},{fieldname:"hide_seconds",label:t("Hide Seconds"),fieldtype:"Check",depends_on:"eval:pg.fieldtype=='Duration'"},{fieldname:"reqd",label:t("Mandatory"),fieldtype:"Check"},{fieldname:"is_virtual",label:t("Virtual"),fieldtype:"Check"},{fieldname:"search_index",label:t("Index"),fieldtype:"Check"},{fieldname:"column_break_18",fieldtype:"Column Break"},{fieldname:"options",label:t("Options"),fieldtype:"Small Text"},{fieldname:"sort_options",label:t("Sort Options"),fieldtype:"Check",depends_on:"eval:pg.fieldtype=='Select'"},{fieldname:"show_dashboard",label:t("Show Dashboard"),fieldtype:"Check",depends_on:"eval:pg.fieldtype=='Tab Break'"},{fieldname:"link_filters",label:t("Link Filters"),fieldtype:"Code",options:"JSON",depends_on:"eval:pg.fieldtype=='Link'"},{fieldname:"defaults_section",fieldtype:"Section Break",label:t("Defaults")},{fieldname:"default",label:t("Default"),fieldtype:"Small Text"},{fieldname:"column_break_6",fieldtype:"Column Break"},{fieldname:"fetch_from",label:t("Fetch From"),fieldtype:"Small Text"},{fieldname:"fetch_if_empty",label:t("Fetch If Empty"),fieldtype:"Check"},{fieldname:"visibility_section",fieldtype:"Section Break",label:t("Visibility")},{fieldname:"hidden",label:t("Hidden"),fieldtype:"Check"},{fieldname:"show_on_timeline",label:t("Show on Timeline"),fieldtype:"Check",depends_on:"eval:pg.hidden"},{fieldname:"bold",label:t("Bold"),fieldtype:"Check"},{fieldname:"allow_in_quick_entry",label:t("Allow in Quick Entry"),fieldtype:"Check"},{fieldname:"translatable",label:t("Translatable"),fieldtype:"Check",depends_on:"eval:['Data', 'Select', 'Text', 'Small Text', 'Text Editor'].includes(pg.fieldtype)"},{fieldname:"print_hide",label:t("Print Hide"),fieldtype:"Check"},{fieldname:"print_hide_if_no_value",label:t("Print Hide If No Value"),fieldtype:"Check",depends_on:"eval:['Int', 'Float', 'Currency', 'Percent'].indexOf(pg.fieldtype)!==-1"},{fieldname:"report_hide",label:t("Report Hide"),fieldtype:"Check"},{fieldname:"column_break_28",fieldtype:"Column Break"},{fieldname:"depends_on",label:t("Display Depends On (JS)"),fieldtype:"Code",options:"JS"},{fieldname:"collapsible",label:t("Collapsible"),fieldtype:"Check",depends_on:"eval:pg.fieldtype=='Section Break'"},{fieldname:"collapsible_depends_on",label:t("Collapsible Depends On (JS)"),fieldtype:"Code",options:"JS",depends_on:"eval:pg.collapsible"},{fieldname:"hide_border",label:t("Hide Border"),fieldtype:"Check",depends_on:"eval:pg.fieldtype=='Section Break'"},{fieldname:"list__search_settings_section",fieldtype:"Section Break",label:t("List / Search Settings")},{fieldname:"in_list_view",label:t("In List View"),fieldtype:"Check"},{fieldname:"in_standard_filter",label:t("In List Filter"),fieldtype:"Check"},{fieldname:"in_preview",label:t("In Preview"),fieldtype:"Check",depends_on:"eval:!in_list(['Table', 'Table MultiSelect'], pg.fieldtype)"},{fieldname:"column_break_35",fieldtype:"Column Break"},{fieldname:"in_filter",label:t("In Filter"),fieldtype:"Check"},{fieldname:"in_global_search",label:t("In Global Search"),fieldtype:"Check",depends_on:"eval:['Data', 'Select', 'Table', 'Text', 'Text Editor', 'Link', 'Small Text', 'Long Text', 'Read Only', 'Heading', 'Dynamic Link'].indexOf(pg.fieldtype)!==-1"},{fieldname:"permissions",fieldtype:"Section Break",label:t("Permissions")},{fieldname:"read_only",label:t("Read Only"),fieldtype:"Check"},{fieldname:"allow_on_submit",label:t("Allow on Submit"),fieldtype:"Check",depends_on:"eval:parent.is_submittable"},{fieldname:"ignore_user_permissions",label:t("Ignore User Permissions"),fieldtype:"Check"},{fieldname:"allow_bulk_edit",label:t("Allow Bulk Edit"),fieldtype:"Check",depends_on:"eval:pg.fieldtype=='Table'"},{fieldname:"make_attachment_public",label:t("Make Attachment Public (by default)"),fieldtype:"Check",depends_on:"eval:['Attach', 'Attach Image'].includes(pg.fieldtype)"},{fieldname:"column_break_13",fieldtype:"Column Break"},{fieldname:"permlevel",label:t("Permission Level"),fieldtype:"Int"},{fieldname:"ignore_xss_filter",label:t("Ignore XSS Filter"),fieldtype:"Check"},{fieldname:"constraints_section",fieldtype:"Section Break",label:t("Constraints")},{fieldname:"unique",label:t("Unique"),fieldtype:"Check"},{fieldname:"no_copy",label:t("No Copy"),fieldtype:"Check"},{fieldname:"set_only_once",label:t("Set Only Once"),fieldtype:"Check"},{fieldname:"remember_last_selected_value",label:t("Remember Last Selected Value"),fieldtype:"Check",depends_on:"eval:pg.fieldtype=='Link'"},{fieldname:"column_break_38",fieldtype:"Column Break"},{fieldname:"mandatory_depends_on",label:t("Mandatory Depends On (JS)"),fieldtype:"Code",options:"JS"},{fieldname:"read_only_depends_on",label:t("Read Only Depends On (JS)"),fieldtype:"Code",options:"JS"},{fieldname:"display",fieldtype:"Section Break",label:t("Display")},{fieldname:"print_width",label:t("Print Width"),fieldtype:"Data"},{fieldname:"width",label:t("Width"),fieldtype:"Data"},{fieldname:"max_height",label:t("Max Height"),fieldtype:"Data"},{fieldname:"columns",label:t("Columns"),fieldtype:"Int"},{fieldname:"column_break_22",fieldtype:"Column Break"},{fieldname:"description",label:t("Description"),fieldtype:"Small Text"},{fieldname:"documentation_url",label:t("Documentation URL"),fieldtype:"Data",options:"URL",depends_on:"eval:!in_list(['Tab Break', 'Section Break', 'Column Break', 'Button', 'HTML'], pg.fieldtype)"},{fieldname:"placeholder",label:t("Placeholder"),fieldtype:"Data"},{fieldname:"oldfieldname",label:t("Old Fieldname"),fieldtype:"Data",hidden:1},{fieldname:"oldfieldtype",label:t("Old Fieldtype"),fieldtype:"Data",hidden:1}],useFormBuilderStore=defineStore("form-builder-store",()=>{let o=ref(""),r=ref(null),e=ref([...DEFAULT_PAGEFIELD_PROPERTIES]),n=ref({layout:{tabs:[]},active_tab:null,selected_field:null}),c=ref(!1),p=ref(!1),u=ref(!1),S=ref(!1),v=ref(!1),C="cubic-bezier(0.34, 1.56, 0.64, 1)",_=ref(null),B=computed(()=>e.value),I=computed(()=>!n.value.layout.tabs||n.value.layout.tabs.length===0?null:n.value.layout.tabs.find(l=>l.df.name===n.value.active_tab));const a=useActiveElement(),f=computed(()=>a.value?.readOnly||a.value?.disabled||a.value?.tagName!=="INPUT"&&a.value?.tagName!=="TEXTAREA");function b(l){return n.value.selected_field?.name===l}function h(l,d="",s=""){return{name:F(8),fieldtype:l,fieldname:d,label:s,__islocal:1,__unsaved:1}}function F(l=8){const d="abcdefghijklmnopqrstuvwxyz0123456789";let s="";for(let k=0;k<l;k++)s+=d.charAt(Math.floor(Math.random()*d.length));return s}function x(l){if(!u.value)return!1;if(!l.df.is_custom_field)return!0;let d={"Tab Break":"sections","Section Break":"columns","Column Break":"fields"}[l.df.fieldtype];return d?l[d]?.some(s=>s.df.is_custom_field?x(s):!0):!1}function A(l){return l.df.is_custom_field&&!l.df.is_system_generated?1:0}function N(l=[],d={}){r.value=d.pg||null,o.value=d.pagetype||"",p.value=d.readOnly||!1,u.value=d.isCustomizeForm||!1,n.value.layout=createLayout(l),n.value.layout.tabs.length>0&&(n.value.active_tab=n.value.layout.tabs[0].df.name),n.value.selected_field=null,nextTick(()=>{c.value=!1,S.value=!1}),P()}onKeyDown(!0,l=>{_.value&&(l.ctrlKey||l.metaKey)&&(l.key==="z"&&!l.shiftKey&&_.value.canUndo?_.value.undo():l.key==="z"&&l.shiftKey&&_.value.canRedo&&_.value.redo())});function P(){_.value=useDebouncedRefHistory(n,{deep:!0,debounce:100})}function R(l,d){l=scrubFieldNames(l);let s="";l.some(i=>!LAYOUT_FIELDS.includes(i.fieldtype))||(s=t("PageType must have at least one field"));let y=["Attach Image",...NO_VALUE_TYPES];d&&(y=y.filter(i=>i!=="Button"));function m(i){let T=`<b>${i.label} (${i.fieldname})</b>`;i.label||(T=`<b>${i.fieldname}</b>`);let L=`<b>${i.fieldtype}</b>`;return[T,L]}return l.forEach(i=>{if(l.filter(L=>L.fieldname===i.fieldname).length>1&&(s=t("Fieldname {0} appears multiple times").replace("{0}",m(i)[0])),["Link",...TABLE_FIELDS].includes(i.fieldtype)&&!i.options&&(s=t("Options is required for field {0} of type {1}").replace("{0}",m(i)[0]).replace("{1}",m(i)[1])),i.hidden&&i.reqd&&!i.default&&(s=t("{0} cannot be hidden and mandatory without any default value").replace("{0}",m(i)[0])),i.in_list_view&&y.includes(i.fieldtype)&&(s=t("'In List View' is not allowed for field {0} of type {1}").replace("{0}",m(i)[0]).replace("{1}",m(i)[1])),i.in_global_search&&NO_VALUE_TYPES.includes(i.fieldtype)&&(s=t("'In Global Search' is not allowed for field {0} of type {1}").replace("{0}",m(i)[0]).replace("{1}",m(i)[1])),i.link_filters===""&&delete i.link_filters,i.link_filters)try{JSON.parse(i.link_filters)}catch{s=t("Invalid Filter Format for field {0} of type {1}").replace("{0}",m(i)[0]).replace("{1}",m(i)[1])}}),s}function J(){let l=[],d=0,s="new-pagefield-";return JSON.parse(JSON.stringify(n.value.layout.tabs)).forEach((y,m)=>{(m===0&&E(y.df,h("Tab Break","",t("Details")))||m>0)&&(d++,y.df.idx=d,y.df.__unsaved&&y.df.__islocal&&(y.df.name=s+d),l.push(y.df)),y.sections.forEach((i,T)=>{let L=JSON.parse(JSON.stringify(l)),H=d;i.has_fields=!1,(T===0&&E(i.df,h("Section Break"))||T>0)&&(d++,i.df.idx=d,i.df.__unsaved&&i.df.__islocal&&(i.df.name=s+d),l.push(i.df)),i.columns.forEach((g,w)=>{(w===0&&E(g.df,h("Column Break"))||w>0||g.fields.length===0)&&(d++,g.df.idx=d,g.df.__unsaved&&g.df.__islocal&&(g.df.name=s+d),l.push(g.df)),g.fields.forEach(D=>{d++,D.df.idx=d,D.df.__unsaved&&D.df.__islocal&&(D.df.name=s+d),l.push(D.df),i.has_fields=!0})}),i.has_fields||(l=L||[],d=H)})}),l}function E(l,d){let s=JSON.parse(JSON.stringify(l)),k=JSON.parse(JSON.stringify(d));return delete s.name,delete k.name,JSON.stringify(s)!==JSON.stringify(k)}function U(){return createLayout(r.value?.fields||[])}function M(){let l={df:h("Tab Break","",t("Tab")+" "+(n.value.layout.tabs.length+1)),sections:[sectionBoilerplate(h)]};n.value.layout.tabs.push(l),O(l)}function O(l){n.value.active_tab=l.df.name,n.value.selected_field=l.df,nextTick(()=>{const d=document.querySelector(".tabs .tab.active");d&&d.scrollIntoView({behavior:"smooth",inline:"center",block:"nearest"})})}return{pagetype:o,pg:r,form:n,dirty:c,readOnly:p,isCustomizeForm:u,preview:S,drag:v,getAnimation:C,getPagefields:B,currentTab:I,notUsingInput:f,selected:b,getDf:h,getRandomId:F,hasStandardField:x,isUserGeneratedField:A,initialize:N,validateFields:R,getUpdatedFields:J,isDfUpdated:E,getLayout:U,addNewTab:M,activateTab:O}});function createLayout(o){let r={tabs:[]},e=null,n=null,c=null;function p(a=8){const f="abcdefghijklmnopqrstuvwxyz0123456789";let b="";for(let h=0;h<a;h++)b+=f.charAt(Math.floor(Math.random()*f.length));return b}function u(a,f="",b=""){return{name:p(8),fieldtype:a,fieldname:f,label:b,__islocal:1,__unsaved:1}}function S(a){e=_(a),c=null,n=null,r.tabs.push(e)}function v(a){e||S(),n=B(a),c=null,e.sections.push(n)}function C(a){n||v(),c=I(a),n.columns.push(c)}function _(a){let f={};return f.df=a||u("Tab Break","",t("Details")),f.sections=[],f.is_first=!a,f}function B(a){let f={};return f.df=a||u("Section Break"),f.columns=[],f.is_first=!a,f}function I(a){let f={};return f.df=a||u("Column Break"),f.fields=[],f.is_first=!a,f}for(let a of o)if(a.fieldname&&(a=JSON.parse(JSON.stringify(a))),a.fieldtype==="Tab Break")S(a);else if(a.fieldtype==="Section Break")v(a);else if(a.fieldtype==="Column Break")C(a);else{c||C();let f={df:a};a.fieldtype==="Table"&&(f.table_columns=getTableColumns()),c.fields.push(f),n.has_fields=!0}for(let a of r.tabs)for(let f=a.sections.length-1;f>=0;--f){let b=a.sections[f];b.has_fields||a.sections.splice(a.sections.indexOf(b),1)}return r.tabs.length===0&&r.tabs.push({df:u("Tab Break","",t("Details")),sections:[sectionBoilerplate(u)],is_first:!0}),r}function getTableColumns(o,r){let e=[];return e.push([{label:t("No.")},1]),e}function evaluateDependsOnValue(expression,pg,parent=null){if(!pg)return!1;let out=null;if(typeof expression=="boolean")out=expression;else if(typeof expression=="function")out=expression(pg);else if(expression.substr(0,5)==="eval:")try{const in_list=(o,r)=>o&&o.includes(r);out=eval(expression.substr(5))}catch(o){out=!1}else{const o=pg[expression];Array.isArray(o)?out=!!o.length:out=!!o}return out}function sectionBoilerplate(o){return{df:o("Section Break"),columns:[{df:o("Column Break"),fields:[]}]}}function moveChildrenToParent(o,r,e,n,c){let p=o[r][e+"s"],u=p.indexOf(o[e]);if(u>0){const S=r.charAt(0).toUpperCase()+r.slice(1);let v={df:c(S+" Break"),[e+"s"]:p.splice(u)},C=n[r+"s"],_=C.indexOf(o[r]);return C.splice(_+1,0,v),p.splice(u+1),v.df.name}}function scrubFieldNames(o){function r(e=4){const n="abcdefghijklmnopqrstuvwxyz0123456789";let c="";for(let p=0;p<e;p++)c+=n.charAt(Math.floor(Math.random()*n.length));return c}return o.forEach(e=>{if(e.fieldtype){if(!e.fieldname)e.label?(e.fieldname=e.label.trim().toLowerCase().replaceAll(" ","_"),e.fieldname.endsWith("?")&&(e.fieldname=e.fieldname.slice(0,-1)),RESTRICTED_FIELDS.includes(e.fieldname)&&(e.fieldname=e.fieldname+"1"),e.fieldtype==="Section Break"?e.fieldname=e.fieldname+"_section":e.fieldtype==="Column Break"?e.fieldname=e.fieldname+"_column":e.fieldtype==="Tab Break"&&(e.fieldname=e.fieldname+"_tab")):e.fieldname=e.fieldtype.toLowerCase().replaceAll(" ","_")+"_"+r(4);else if(RESTRICTED_FIELDS.includes(e.fieldname))throw new Error(t("Fieldname {0} is restricted").replace("{0}",e.fieldname));let n=new RegExp(/['",./%@()<>{}]/g);e.fieldname=e.fieldname.replace(n,""),e.fieldname=e.fieldname.toLowerCase()}e.unique&&(e.search_index=0)}),o}function cloneField(o){function r(n=8){const c="abcdefghijklmnopqrstuvwxyz0123456789";let p="";for(let u=0;u<n;u++)p+=c.charAt(Math.floor(Math.random()*c.length));return p}let e=JSON.parse(JSON.stringify(o));return e.df.name=r(8),e}function confirmDialog(o,r,e,n,c,p){window.confirm(`${o}

${r}`)?e&&e():c&&c()}function isTouchScreenDevice(){return"ontouchstart"in document.documentElement}function inList(o,r){return o&&o.includes(r)}export{ALL_FIELDTYPES as A,LAYOUT_FIELDS as L,NO_VALUE_TYPES as N,inList as a,cloneField as b,confirmDialog as c,evaluateDependsOnValue as e,isTouchScreenDevice as i,moveChildrenToParent as m,sectionBoilerplate as s,useFormBuilderStore as u};
