import{_,i as r,c as o,o as d,j as s,x as u,a as m,z as w,b as g,a5 as c,F as P}from"./main-D6qyxymd.js";const k={props:{plugin:{type:Object,required:!0},collection:{type:Object,required:!0},config:{type:Object,required:!0}},emits:["update:config"],data(){return{isLoading:!1,definitions:{},fieldsModeChoices:[{label:"Guided",value:"guided",default:!0},{label:"Advanced",value:"advanced"}]}},computed:{database(){return{table:null,fieldsMode:"guided",dataFields:[],dataFieldsAdvanced:"",...this.config}},tablesOptions(){return Object.keys(this.definitions).map(t=>({label:t,value:t})).sort((t,e)=>t.label.localeCompare(e.label))},primaryProperties(){return this.definitions[this.database.table]?Object.keys(this.definitions[this.database.table].properties).filter(t=>(this.definitions[this.database.table].properties[t].description||"").includes("<pk/>")).map(t=>({name:t})):[]},tableProperties(){return this.definitions[this.database.table]?Object.keys(this.definitions[this.database.table].properties).map(t=>({name:t})):[]},tablePropertiesOptions(){return this.tableProperties.map(t=>({label:t.name+(this.primaryProperties.some(e=>e.name===t.name)?"#primary":""),value:t.name}))},isRealtime(){return this.plugin.settings.publicData.realtimeTables[this.database.table]},selectedFields(){return this.database.fieldsMode==="guided"?this.database.dataFields:this.database.dataFieldsAdvanced.split(",").map(t=>{const e=t.replace(`
`,"").trim();return e.includes(":")?e.match(/:[^\(]+/g)[0].split(":")[1].trim():e})},isFieldsIncorrect(){return this.tableProperties.length&&this.selectedFields.some(t=>!this.tableProperties.some(e=>e.name===t))},isAdvancedFieldsInvalid(){return this.database.fieldsMode==="advanced"&&this.database.dataFieldsAdvanced.trim().endsWith(",")},isPrimaryRequired(){return this.database.fieldsMode==="guided"&&!this.selectedFields.length?!1:this.isRealtime&&this.primaryProperties.some(t=>!this.selectedFields.some(e=>e===t.name))}},watch:{"database.table"(){this.refreshSchema()},isFieldsIncorrect(t){t&&this.database.fieldsMode==="guided"&&this.refreshSchema()}},mounted(){var t,e;this.definitions=((e=(t=this.plugin)==null?void 0:t.doc)==null?void 0:e.definitions)||{}},methods:{async fetchTables(){var t,e;try{this.isLoading=!0,await this.plugin.fetchDoc(),this.definitions=((e=(t=this.plugin)==null?void 0:t.doc)==null?void 0:e.definitions)||{},this.refreshSchema()}catch(l){wwLib.wwLog.error(l)}finally{this.isLoading=!1}},refreshSchema(){const t=this.primaryProperties.map(l=>l.name),e=this.database.dataFields.filter(l=>this.tableProperties.some(p=>p.name===l));this.$emit("update:config",{...this.database,primaryData:t,dataFields:e})},setProp(t,e){this.$emit("update:config",{...this.database,[t]:e})}}},I={class:"flex items-center"},E={key:2,class:"error-message mt-2"},M={key:3,class:"error-message mt-2"},O={key:4,class:"error-message mt-2"};function q(t,e,l,p,b,a){const v=r("wwEditorInputTextSelect"),n=r("wwEditorIcon"),h=r("wwEditorFormRow"),y=r("wwEditorInputRadio"),f=r("wwEditorInput"),F=r("wwLoader");return d(),o(P,null,[s(h,{label:"Table",required:""},{default:w(()=>[g("div",I,[s(v,{class:"w-100",placeholder:"Select a table",required:"","model-value":a.database.table,options:a.tablesOptions,"onUpdate:modelValue":e[0]||(e[0]=i=>a.setProp("table",i))},null,8,["model-value","options"]),g("button",{type:"button",class:"ww-editor-button -primary -small -icon ml-2",onClick:e[1]||(e[1]=(...i)=>a.fetchTables&&a.fetchTables(...i))},[s(n,{name:"refresh",medium:""})])])]),_:1}),a.database.table?(d(),u(h,{key:0,label:"Fields",required:""},{default:w(()=>[s(y,{class:"mb-2","model-value":a.database.fieldsMode,choices:b.fieldsModeChoices,small:"","onUpdate:modelValue":e[2]||(e[2]=i=>a.setProp("fieldsMode",i))},null,8,["model-value","choices"]),a.database.fieldsMode==="guided"?(d(),u(f,{key:0,type:"select",multiple:"",options:a.tablePropertiesOptions,"model-value":a.database.dataFields,placeholder:"All fields","onUpdate:modelValue":e[3]||(e[3]=i=>a.setProp("dataFields",i))},null,8,["options","model-value"])):(d(),u(f,{key:1,type:"string","model-value":a.database.dataFieldsAdvanced,placeholder:`id,
supplier:supplier_id ( name ),
purchaser:purchaser_id ( name )`,"onUpdate:modelValue":e[4]||(e[4]=i=>a.setProp("dataFieldsAdvanced",i))},null,8,["model-value"])),a.isPrimaryRequired?(d(),o("div",E,[s(n,{name:"warning",small:""}),e[5]||(e[5]=c(" You must include all primary properties when using realtime table "))])):m("",!0),a.isAdvancedFieldsInvalid?(d(),o("div",M,[s(n,{name:"warning",small:""}),e[6]||(e[6]=c(" You have an invalid comma at the end of your query "))])):a.isFieldsIncorrect?(d(),o("div",O,[s(n,{name:"warning",small:""}),e[7]||(e[7]=c(" You have invalid fields in your advanced selection "))])):m("",!0)]),_:1})):m("",!0),s(F,{loading:b.isLoading},null,8,["loading"])],64)}const A=_(k,[["render",q],["__scopeId","data-v-29c30a2c"]]);export{A as default};
