@dbml/parse 10.4.0-alpha.0 → 10.4.0-alpha.1

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.
@@ -5,7 +5,7 @@ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=Symbol(
5
5
  `);return new Sf(w.INVALID_REF_RELATIONSHIP,c,t,{category:bf.RefColumnMismatch,explanation:f,quickFixes:o})}function Lh(e,t,n){let{relOp:r,columns:i,refNode:a,quickFixes:o}=n,s=Fh(e,i),c=i.length>1?`Columns (\`${s}\`) are NOT NULL but operator \`${r}\` allows them to be optional`:`Column \`${s}\` is NOT NULL but operator \`${r}\` allows it to be optional`,l=Ph(e,a),u=i[0]?.declaration,d=u&&Ph(e,u),f=[`- In ref definition \`${l??r}\`, the operator \`${r}\` marks \`${s}\` as optional.`,d?`- In column definition \`${d}\`, the column is NOT NULL.`:`- The column is NOT NULL.`].join(`
6
6
  `);return new Sf(w.INVALID_REF_RELATIONSHIP,c,t,{category:bf.RefColumnMismatch,explanation:f,quickFixes:o})}function Rh(e,t,n){let{relOp:r,columns:i,refNode:a,quickFixes:o}=n,s=Fh(e,i),c=i.length>1?`Columns (\`${s}\`) have a unique index but operator \`${r}\` allows many`:`Column \`${s}\` is unique but operator \`${r}\` allows many`,l=[`- In ref definition \`${Ph(e,a)??r}\`, the operator \`${r}\` allows many.`,`- \`${s}\` has a unique constraint, so each value can appear at most once.`].join(`
7
7
  `);return new Sf(w.INVALID_REF_RELATIONSHIP,c,t,{category:bf.RefColumnMismatch,explanation:l,quickFixes:o})}function zh(e,t,n){let{relOp:r,columns:i,refNode:a,quickFixes:o}=n,s=Fh(e,i),c=i.length>1?`Columns (\`${s}\`) should have a composite unique index for operator \`${r}\``:`Column \`${s}\` should be unique or primary key for operator \`${r}\``,l=[`- In ref definition \`${Ph(e,a)??r}\`, the operator \`${r}\` implies \`${s}\` has at most one row.`,`- \`${s}\` has no unique or primary key constraint.`].join(`
8
- `);return new Sf(w.INVALID_REF_RELATIONSHIP,c,t,{category:bf.RefColumnMismatch,explanation:l,quickFixes:o})}function Bh(e,t,n,r){let{schema:i,name:a}=r?t.interpretedName(e,r):{schema:t.schema(e),name:t.name??``},o=n.map(e=>{let t=e.name??``;return i?`${i}.${a}.${t}`:`${a}.${t}`});return o.length===1?o[0]:`(${o.join(`, `)})`}function Vh(e,t){return e.length>1?`Composite ${t}`:t}function Hh(e,t){let{colName:n}=t;return new Sf(w.INVALID_RECORDS_FIELD,`NULL not allowed for NOT NULL column \`${n}\` without a default value or auto-increment`,e,{category:bf.RecordValueTypeMismatch,explanation:`\`${n}\` is marked NOT NULL and has no default value or auto-increment.`})}function Uh(e,t){let{colName:n}=t;return new Sf(w.INVALID_RECORDS_FIELD,`Invalid enum value for column \`${n}\``,e,{category:bf.RecordValueTypeMismatch,explanation:`\`${n}\` uses an enum type. The value must match one of the defined enum members.`})}function Wh(e,t){let{colName:n,typeName:r}=t;return new Sf(w.INVALID_RECORDS_FIELD,`Invalid numeric value for column \`${n}\``,e,{category:bf.RecordValueTypeMismatch,explanation:`\`${n}\` has type \`${r}\` which expects a numeric value. Provide a valid integer or decimal.`})}function Gh(e,t){let{colName:n,value:r}=t;return new Sf(w.INVALID_RECORDS_FIELD,`Invalid integer value \`${r}\` for column \`${n}\`: expected integer, got decimal`,e,{category:bf.RecordValueTypeMismatch,explanation:`\`${n}\` is an integer column and cannot store decimal values.`})}function Kh(e,t){let{colName:n,typeName:r,value:i,precision:a,scale:o,totalDigits:s}=t;return new Sf(w.INVALID_RECORDS_FIELD,`Numeric value \`${i}\` for column \`${n}\` exceeds precision: expected at most ${a} total digits, got ${s}`,e,{category:bf.RecordValueTypeMismatch,explanation:`\`${n}\` is defined as \`${r}(${a},${o})\`, allowing at most ${a} total digits. The value \`${i}\` has ${s}.`})}function qh(e,t){let{colName:n,typeName:r,value:i,precision:a,scale:o,decimalDigits:s}=t;return new Sf(w.INVALID_RECORDS_FIELD,`Numeric value \`${i}\` for column \`${n}\` exceeds scale: expected at most ${o} decimal digits, got ${s}`,e,{category:bf.RecordValueTypeMismatch,explanation:`\`${n}\` is defined as \`${r}(${a},${o})\`, allowing at most ${o} decimal places. The value \`${i}\` has ${s}.`})}function Jh(e,t){let{colName:n}=t;return new Sf(w.INVALID_RECORDS_FIELD,`Invalid boolean value for column \`${n}\``,e,{category:bf.RecordValueTypeMismatch,explanation:`\`${n}\` is a boolean column. Use \`true\`/\`false\`, \`1\`/\`0\`, or \`'yes'\`/\`'no'\`.`})}function Yh(e,t){let{colName:n}=t;return new Sf(w.INVALID_RECORDS_FIELD,`Invalid datetime value for column \`${n}\``,e,{category:bf.RecordValueTypeMismatch,explanation:`\`${n}\` expects a date or time value. Supported formats include \`'YYYY-MM-DD'\`, \`'HH:MM:SS'\`, \`'YYYY-MM-DD HH:MM:SS'\`, \`'MM/DD/YYYY'\`, \`'D MMM YYYY'\`, or \`'MMM D, YYYY'\`.`})}function Xh(e,t){let{colName:n}=t;return new Sf(w.INVALID_RECORDS_FIELD,`Invalid string value for column \`${n}\``,e,{category:bf.RecordValueTypeMismatch,explanation:`\`${n}\` is a string column. Values must be wrapped in quotes.`})}function Zh(e,t){let{colName:n,typeName:r,typeArg:i,maxLength:a,actualLength:o}=t;return new Sf(w.INVALID_RECORDS_FIELD,`String value for column \`${n}\` exceeds maximum length: expected at most ${a} bytes (UTF-8), got ${o} bytes`,e,{category:bf.RecordValueTypeMismatch,explanation:`\`${n}\` is defined as \`${r}(${i})\`, limiting values to ${a} bytes in UTF-8 encoding. The provided value uses ${o} bytes.`})}function Qh(e,t,n){let{table:r,columns:i}=n,a=Bh(e,r,i),o=Vh(i,`PK`);return new Sf(w.INVALID_RECORDS_FIELD,`NULL in ${o}: \`${a}\` cannot be NULL`,t,{category:bf.RecordConstraintViolation,explanation:`\`${a}\` is a primary key and cannot be NULL.`})}function $h(e,t,n){let{table:r,columns:i,valueStr:a}=n,o=Bh(e,r,i),s=Vh(i,`PK`);return new Sf(w.INVALID_RECORDS_FIELD,`Duplicate ${s}: \`${o}\` = \`${a}\``,t,{category:bf.RecordConstraintViolation,explanation:`\`${o}\` is a primary key and must be unique across all rows.`})}function eg(e,t,n){let{table:r,columns:i}=n,a=Bh(e,r,i),o=Vh(i,`PK`);return new Sf(w.INVALID_RECORDS_FIELD,`${o}: Column \`${a}\` is missing from record and has no default value`,t,{category:bf.RecordConstraintViolation,explanation:`\`${a}\` is a primary key with no default or auto-increment, so it must be specified in the records block.`})}function tg(e,t,n){let{leftTable:r,leftColumns:i,rightTable:a,rightColumns:o,filepath:s,valueStr:c}=n,l=Bh(e,r,i,s),u=Bh(e,a,o,s);return new Sf(w.INVALID_RECORDS_FIELD,`FK violation: \`${l}\` = \`${c}\` must not be NULL`,t,{category:bf.RecordConstraintViolation,explanation:`\`${l}\` references \`${u}\`, so every row must have a non-null value.`})}function ng(e,t,n){let{leftTable:r,leftColumns:i,rightTable:a,rightColumns:o,filepath:s,valueStr:c}=n,l=Bh(e,r,i,s),u=Bh(e,a,o,s);return new Sf(w.INVALID_RECORDS_FIELD,`FK violation: \`${l}\` = \`${c}\` does not exist in \`${u}\``,t,{category:bf.RecordConstraintViolation,explanation:`\`${l}\` is a foreign key referencing \`${u}\`. Every non-null value in an FK must match an existing row in the referenced column.`})}function rg(e,t,n){let{table:r,columns:i,valueStr:a}=n,o=Bh(e,r,i),s=Vh(i,`UNIQUE`);return new Sf(w.INVALID_RECORDS_FIELD,`Duplicate ${s}: \`${o}\` = \`${a}\``,t,{category:bf.RecordConstraintViolation,explanation:`\`${o}\` has a unique constraint, so no two rows can have the same value.`})}function ig(e){return e.values.map(t=>{let n={};return e.columns.forEach((e,r)=>{n[e]=t[r]}),n})}function ag(e,t){return e?`${e}.${t}`:`${y}.${t}`}function og(e,t,n){return n.map(n=>{let r=t[n.name??``]?.value;if(r==null){let t=n.default(e);if(t)return JSON.stringify(t.value)}return JSON.stringify(r)}).join(`|`)}function sg(e,t,n){return n.some(n=>{let r=t[n.name??``]?.value;return r==null&&n.default(e)?!1:r==null})}function cg(e,t,n){let r=n.map(n=>{let r=n.name??``,i=t[r]===void 0?n.default(e)?.value:t[r].value;return JSON.stringify(i)}).join(`, `);return n.length===1?r:`(${r})`}function lg(e,t){let n=t.filter(t=>e[t]).map(t=>e[t]);return n.length>0?n:Object.values(e).filter(Boolean)}function ug(e,t){let{token:n}=t;return e.nodeAtPosition(n.filepath,n.start.offset)??e.parse.ast(n.filepath)}function dg(e,t,n,r){return Oa(r.values)?[]:xa(gg(t,e),i=>fg(e,t,n,i,r))}function fg(e,t,n,r,i){let a=ig(i),o=hg(e,t,n,r,i);if(!Oa(o))return o;let s=r.filter(t=>!t.increment(e)&&!t.default(e)),[c,l]=_(a,t=>sg(e,t,s)),u=pg(e,t,s,c),d=r.some(t=>t.increment(e))?[]:mg(e,t,r,l);return[...u,...d]}function pg(e,t,n,r){if(Oa(r))return[];let i=n.map(e=>e.name??``);return xa(r,r=>lg(r,i).map(r=>Qh(e,ug(e,r),{table:t,columns:n})))}function mg(e,t,n,r){let i=n.map(e=>e.name??``);return xa(ma(wa(r,t=>og(e,t,n)),e=>e.length>1),r=>xa(r,r=>{let a=cg(e,r,n);return lg(r,i).map(r=>$h(e,ug(e,r),{table:t,columns:n,valueStr:a}))}))}function hg(e,t,n,r,i){let a=r.map(e=>e.name??``),o=new Set(i.columns),s=da(a,Array.from(o));if(Oa(s))return[];let c=new Set(s),l=r.filter(e=>c.has(e.name??``)).filter(t=>!t.increment(e)&&!t.default(e));return Oa(l)?[]:i.values.map(()=>eg(e,n,{table:t,columns:l}))}function gg(e,t){let n=e.mergedColumns(t),r=ka(n,e=>e.name??``);return[...n.filter(e=>e.pk(t)).map(e=>[e]),...e.mergedIndexes(t).flatMap(e=>{let n=t.interpretMetadata(e,e.declaration.filepath).getValue();return Array.isArray(n)?n.filter(e=>e.pk).map(e=>Qr(e.columns.map(e=>r[e.value]))):[]})]}function _g(e,t,n){if(Oa(n.values))return[];let r=ig(n);return xa(vg(e,t),n=>yg(e,t,n,r))}function vg(e,t){let n=t.mergedColumns(e),r=ka(n,e=>e.name??``);return[...n.filter(t=>t.isUniqueSet(e)).map(e=>[e]),...t.mergedIndexes(e).flatMap(t=>{let n=e.interpretMetadata(t,t.declaration.filepath).getValue();return Array.isArray(n)?n.filter(e=>e.unique).map(e=>Qr(e.columns.map(e=>r[e.value]))):[]})]}function yg(e,t,n,r){let i=n.map(e=>e.name??``);return xa(ma(wa(r.filter(t=>!sg(e,t,n)),t=>og(e,t,n)),e=>e.length>1),r=>xa(r,r=>{let a=cg(e,r,n);return lg(r,i).map(r=>rg(e,ug(e,r),{table:t,columns:n,valueStr:a}))}))}function bg(e,t,n){let r=new Map;for(let i of e.values()){let{name:e,schema:a}=i.tableSymbol.interpretedName(t,n);r.set(ag(a,e),i)}return r}function xg(e,t,n,r){let i=bg(n,e,r),a=new Set;for(let[e,t]of i)t.record&&!Oa(t.record.values)&&a.add(e);return xa(t.filter(e=>e.endpoints?e.endpoints.some(e=>a.has(ag(e.schemaName,e.tableName))):!1),t=>Sg(e,t,i,r))}function Sg(e,t,n,r){if(!t.endpoints)return[];let[i,a]=t.endpoints,o=n.get(ag(i.schemaName,i.tableName)),s=n.get(ag(a.schemaName,a.tableName));if(!o||!s)return[];let c=o.tableSymbol.mergedColumns(e),l=s.tableSymbol.mergedColumns(e),u=Qr(i.fieldNames.map(e=>c.find(t=>t.name===e))),d=Qr(a.fieldNames.map(e=>l.find(t=>t.name===e))),{min:f,max:p}=nf(i.relation),{min:m,max:ee}=nf(a.relation),te=p===1&&m===0||p!==`*`&&ee!==`*`,ne=ee===1&&f===0;return[...te?[]:Cg(e,o,u,s,d,i.relation,a.relation,r),...ne?[]:Cg(e,s,d,o,u,a.relation,i.relation,r)]}function Cg(e,t,n,r,i,a,o,s){if(!t.record||Oa(t.record.values))return[];let{min:c,max:l}=nf(a),{min:u,max:d}=nf(o),f=Qr(n.map(e=>e.name));Qr(i.map(e=>e.name));let p=u===0,m=ig(t.record),ee=r.record?ig(r.record):[],te=new Set(ee.map(t=>og(e,t,i))),ne={leftTable:t.tableSymbol,leftColumns:n,rightTable:r.tableSymbol,rightColumns:i,filepath:s};return xa(m,t=>{if(sg(e,t,n)){if(p)return[];let r=cg(e,t,n);return lg(t,f).map(t=>tg(e,ug(e,t),{...ne,valueStr:r}))}if(d===`*`&&c===0&&l===`*`)return[];let r=og(e,t,n);if(te.has(r))return[];let i=cg(e,t,n);return lg(t,f).map(t=>ng(e,ug(e,t),{...ne,valueStr:i}))})}function wg(e){return e instanceof qm&&e.declaration instanceof z&&e.declaration.body instanceof U}function Tg(e,t){let n=e.edges??[];if(n.length===0||!wg(t))return[];let r=[],i=ag(n[0].downstream.schemaName,n[0].downstream.tableName);for(let e=1;e<n.length;e++){let a=ag(n[e].downstream.schemaName,n[e].downstream.tableName);if(a!==i){r.push(new T(w.DEP_MIXED_DOWNSTREAM_TABLES,`All edges in a Dep block must target the same downstream table, but found '${i}' and '${a}'`,t.declaration));break}}let a=e=>e.upstream.fieldNames.length>0||e.downstream.fieldNames.length>0,o=a(n[0]);for(let e=1;e<n.length;e++)if(a(n[e])!==o){r.push(new T(w.DEP_MIXED_LEVEL,`All edges in a Dep block must be at the same level (all table-level or all column-level)`,t.declaration));break}return r}function Eg(e,t){let n=Ag(e);if(n){let e=n.elementList??[],r=e.findIndex(e=>e instanceof B?hp(e)?.toLowerCase()===t.toLowerCase():!1);if(r!==-1)return{kind:`attribute`,settingsList:n,settingNode:e[r],settingIndex:r}}if(e instanceof z){let n=jg(e,t);if(n)return{kind:`body`,declaration:n}}}function Dg(e,t){let n=Ag(e);if(n){let e=n.end-1;return{start:e,end:e,newText:`, ${t}`}}if(e instanceof z){let n=e.body;if(n&&!(n instanceof H))return{start:n.start,end:n.start,newText:`[${t}] `}}return{start:e.end,end:e.end,newText:` [${t}]`}}function Og(e,t,n,r){if(n===null)return kg(e,t,r);let i=n===void 0?t:`${t}: ${n}`,a=Eg(e,t);if(a){let e=a.kind===`attribute`?a.settingNode:a.declaration;return{start:e.start,end:e.end,newText:i}}return Dg(e,i)}function kg(e,t,n){let r=Eg(e,t);if(!r)return;if(r.kind===`body`)return{start:r.declaration.start,end:r.declaration.end,newText:``};let{settingsList:i,settingIndex:a}=r,o=i.elementList??[];if(o.length===1){let e=i.start;for(;e>0&&n[e-1]===` `;)e--;return{start:e,end:i.end,newText:``}}let s=o[a];if(a===0){let e=o[a+1];return{start:s.start,end:e.start,newText:``}}return{start:o[a-1].end,end:s.end,newText:``}}function Ag(e){if(e instanceof H)return e.args.find(e=>e instanceof W);if(e instanceof z){if(e.attributeList)return e.attributeList;let t=e.body;if(t instanceof H)return t.args.find(e=>e instanceof W)}}function jg(e,t){let n=e.body;if(!(!n||Af(e)||n instanceof H)){for(let e of n.body)if(e instanceof z&&e.type?.value?.toLowerCase()===t.toLowerCase())return e}}function Mg(e,t,n,r){let{allowOtherColFix:i=!0,allowOwnColFix:a=!0}=r??{},o=t.op(e),s=t.opToken(),c=t.cardinalities(e);if(!o||!s||!c)return[];let l=n===`left`?c[0]:c[1],u=nf(l),d=n===`left`?c[1]:c[0],f=nf(d),p=n===`left`?t.leftColumns(e):t.rightColumns(e),m=n===`left`?t.rightColumns(e):t.leftColumns(e),ee=n===`left`?t.leftToken():t.rightToken(),te=n===`left`?t.rightToken():t.leftToken(),ne=[];function re(e,t,n){ne.push(n(e));for(let r of t)r.declaration&&r.declaration!==e&&ne.push(n(r.declaration))}let ie=f.max===`*`||u.max===1&&f.max===1&&n===`left`;if(u.min>=1&&ie&&m.length>0&&m.every(t=>t.nullable(e))){let r=m.map(t=>t.qualifiedName(e).join(`.`)).join(`, `),a=[s&&Pg(s,_f(l),d,n,`Make '${r}' optional in the ref`),...i?m.map(t=>Fg(t,e)).filter(e=>!!e):[]].filter(e=>!!e);re(te,m,n=>Ih(e,n,{relOp:o,columns:m,refNode:t.declaration,quickFixes:a}))}if(u.min===0&&ie&&m.length>0&&m.every(t=>t.nullable(e)===!1)){let r=m.map(t=>t.qualifiedName(e).join(`.`)).join(`, `),a=[s&&Pg(s,vf(l),d,n,`Make '${r}' required in the ref`),...i?m.map(t=>Ig(t,e)).filter(e=>!!e):[]].filter(e=>!!e);re(te,m,n=>Lh(e,n,{relOp:o,columns:m,refNode:t.declaration,quickFixes:a}))}if(u.max===`*`&&Lg(e,p)){let r=p.map(t=>t.qualifiedName(e).join(`.`)).join(`, `),i=[s&&Pg(s,`1`,d,n,`Make '${r}' one in the ref`)].filter(e=>!!e);re(ee,p,n=>Rh(e,n,{relOp:o,columns:p,refNode:t.declaration,quickFixes:i}))}if(u.max===1&&!Lg(e,p)){let r=p.map(t=>t.qualifiedName(e).join(`.`)).join(`, `),i=[s&&Pg(s,yf(l),d,n,`Make '${r}' many in the ref`),a&&p.length===1&&Rg(p[0],e)].filter(e=>!!e);re(ee,p,n=>zh(e,n,{relOp:o,columns:p,refNode:t.declaration,quickFixes:i}))}return ne}function Ng(e,t){return[...Mg(e,t,`right`,{allowOtherColFix:!1}),...Mg(e,t,`left`,{allowOwnColFix:!1})]}function Pg(e,t,n,r,i){let a=gf(r===`left`?t:n,r===`right`?t:n);return{title:`${i} (change operator to '${a}')`,shortTitle:`Change to ${a}`,filepath:e.filepath,edits:[{start:e.start,end:e.end,newText:a}],isPreferred:!0}}function Fg(e,t){if(!e.declaration||e.pk(t)||e.increment(t)||e.isNotNullSet(t))return;let n=t.getSource(e.declaration.filepath);if(!n)return;let r=e.qualifiedName(t).join(`.`),i=kg(e.declaration,`null`,n);if(i)return{title:`Mark '${r}' as NOT NULL`,shortTitle:`Add not null`,filepath:e.declaration.filepath,edits:[i]};let a=Dg(e.declaration,`not null`);if(a)return{title:`Mark '${r}' as NOT NULL`,shortTitle:`Add not null`,filepath:e.declaration.filepath,edits:[a]}}function Ig(e,t){if(!e.declaration)return;let n=t.getSource(e.declaration.filepath);if(!n)return;let r=kg(e.declaration,`not null`,n);if(r)return{title:`Mark '${e.qualifiedName(t).join(`.`)}' as NULL`,shortTitle:`Make nullable`,filepath:e.declaration.filepath,edits:[r]}}function Lg(e,n){if(n.length===0)return!1;if(n.length===1&&n[0].unique(e))return!0;let r=n[0],i=(r instanceof um?r.injectionDeclaration:r.declaration)?.parentOfKind(z);if(!i)return!1;let a=e.nodeSymbol(i).getFiltered(t);if(!(a instanceof qp))return!1;let o=a.mergedIndexes(e),s=new Set(n.map(e=>e.name));for(let t of o){let r=e.interpretMetadata(t,n[0].filepath).getValue();if(!(!r||!Array.isArray(r))){for(let e of r)if(!(!e.unique&&!e.pk)&&e.columns.length===s.size&&e.columns.every(e=>s.has(e.value)))return!0}}return!1}function Rg(e,t){if(!e.declaration||e.isUniqueSet(t)||e.pk(t))return;let n=Dg(e.declaration,`unique`);if(n)return{title:`Mark '${e.qualifiedName(t).join(`.`)}' as UNIQUE`,shortTitle:`Add unique`,filepath:e.declaration.filepath,edits:[n]}}var zg=class{constructor(e,t,n){this.errors=[],this.warnings=[],this.infos=[],this.compiler=e,this.programSymbol=t,this.programNode=t.declaration,this.filepath=n,this.db={schemas:[],tables:[],notes:[],refs:[],deps:[],enums:[],tableGroups:[],aliases:[],tablePartials:[],records:[],diagramViews:[],token:F(this.programNode),externals:{tables:[],enums:[],tableGroups:[],tablePartials:[],notes:[]}}}interpret(){return this.interpretAllSymbols(),this.interpretAllMetadata(),this.interpretAllAliases(),this.validatePartialRefs(),this.infos.push(...this.validateRecords()),new n(this.db,this.errors,this.warnings,this.infos)}interpretAllSymbols(){for(let e of this.programSymbol.declaration.declarations){let n=this.compiler.nodeSymbol(e).getFiltered(t);if(!n||n instanceof lm)continue;let r=this.compiler.interpretSymbol(n,this.filepath);if(r.hasValue(t))continue;this.errors.push(...r.getErrors()),this.warnings.push(...r.getWarnings()),this.infos.push(...r.getInfos());let i=r.getValue();i&&this.pushElement(n,i)}let e=new Set,n=(this.compiler.symbolMembers(this.programSymbol).getFiltered(t)??[]).filter(e=>e instanceof Wp);for(;n.length>0;){let r=n.shift(),i=this.compiler.symbolMembers(r).getFiltered(t)??[];for(let t of i){if(t instanceof Wp){n.push(t);continue}if(!(t instanceof lm))continue;let r=t.originalSymbol.intern();e.has(r)||(e.add(r),this.interpretUseSymbol(t))}}}interpretUseSymbol(e){let n=e.originalSymbol,r=this.compiler.interpretSymbol(n,this.filepath);if(!r.hasValue(t)){this.errors.push(...r.getErrors()),this.warnings.push(...r.getWarnings()),this.infos.push(...r.getInfos());let t=r.getValue();t&&this.pushElement(e,t)}let i=n instanceof qp?n.interpretedName(this.compiler,n.filepath):{name:n.name??``,schema:null},a=n instanceof qp?n.interpretedName(this.compiler,this.filepath):{name:e.name??n.name??``,schema:null},o={name:i.name,schemaName:i.schema,filepath:n.filepath,visibleNames:[{schemaName:a.schema,name:a.name}]};jh(this.db,e,o)}interpretAllMetadata(){let e=this.compiler.symbolMetadata(this.programSymbol)??[],n=new Set,r=new Set,i=new Map;for(let t of e){if(!(t instanceof Zm))continue;let e=t.table(this.compiler);if(!e)continue;let n=e.originalSymbol.intern(),r=i.get(n);r?r.count++:i.set(n,{count:1,tableName:e.name??``})}for(let a of e){let e=this.compiler.interpretMetadata(a,this.filepath);if(e.hasValue(t))continue;this.errors.push(...e.getErrors()),this.warnings.push(...e.getWarnings()),this.infos.push(...e.getInfos());let o=e.getValue();if(o!==void 0)switch(a.kind){case Wm.Ref:{let e=o,[t,r]=e.endpoints??[];if(t&&r){let e=[t.schemaName,t.tableName,t.fieldNames.join(`,`),r.schemaName,r.tableName,r.fieldNames.join(`,`)].join(`|`),i=[r.schemaName,r.tableName,r.fieldNames.join(`,`),t.schemaName,t.tableName,t.fieldNames.join(`,`)].join(`|`);if(n.has(e)||n.has(i)){this.errors.push(new T(w.SAME_ENDPOINT,`Ref with same endpoints already exists`,a.declaration));break}n.add(e)}this.db.refs.push(e);break}case Wm.Dep:{let e=o,t=a instanceof qm?a.edgeExpressions():[],n=-1;if((e.edges??[]).some((e,t)=>{let{upstream:i,downstream:a}=e,o=[i.schemaName,i.tableName,i.fieldNames.join(`,`),a.schemaName,a.tableName,a.fieldNames.join(`,`)].join(`|`);return r.has(o)?(n=t,!0):(r.add(o),!1)}),n>=0){let e=t[n]??a.declaration;this.errors.push(new T(w.SAME_ENDPOINT,`Dep with same endpoints already exists`,e));break}let i=Tg(e,a);if(i.length>0){this.errors.push(...i);break}this.db.deps.push(e);break}case Wm.Records:if(a instanceof Zm){let e=a.table(this.compiler)?.originalSymbol.intern()??``,t=e?i.get(e):void 0;if(t&&t.count>1){this.errors.push(new T(w.DUPLICATE_RECORDS_FOR_TABLE,`Duplicate Records blocks for the same Table '${t.tableName}' - A Table can only have one Records block`,a.declaration));break}}this.db.records.push(o);break;case Wm.Project:this.db.project=o;break;case Wm.MetadataElement:default:break}}}interpretAllAliases(){let e=this.compiler.symbolMembers(this.programSymbol).getFiltered(t)??[];for(let t of e){if(!(t instanceof cm)||!t.isKind(I.Table))continue;let{name:e,schema:n}=t.originalSymbol.interpretedName(this.compiler,this.filepath),r={name:t.name,kind:jm.Table,value:{tableName:e,schemaName:n}};this.db.aliases.push(r)}}validateRecords(){let e=[],n=new Map,r=this.compiler.symbolMembers(this.programSymbol).getFiltered(t)??[];for(let e of r){if(!(e instanceof Wp))continue;let r=this.compiler.symbolMembers(e).getFiltered(t)??[];for(let e of r){if(!e.isKind(I.Table))continue;let t=e.originalSymbol;if(!(t instanceof qp))continue;let r=t.intern();n.has(r)||n.set(r,{tableSymbol:t,record:void 0,recordBlock:t.declaration})}}let i=this.compiler.symbolMetadata(this.programSymbol);for(let r of i){if(!(r instanceof Zm))continue;let i=r.table(this.compiler);if(!(i instanceof qp))continue;let a=this.compiler.interpretMetadata(r,this.filepath);if(a.hasValue(t))continue;let o=a.getValue();if(!o)continue;e.push(...dg(this.compiler,i,r.declaration,o)),e.push(..._g(this.compiler,i,o));let s=i.originalSymbol.intern(),c=n.get(s);c?c.record=o:n.set(s,{tableSymbol:i,record:o,recordBlock:r.declaration})}let a=this.collectPartialRefs(n);return e.push(...xg(this.compiler,[...this.db.refs,...a],n,this.filepath)),e}validatePartialRefs(){let e=this.compiler.symbolMetadata(this.programSymbol).filter(e=>e instanceof Jm);for(let t of e)this.infos.push(...Ng(this.compiler,t))}collectPartialRefs(e){let t=this.compiler.symbolMetadata(this.programSymbol).filter(e=>e instanceof Jm),n=[];for(let{tableSymbol:r}of e.values())for(let e of r.resolvedPartials(this.compiler))for(let i of t){if(i.leftTablePartial(this.compiler)?.originalSymbol!==e.originalSymbol)continue;let t=i.leftColumns(this.compiler),a=i.rightTable(this.compiler),o=i.rightColumns(this.compiler),s=i.op(this.compiler);if(!a||!s||t.length===0||o.length===0)continue;let c=r.mergedColumns(this.compiler);if(!t.some(e=>c.some(t=>t instanceof um&&t.declaration===e.declaration)))continue;let l=uf(s);if(!l)continue;let u=a instanceof $p&&a.originalSymbol===e.originalSymbol?r:a,d=r.interpretedName(this.compiler,this.filepath),f=u.interpretedName(this.compiler,this.filepath),p={schemaName:d.schema,tableName:d.name,fieldNames:t.map(e=>e.name??``),relation:l[0],token:F(i.leftToken())},m={schemaName:f.schema,tableName:f.name,fieldNames:o.map(e=>e.name??``),relation:l[1],token:F(i.rightToken())};n.push({token:F(i.declaration),endpoints:[p,m]})}return n}pushElement(e,t){if(!Array.isArray(t))switch(e.kind){case I.Table:this.db.tables.push(t);break;case I.Enum:this.db.enums.push(t);break;case I.TableGroup:this.db.tableGroups.push(t);break;case I.TablePartial:this.db.tablePartials.push(t);break;case I.StickyNote:this.db.notes.push(t);break;case I.DiagramView:this.db.diagramViews.push(t);break;default:break}}},Bg={nodeSymbol(t,r){return Jf(r)?n.create(t.symbolFactory.create(sm,{declaration:r,name:r.filepath.absolute},r.filepath)):n.create(e)},symbolMembers(r,i){if(!i.isKind(I.Program))return n.create(e);if(!(i.declaration instanceof R))return n.create([]);let a=new Map;for(let e of r.reachableFiles(i.filepath)){let{schemaMembers:t}=r.usableMembers(e).getValue();for(let e of t)a.has(e.name)||a.set(e.name,e)}let o=a.get(y);if(!o)return n.create([...a.values()]);let s=r.symbolMembers(o).getFiltered(t)?.filter(e=>!e.isKind(I.Schema));return s?n.create([...a.values(),...s]):n.create([...a.values()])},bindNode(t,r){return Jf(r)?new kh(r,t).resolve():n.create(e)},interpretSymbol(t,r,i){if(!(r instanceof sm))return n.create(e);if(!(r.declaration instanceof R))return n.create(void 0);if(!Rm(t,r.declaration)){let e=t.reachableFiles(i).flatMap(e=>t.bindFile(e).getErrors());return n.create(void 0,e)}return new zg(t,r,i).interpret()}},Vg=class{constructor(e,t){this.compiler=e,this.declarationNode=t}bind(){return this.declarationNode.body instanceof U?this.bindBody(this.declarationNode.body):[]}bindBody(e){if(!e||e instanceof H)return[];let t=e.body.filter(e=>e instanceof z);return this.bindSubElements(t)}bindSubElements(e){return e.flatMap(e=>e.type?this.compiler.bindNode(e).getErrors():[])}},Hg=class{constructor(e,t,n){this.declarationNode=t,this.compiler=e,this.filepath=n,this.project={enums:[],refs:[],tableGroups:[],tables:[],tablePartials:[]}}interpret(){this.project.token=F(this.declarationNode);let e=[...this.interpretName(this.declarationNode.name),...this.interpretBody(this.declarationNode.body)];return new n(this.project,e)}interpretName(e){if(!e)return this.project.name=null,[];let{name:t}=Pp(e);return this.project.name=t,[]}interpretSubElement(e){let r=this.compiler.nodeSymbol(e).getFiltered(t);if(r){let e=this.compiler.interpretSymbol(r,this.filepath);if(!e.hasValue(t))return e}return n.create(void 0)}interpretBody(e){return e.body.flatMap(e=>{let t=e;switch(t.getElementKind()){case r.Table:{let e=this.interpretSubElement(t);return e.getValue()&&this.project.tables?.push(e.getValue()),e.getErrors()}case r.TableGroup:{let e=this.interpretSubElement(t);return e.getValue()&&this.project.tableGroups?.push(e.getValue()),e.getErrors()}case r.Enum:{let e=this.interpretSubElement(t);return e.getValue()&&this.project.enums?.push(e.getValue()),e.getErrors()}case r.Note:{let e=t.body instanceof U?t.body.body[0]?.callee:t.body?.callee,n=e?N(e):void 0;return n&&(this.project.note={value:Np(n),token:F(t)}),[]}case r.TablePartial:{let e=this.interpretSubElement(t);return e.getValue()&&this.project.tablePartials?.push(e.getValue()),e.getErrors()}default:return this.project[t.type.value.toLowerCase()]=N(t.body.callee),[]}})}},Ug={nodeMetadata(t,i){return A(i,r.Project)?n.create(new $m(i)):n.create(e)},bindNode(t,i){return A(i,r.Project)?n.create(void 0,new Vg(t,i).bind()):n.create(e)},interpretMetadata(t,r,i){return!(r instanceof $m)||!(r.declaration instanceof z)?n.create(e):new Hg(t,r.declaration,i).interpret()}},Wg=class{constructor(e,t){this.compiler=e,this.declarationNode=t,this.boundColumns=new Map}bind(){let e=[];return this.declarationNode.name&&e.push(...this.bindRecordsName(this.declarationNode.name)),this.declarationNode.body instanceof U&&e.push(...this.bindBody(this.declarationNode.body)),e}bindRecordsName(e){return this.declarationNode.parent instanceof R?this.bindTopLevelName(e):this.bindInsideTableName(e)}bindTopLevelName(e){let n=pp(e);if(!n)return[];let r=n.variables.pop(),i=n.variables;if(!r)return[];let a=this.compiler.nodeReferee(r).getErrors();if(a.length>0)return a;let o=this.compiler.nodeReferee(r).getFiltered(t);if(!o)return[];let s=op(o.declaration)??`<invalid name>`,c=i.flatMap(e=>this.compiler.nodeReferee(e).getErrors());for(let e of n.args){let n=mp(e)??`<unnamed>`,r=this.compiler.nodeReferee(e).getFiltered(t);if(!r){c.push(new T(w.BINDING_ERROR,`Column '${n}' does not exist in Table '${s}'`,e));continue}let i=this.boundColumns.get(r);i&&(c.push(new T(w.DUPLICATE_COLUMN_REFERENCES_IN_RECORDS,`Column '${n}' is referenced more than once in a Records for Table '${s}'`,i)),c.push(new T(w.DUPLICATE_COLUMN_REFERENCES_IN_RECORDS,`Column '${n}' is referenced more than once in a Records for Table '${s}'`,e))),this.boundColumns.set(r,e)}return c}bindInsideTableName(e){let n=this.declarationNode.parent;if(!(n instanceof z)||!n.isKind(r.Table)||!this.compiler.nodeSymbol(n).getFiltered(t)||!Bf(e))return[];let i=op(n)??`<invalid name>`,a=[];for(let n of e.elementList){let e=mp(n)??`<unnamed>`;if(!this.compiler.nodeReferee(n).getFiltered(t)){a.push(new T(w.BINDING_ERROR,`Column '${e}' does not exist in Table '${i}'`,n));continue}}return a}bindBody(e){if(!e)return[];if(e instanceof H)return this.bindDataRow(e);let t=e.body.filter(e=>e instanceof H),n=e.body.filter(e=>e instanceof z);return[...this.bindDataRows(t),...this.bindSubElements(n)]}bindDataRows(e){return e.flatMap(e=>this.bindDataRow(e))}bindDataRow(e){return e.callee?(e.callee instanceof xm?e.callee.elementList:[e.callee]).flatMap(zm).flatMap(e=>{let t=e.variables.pop(),n=e.variables.pop();return!t||!n?[]:[...e.variables.flatMap(e=>this.compiler.nodeReferee(e).getErrors()),...n?this.compiler.nodeReferee(n).getErrors():[],...this.compiler.nodeReferee(t).getErrors()]}):[]}bindSubElements(e){return e.flatMap(e=>e.type?this.compiler.bindNode(e).getErrors():[])}},Gg=class{constructor(e,t,n){this.compiler=e,this.metadata=t,this.element=t.declaration,this.filepath=n}interpret(){let e=[],t=[],r=Kg(this.element,this.compiler,this.filepath);if(!r||r.columns.length===0)return new n(void 0,e);let i=[];for(let n of this.element.body.body){let a=qg(n,r.columns,this.compiler);e.push(...a.getErrors()),t.push(...a.getInfos());let o=a.getValue();o.row&&i.push(o.row)}let a=F(this.element),o=this.metadata.example(this.compiler)||void 0;return new n({schemaName:r.schemaName,tableName:r.tableName,columns:r.columns.map(e=>e.name??``),values:i,example:o,token:a},e,[],t)}};function Kg(e,n,i){let a=e.name,o=e.parent;if(o instanceof z&&o.isKind(r.Table)){let e=n.nodeSymbol(o).getFiltered(t);if(!(e instanceof qp))return;let r=e.mergedColumns(n),s=e.interpretedName(n,i),c=s.schema,l=s.name;return a?{schemaName:c,tableName:l,columns:a.elementList.flatMap(e=>r.find(t=>t.name===P(e))||[])}:{schemaName:c,tableName:l,columns:r}}let s=pp(a);if(!s)return;let c=s.variables.map(e=>e.expression.variable?.value??``),l=c.at(-1)??``,u=c.length>1?c.slice(0,-1).join(`.`):void 0,d=n.parseFile(e.filepath).getValue().ast,f=n.nodeSymbol(d).getFiltered(t);if(!f)return;let p;if(u){let e=n.lookupMembers(f,I.Schema,u);e&&(p=n.lookupMembers(e,I.Table,l))}if(p||=n.lookupMembers(f,I.Table,l),!p)return;let m=p.originalSymbol;if(!(m instanceof qp))return;let ee=m.mergedColumns(n),te=s.args.map(e=>ee.find(t=>t.name===P(e))).filter(Boolean),ne=m.interpretedName(n,i);return{schemaName:ne.schema,tableName:ne.name,columns:te}}function qg(e,t,r){let i=[],a=[],o={},s=e.callee instanceof xm?e.callee.elementList:[e.callee];if(s.length!==t.length)return i.push(new T(w.INVALID_RECORDS_FIELD,`Expected ${t.length} values but got ${s.length}`,e)),new n({row:null,columnNodes:{}},i,[],a);let c=[];for(let e=0;e<t.length;e++){let n=s[e],l=t[e];o[l.name??``]=n;let u=Yg(n,l,r);i.push(...u.getErrors()),a.push(...u.getInfos());let d=u.getValue();c.push(d??{value:null,type:`expression`,token:F(n)})}return new n({row:c,columnNodes:o},i,[],a)}function Jg(e){return e instanceof ym&&e.value?.value||``}function Yg(e,t,r){let i=F(e),a=t.type(r),o=(a?.name??``).split(`(`)[0],s=!!a?.enumSymbol,c=t.increment(r),l=t.isNotNullSet(r)===!0,u=t.default(r),d=t.name??``,f=Uo(o,s),p=Ep(e),m=p===null?Jg(e):p,ee=p===null?`expression`:f;if(e instanceof ym)return new n({value:e.value?.value||``,type:`expression`,token:i},[],[]);if(gp(e)||_p(e)&&!zo(o)){let t=u&&u.value.toString().toLowerCase()!==`null`,r=Ho(o);return l&&!t&&!c&&!r?new n({value:null,type:f,token:i},[],[],[Hh(e,{colName:d})]):new n({value:null,type:f,token:i},[],[])}if(s){let a=t.type(r)?.enumSymbol,o=a?a.members(r).filter(e=>e.isKind(I.EnumField)).map(e=>e.name).filter(Boolean):[],s=N(e);return s===void 0&&(s=O(e)?e.expression.variable.value:void 0),o.includes(s)?new n({value:s,type:f,token:i},[],[]):new n({value:s,type:f,token:i},[],[],[Uh(e,{colName:d})])}if(Fo(o)||Io(o)||Ho(o)){let t=yp(e);if(t===null)return new n({value:m,type:ee,token:i},[],[],[Wh(e,{colName:d,typeName:o})]);if(Fo(o)&&!Number.isInteger(t))return new n({value:Math.floor(t),type:f,token:i},[],[],[Gh(e,{colName:d,value:t})]);let r=Io(o)&&a?.args?.length?{precision:Number(a.args[0]),scale:a.args.length>=2?Number(a.args[1]):0}:void 0;if(Io(o)&&r){let{precision:a,scale:s}=r,c=t.toString().split(`.`),l=c[0].replace(/^-/,``),u=c[1]||``,p=l.length+u.length,m=u.length;if(p>a)return new n({value:t,type:f,token:i},[],[],[Kh(e,{colName:d,typeName:o,value:t,precision:a,scale:s,totalDigits:p})]);if(m>s)return new n({value:t,type:f,token:i},[],[],[qh(e,{colName:d,typeName:o,value:t,precision:a,scale:s,decimalDigits:m})])}return new n({value:t,type:f,token:i},[],[])}if(Ro(o)){let t=wp(e);return t===null?new n({value:m,type:ee,token:i},[],[],[Jh(e,{colName:d})]):new n({value:t,type:f,token:i},[],[])}if(Vo(o)){let t=Ap(e);return t===null?new n({value:m,type:ee,token:i},[],[],[Yh(e,{colName:d})]):new n({value:t,type:f,token:i},[],[])}if(zo(o)){let t=Ep(e);if(t===null)return new n({value:m,type:ee,token:i},[],[],[Xh(e,{colName:d})]);let r=a?.args?.length?Number(a.args[0]):void 0;if(r&&!Number.isNaN(r)){let s=r,c=new TextEncoder().encode(t).length;if(c>s)return new n({value:t,type:f,token:i},[],[],[Zh(e,{colName:d,typeName:o,typeArg:String(a.args[0]),maxLength:s,actualLength:c})])}return new n({value:t,type:`string`,token:i},[],[])}return new n({value:m,type:ee,token:i},[],[])}var Xg={nodeMetadata(t,i){if(!A(i,r.Records))return n.create(e);let a=i.parent;if(a&&A(a,r.Table))return n.create(new Zm(i));let o=i.name;return o instanceof Cm&&o.callee?n.create(new Zm(i)):n.create(e)},nodeReferee(i,a){if(!O(a))return n.create(e);let o=a.parentOfKind(z);if(!o?.isKind(r.Records))return n.create(e);let s=i.parseFile(a.filepath).getValue().ast,c=i.nodeSymbol(s).getFiltered(t);if(!c)return n.create(void 0);let l=a.parentOfKind(bm);if(l?.parentNode===o)return Zg(i,o,a);let u=a.parentOfKind(Cm);return u?.parentNode===o&&l?.parentNode===u?Qg(i,u,a):u?.parentNode===o&&u.callee?.containsEq(a)?$g(i,c,a):t_(i,c,a)},bindNode(t,i){return A(i,r.Records)?n.create(void 0,new Wg(t,i).bind()):n.create(e)},interpretMetadata(t,r,i){return r instanceof Zm?r.declaration instanceof z?new Gg(t,r,i).interpret():n.create(void 0):n.create(e)}};function Zg(e,i,a){let o=i.parent;if(o instanceof z&&o.isKind(r.Table)){let n=e.nodeSymbol(o).getFiltered(t);if(n)return e_(e,n,a)}return new n(void 0)}function Qg(e,r,i){if(r.callee){let n;if(O(r.callee))n=e.nodeReferee(r.callee).getFiltered(t);else{let i=lp(r.callee);if(i&&i.length>0){let r=i[i.length-1];n=e.nodeReferee(r).getFiltered(t)}}if(n)return e_(e,n,i)}return new n(void 0)}function $g(e,t,r){if(!O(r))return new n(void 0);let i=mp(r)??``;if(!k(r.parentNode)){let a=e.lookupMembers(t,[I.Table,I.Schema],i);return a?n.create(a):new n(void 0,[new T(w.BINDING_ERROR,`Table '${i}' does not exist in Schema 'public'`,r)])}let a=Bm(e,r);if(!a){let a=e.lookupMembers(t,[I.Table,I.Schema],i);return a?n.create(a):new n(void 0,[new T(w.BINDING_ERROR,`Table or schema '${i}' does not exist`,r)])}if(a.isKind(I.Schema)){let t=e.lookupMembers(a,[I.Table,I.Schema],i);return t?n.create(t):new n(void 0,[new T(w.BINDING_ERROR,`Table or schema '${i}' does not exist`,r)])}return new n(void 0)}function e_(e,t,r){if(!O(r))return new n(void 0);let i=mp(r)??``,a=e.lookupMembers(t,I.Column,i);return a?n.create(a):new n(void 0,[new T(w.BINDING_ERROR,`Column '${i}' does not exist in Table 'public.${t.name}'`,r)])}function t_(e,t,r){if(!O(r))return new n(void 0);let i=mp(r)??``;if(!k(r.parentNode))return new n(void 0);let a=Bm(e,r);if(a){if(a.isKind(I.Schema)){let t=e.lookupMembers(a,[I.Enum,I.Schema],i);return t?n.create(t):new n(void 0,[new T(w.BINDING_ERROR,`Enum or schema '${i}' does not exist`,r)])}if(a.isKind(I.Enum)){let t=e.lookupMembers(a,I.EnumField,i);return t?n.create(t):new n(void 0,[new T(w.BINDING_ERROR,`Enum field '${i}' does not exist in Enum 'public.${a.name}'`,r)])}return new n(void 0)}let o=r.parentNode;if(o.leftExpression===r){if(k(o.parentNode)&&o.parentNode.leftExpression===o){let a=e.lookupMembers(t,I.Schema,i);return a?n.create(a):new n(void 0,[new T(w.BINDING_ERROR,`Schema '${i}' does not exist in Schema 'public'`,r)])}let a=e.lookupMembers(t,I.Enum,i);return a?n.create(a):new n(void 0,[new T(w.BINDING_ERROR,`Enum '${i}' does not exist in Schema 'public'`,r)])}return new n(void 0)}var n_=class{constructor(e,t){this.compiler=e,this.declarationNode=t}bind(){return!(this.declarationNode.parent instanceof R)&&!this.declarationNode.parent?.isKind(r.Project)?[]:this.bindBody(this.declarationNode.body)}bindBody(e){if(!e)return[];if(e instanceof H)return this.bindFields([e]);let[t,n]=_(e.body,e=>e instanceof H);return[...this.bindFields(t),...this.bindSubElements(n)]}bindFields(e){return e.flatMap(e=>e.callee?[e.callee,...e.args].flatMap(zm).flatMap(e=>{let t=e.tupleElements.length?e.tupleElements:e.variables.pop(),n=e.variables.pop();return!t||!n?[]:(Array.isArray(t)||(t=[t]),[...e.variables,n,...t].flatMap(e=>this.compiler.nodeReferee(e).getErrors()))}):[])}bindSubElements(e){return e.flatMap(e=>e.type?this.compiler.bindNode(e).getErrors():[])}};function r_(e,t){if(e.length===0)return[];let n;switch(t){case Vp.Table:n=[...Array(e.length-1).fill(I.Schema),I.Table];break;case Vp.Column:n=e.length===1?[I.Column]:[...Array(e.length-2).fill(I.Schema),I.Table,I.Column];break;case Vp.TableGroup:n=[...Array(e.length-1).fill(I.Schema),I.TableGroup];break;case Vp.Note:n=[...Array(e.length-1).fill(I.Schema),I.StickyNote];break;default:break}return e.map((e,t)=>({name:e,symbolKind:n[t]}))}function i_(e,t){let n=[],r=new Set,i=t=>{let a=t.intern();if(!r.has(a)){r.add(a);for(let n of e.fileDependencies(t))i(n);n.push(t)}};i(t);let a=new Map;return n.forEach((e,t)=>a.set(e.intern(),t)),a}function a_(e,n,r,i,a){let o=Um(e,a);if(!o||!e.isSymbolVisible(o,r))return;let s=new Set(e.reachableFiles(o.filepath).map(e=>e.absolute)),c=e.symbolMetadata(r).filter(e=>e instanceof Qm).filter(e=>s.has(e.declaration.filepath.absolute));if(!c.length)return;let l=i_(e,a),u=[...c].sort((e,t)=>{let n=l.get(e.declaration.filepath.intern())??-1,r=l.get(t.declaration.filepath.intern())??-1;return n===r?e.declaration.start-t.declaration.start:n-r}).map(n=>e.interpretMetadata(n,a).getFiltered(t)).filter(e=>!!e);if(!u.length)return;let d={};for(let e of u)for(let[t,{value:n,token:r}]of Object.entries(e))d[t.toLowerCase()]={originalKey:t,value:n,token:r};let f=n.metadata?Object.fromEntries(Object.entries(n.metadata).map(([e,t])=>[e.toLowerCase(),{originalKey:e,value:t}])):{};Object.entries(d).forEach(([e,t])=>{let{originalKey:r,value:a,token:o}=t;i[e]?i[e].assignBuiltinField(n,a,o):f[e]={originalKey:r,value:a}}),n.metadata=Object.fromEntries(Object.values(f).map(e=>[e.originalKey,e.value]))}var o_={[i.Color]:{isValidBuiltinFieldValue:Pf,message:`'color' must be a color literal`,assignBuiltinField(e,t){e.color=t}}},s_=[i.Color,i.Delete,i.Update,i.Inactive],c_=class{constructor(e,t,n){this.compiler=e,this.filepath=n,this.metadata=t,this.declarationNode=t.declaration,this.ref={}}interpret(){this.ref.token=F(this.declarationNode);let e=[...this.interpretName(),...this.interpretBody()],r=this.compiler.nodeSymbol(this.declarationNode).getFiltered(t);r instanceof Qp&&a_(this.compiler,this.ref,r,o_,this.filepath);let{infos:i}=this.validateRefConstraints();return n.create(this.ref,e,void 0,i)}interpretName(){if(!(this.declarationNode instanceof z))return[];let e=[],t=M(this.declarationNode.name)??[];return this.ref.name=t.pop()||null,t.length>1&&e.push(new T(w.UNSUPPORTED,`Nested schema is not supported`,this.declarationNode.name)),this.ref.schemaName=t.join(`.`)||null,e}interpretBody(){let e=this.metadata.op(this.compiler),t=this.metadata.leftColumns(this.compiler),n=this.metadata.leftTable(this.compiler),r=this.metadata.rightColumns(this.compiler),i=this.metadata.rightTable(this.compiler);if(La(t,r).every(([e,t])=>e?.originalSymbol===t?.originalSymbol))return[new T(w.SAME_ENDPOINT,`Two endpoints are the same`,this.declarationNode)];let a=uf(e),o=n?.interpretedName(this.compiler,this.filepath),s=i?.interpretedName(this.compiler,this.filepath),c=F(this.metadata.leftToken()),l=F(this.metadata.rightToken());if(this.ref.endpoints=this.declarationNode instanceof z?[{schemaName:o?.schema??null,tableName:o?.name??``,fieldNames:t.map(e=>e.name??``),relation:a[0],token:c},{schemaName:s?.schema??null,tableName:s?.name??``,fieldNames:r.map(e=>e.name??``),relation:a[1],token:l}]:[{schemaName:s?.schema??null,tableName:s?.name??``,fieldNames:r.map(e=>e.name??``),relation:a[1],token:l},{schemaName:o?.schema??null,tableName:o?.name??``,fieldNames:t.map(e=>e.name??``),relation:a[0],token:c}],!(this.declarationNode instanceof z))return[];let u=cp(this.declarationNode)[0];if(u.args[0]){let e=E(u.args[0]).getValue(),t=e.delete?.at(0)?.value;this.ref.onDelete=t instanceof gm?ap(t):P(t);let n=e.update?.at(0)?.value;this.ref.onUpdate=n instanceof gm?ap(n):P(n),this.ref.color=e.color?.length?Fp(e.color?.at(0)?.value):void 0,this.ref.inactive=e.inactive?.length?!0:void 0,this.ref.metadata=Rp(e,s_)}return[]}validateRefConstraints(){return this.metadata.cardinalities(this.compiler)?{infos:[...Mg(this.compiler,this.metadata,`right`),...Mg(this.compiler,this.metadata,`left`)]}:{infos:[]}}};function l_(e){let t=e.parent;for(;t;){if(t instanceof z&&t.isKind(r.Ref))return t.body?.containsEq(e)??!1;t=t.parent}return!1}var u_={getDuplicateError(e,t,n){return new T(w.DUPLICATE_NAME,`Ref '${e}' already exists in schema '${t}'`,n)}},d_={nodeSymbol(i,a){if(!A(a,r.Ref)||!(a.parentNode instanceof R))return n.create(e);let o=i.nodeFullname(a).getFiltered(t)?.at(-1);return o===void 0?n.create(e):new n(i.symbolFactory.create(Qp,{declaration:a,name:o},a.filepath))},symbolMembers(t,r){return r.isKind(I.Ref)?new n([]):n.create(e)},nodeReferee(r,i){if(!O(i)&&!k(i)||!l_(i)||i.parentOfKind(B))return n.create(e);let a=r.parseFile(i.filepath).getValue().ast,o=r.nodeSymbol(a).getValue();return o===t?n.create(void 0):f_(r,o,i)},nodeMetadata(t,a){if(A(a,r.Ref)){let t=cp(a)[0];if(!(t instanceof H)||!t.callee)return n.create(e);let r=t.callee;return!(r instanceof _m)||!r.op?n.create(e):n.create(new Km(a))}if(a instanceof B){if(!(a.name instanceof gm)||ap(a.name)?.toLowerCase()!==i.Ref)return n.create(e);let t=a.parent;return t instanceof z&&t.isKind(r.TablePartial)?n.create(new Jm(a)):n.create(new Km(a))}return n.create(e)},bindNode(t,i){return A(i,r.Ref)?n.create(void 0,new n_(t,i).bind()):n.create(e)},interpretMetadata(t,r,i){return r instanceof Km?new c_(t,r,i).interpret():n.create(e)}};function f_(e,r,i){if(!O(i))return new n(void 0);let a=mp(i)??``;if(!k(i.parentNode)){let r=i.parentNode;if(r&&k(r.parentNode)&&r.parentNode.rightExpression===r){let o=r.parentNode.leftExpression,s=k(o)?o.rightExpression??o:o,c=e.nodeReferee(s).getFiltered(t);if(c?.isKind(I.Table)){let r=e.lookupMembers(c,I.Column,a);if(r)return n.create(r);let o=c.declaration?e.nodeFullname(c.declaration).getFiltered(t)?.join(`.`)??c.name:c.name;return new n(void 0,[new T(w.BINDING_ERROR,`Column '${a}' does not exist in Table '${o}'`,i)])}}return n.create(void 0)}let o=Bm(e,i);if(o){if(o.isKind(I.Schema)){let t=e.lookupMembers(o,[I.Table,I.Schema],a);return t?n.create(t):new n(void 0,[new T(w.BINDING_ERROR,`Table or schema '${a}' does not exist`,i)])}if(o.isKind(I.Table)){let r=e.lookupMembers(o,I.Column,a);return r?n.create(r):new n(void 0,[new T(w.BINDING_ERROR,`Column '${a}' does not exist in Table '${o.declaration?e.nodeFullname(o.declaration).getFiltered(t)?.join(`.`)??o.name:o.name}'`,i)])}return new n(void 0)}let s=i.parentNode;if(s.leftExpression===i){if(k(s.parentNode)&&s.parentNode.leftExpression===s){let t=e.lookupMembers(r,I.Schema,a);return t?n.create(t):new n(void 0,[new T(w.BINDING_ERROR,`Schema '${a}' does not exist`,i)])}let t=Hm(e,r);if(t){let r=e.lookupMembers(t,I.Table,a);if(r)return n.create(r)}let o=e.lookupMembers(r,I.Table,a);return o?n.create(o):new n(void 0,[new T(w.BINDING_ERROR,`Table '${a}' does not exist in Schema 'public'`,i)])}return new n(void 0)}var p_={visibleName(e,n){let r=n instanceof L?n:n.useSpecifierDeclaration instanceof mm?n.useSpecifierDeclaration:n.declaration;if(r instanceof mm&&n instanceof Up&&r.getSymbolKind()!==n.kind&&(r=n.declaration),!r)return;let i=e.nodeAlias(r).getFiltered(t);return i===void 0?e.nodeFullname(r).getFiltered(t):[i]}},m_={nodeSymbol(r,i){if(!qf(i)||!i.name)return n.create(e);let a=i.getSymbolKind();if(a===void 0||a===I.Schema)return n.create(e);let o=r.nodeReferee(i.name).getFiltered(t);return o&&!o.canBeImported?n.create(e):n.create(r.symbolFactory.create(lm,{useSpecifierDeclaration:i,declaration:o?.declaration,usedSymbol:o,kind:a,name:p_.visibleName(r,i)?.at(-1)},i.filepath),o===void 0?[new T(w.BINDING_ERROR,`Failed to resolve the import of ${a} '${(M(i.name)||[]).map(R_).join(`.`)}'`,i)]:[])},symbolMembers(r,i){if(!(i instanceof lm))return n.create(e);if(i.originalSymbol===i)return n.create([]);let a=r.symbolMembers(i.originalSymbol).getFiltered(t);return n.create(a??[])},nodeReferee(r,i){if(!O(i)&&!Gf(i))return n.create(e);let a=i.parentOfKind(mm);if(!a)return n.create(e);let o=a.name===i||k(a.name)&&a.name.rightExpression===i?a.getSymbolKind():I.Schema;if(o===void 0)return n.create(void 0);let s=M(i.parentOfKind(_m)),c=P(i)??s?.at(-1);if(c===void 0)return n.create(void 0);let l=i.parentOfKind(pm);if(l?.importPath?.value===void 0)return n.create(void 0);let u=r.layout.resolveFileSpecifier(i.filepath,l.importPath.value);if(!u)return n.create(void 0,[new T(w.BINDING_ERROR,`Import path must be relative, got '${l.importPath.value}'`,i)]);if(!r.layout.exists(u))return n.create(void 0,[new T(w.NONEXISTENT_MODULE,`${o} '${s?.join(`.`)??c}' does not exist in file ${u.toKey()}. Does the file exist?`,i)]);let d=i.parentOfKind(_m)?.leftExpression;if(d&&d!==i){let e=r.nodeReferee(d).getFiltered(t);if(!e)return n.create(void 0);let a=r.lookupMembers(e,o,c);return a?n.create(a):new n(void 0,[new T(w.BINDING_ERROR,`Could not find ${o} '${c}'`,i)])}let f=h_(r,u,c,o);return f?n.create(f):n.create(void 0,[new T(w.BINDING_ERROR,`${o} '${c}' does not exist in file ${u.toKey()}`,i)])},bindNode(t,r){if(Kf(r)){let e=[];if(r.importPath?.value){let n=t.layout.resolveFileSpecifier(r.filepath,r.importPath.value);(!n||!t.layout.exists(n))&&e.push(new T(w.NONEXISTENT_MODULE,`Failed to resolve the non-existent file '${r.importPath.value}'`,r.importPath))}if(r.specifiers){let n=t.bindNode(r.specifiers);e.push(...n.getErrors())}return new n(void 0,e)}return r instanceof hm?new n(void 0,r.specifiers.flatMap(e=>t.bindNode(e).getErrors())):qf(r)?r.name?new n(void 0,t.nodeReferee(r.name).getErrors()):new n(void 0):n.create(e)},interpretSymbol(r,i,a){if(!(i instanceof lm))return n.create(e);if(!i.usedSymbol)return n.create(void 0);let o=r.interpretSymbol(i.usedSymbol,a);if(o.hasValue(t))return n.create(void 0);let s=o.getValue();if(!s||Array.isArray(s))return o;let c=i.name;return c&&`name`in s?n.create({...s,name:c}):n.create(s)}};function h_(e,n,r,i,a=new Set){if(!n||a.has(n.intern()))return;a.add(n.intern());let o=e.usableMembers(n).getFiltered(t);if(!o)return;let s=o.nonSchemaMembers.find(e=>e.name===r&&e.isKind(i));if(s)return s;let c=o.schemaMembers.find(e=>e.name===r);if(c?.isKind(I.Schema))return c;for(let t of o.reuses.selective)if(M(t.alias??t.name)?.at(-1)===r){let o=t.parentOfKind(pm);if(o?.importPath?.value){let s=h_(e,e.layout.resolveFileSpecifier(n,o.importPath.value),M(t.name)?.at(-1)??r,i,a);if(s)return s}}for(let{importPath:t}of o.reuses.wildcard){let n=h_(e,t,r,i,a);if(n)return n}}function g_(e){if(e instanceof bo){let{ast:n}=this.parseFile(e).getValue(),r=this.nodeSymbol(n).getFiltered(t);return this.usableMembers(r)}let r=e.filepath,i=v_(this,r),{nonSchemaMembers:a,schemaMembers:o}=e instanceof sm?y_(this,e,i):b_(this,e,i);return new n({nonSchemaMembers:a,schemaMembers:o,reuses:{selective:i.selectiveReuses,wildcard:i.wildcardReuses},uses:{selective:i.selectiveUses,wildcard:i.wildcardUses}},i.parseResult.getErrors(),i.parseResult.getWarnings())}function __(e,n,r){let i=n.qualifiedName,a;if(a=r instanceof mm?p_.visibleName(e,r):e.nodeFullname(r).getFiltered(t),!a)return{kind:`none`};let o=r instanceof mm&&r.getSymbolKind()===I.Schema?a:a.length<=1?[y]:a.slice(0,-1);return o.length<i.length||!i.every((e,t)=>e===o[t])?{kind:`none`}:o.length===i.length?{kind:`direct`}:{kind:`child`,schemaName:o[i.length]}}function v_(e,t){let n=e.parseFile(t),{ast:r}=n.getValue(),i=[],a=[],o=[],s=[];for(let n of r.body){if(!(n instanceof pm)||!n.specifiers||!n.importPath)continue;let r=e.layout.resolveFileSpecifier(t,n.importPath.value);r&&(n.isReuse?n.specifiers instanceof Tm?i.push({importPath:r,node:n.specifiers}):a.push(...n.specifiers.specifiers):n.specifiers instanceof Tm?o.push({importPath:r,node:n.specifiers}):s.push(...n.specifiers.specifiers))}return{ast:r,parseResult:n,selectiveReuses:a,wildcardReuses:i,selectiveUses:s,wildcardUses:o}}function y_(e,n,r){let{ast:i,selectiveUses:a,selectiveReuses:o}=r,s=n.filepath,c=new Map([[y,e.symbolFactory.create(Wp,{name:y},s)]]);for(let n of i.declarations){let r=e.nodeFullname(n).getFiltered(t)||[],i=r.length<=1?y:r[0];c.has(i)||c.set(i,e.symbolFactory.create(Wp,{name:i},s))}x_(e,[...a,...o],c,s);for(let t of e.reachableFiles(s))for(let n of C_(e,t))c.has(n)||c.set(n,e.symbolFactory.create(Wp,{name:n},s));let l=[...c.values()],u=[],d=l.find(e=>e.isPublicSchema());if(d){let n=e.usableMembers(d).getFiltered(t);n&&u.push(...n.nonSchemaMembers)}return{nonSchemaMembers:u,schemaMembers:l}}function b_(e,n,r){let{ast:i,selectiveUses:a,selectiveReuses:o}=r,s=[],c=new Map;for(let r of i.body){if(!(r instanceof z))continue;let i=__(e,n,r),a=e.nodeAlias(r).getFiltered(t),o=e.nodeSymbol(r).getFiltered(t);if(a!==void 0&&n.isPublicSchema()&&o&&s.push(e.symbolFactory.create(cm,{kind:o.kind,declaration:r,aliasedSymbol:o,name:a},n.filepath)),i.kind!==`none`)if(i.kind===`direct`){let n=e.nodeSymbol(r).getFiltered(t);if(!n)continue;s.push(n)}else c.has(i.schemaName)||c.set(i.schemaName,e.symbolFactory.create(Wp,{name:i.schemaName,parent:n},n.filepath))}S_(e,[...a,...o],n,c);let l=n.qualifiedName;for(let t of e.reachableFiles(n.filepath))for(let r of w_(e,t,l))c.has(r)||c.set(r,e.symbolFactory.create(Wp,{name:r,parent:n},n.filepath));return{nonSchemaMembers:s,schemaMembers:[...c.values()]}}function x_(e,t,n,r){for(let i of t){if(i.getSymbolKind()!==I.Schema)continue;let t=p_.visibleName(e,i)?.at(0);t!==void 0&&!n.has(t)&&n.set(t,e.symbolFactory.create(Wp,{name:t},r))}}function S_(e,t,n,r){let i=n.qualifiedName;for(let a of t){if(a.getSymbolKind()!==I.Schema)continue;let t=p_.visibleName(e,a);if(!t||t.length<=i.length||!i.every((e,n)=>e===t[n]))continue;let o=t[i.length];o&&!r.has(o)&&r.set(o,e.symbolFactory.create(Wp,{name:o,parent:n},n.filepath))}}function C_(e,n){let{ast:r}=e.parseFile(n).getValue(),i=[];for(let n of r.declarations){let r=e.nodeFullname(n).getFiltered(t)||[],a=r.length<=1?y:r[0];i.push(a)}let a=v_(e,n);for(let t of[...a.selectiveUses,...a.selectiveReuses]){if(t.getSymbolKind()!==I.Schema)continue;let n=p_.visibleName(e,t)?.at(0);n!==void 0&&i.push(n)}return i}function w_(e,n,r){let{ast:i}=e.parseFile(n).getValue(),a=[];for(let n of i.declarations){let i=e.nodeFullname(n).getFiltered(t)||[],o=i.length<=1?[y]:i.slice(0,-1);o.length>r.length&&r.every((e,t)=>e===o[t])&&a.push(o[r.length])}let o=v_(e,n);for(let t of[...o.selectiveUses,...o.selectiveReuses]){if(t.getSymbolKind()!==I.Schema)continue;let n=p_.visibleName(e,t);!n||n.length<=r.length||r.every((e,t)=>e===n[t])&&a.push(n[r.length])}return a}var T_=class{constructor(e,t){this.compiler=e,this.declarationNode=t}bind(){let e=[];return this.declarationNode.attributeList&&e.push(...this.bindHeaderSettings(this.declarationNode.attributeList)),this.declarationNode.body instanceof U&&e.push(...this.bindBody(this.declarationNode.body)),e}bindHeaderSettings(e){return E(e).getValue().dep?.flatMap(e=>e.value?this.bindInlineDepValue(e.value):[])??[]}bindInlineDepValue(e){return zm(e).flatMap(e=>[...e.variables,...e.tupleElements].flatMap(e=>this.compiler.nodeReferee(e).getErrors()))}bindBody(e){if(!e)return[];if(e instanceof H)return this.bindFields([e]);let[t,n]=_(e.body,e=>e instanceof H);return[...this.bindFields(t),...this.bindSubElements(n)]}bindFields(e){return(e=>e.flatMap(e=>{if(!e.callee)return[];let t=[],n=[e.callee,...e.args];if(g(n)instanceof W){let e=E(g(n)).getValue();t.push(...e.ref?.flatMap(e=>e.value?this.bindInlineRef(e.value):[])||[]),t.push(...e.dep?.flatMap(e=>e.value?this.bindInlineDepValue(e.value):[])||[]),t.push(...e.default?.flatMap(e=>e.value?this.tryToBindEnumFieldRef(e.value):[])||[]),n.pop()}return n[1]&&this.tryToBindColumnType(n[1]),t}))(e.filter(e=>!jf(e.callee)))}tryToBindColumnType(e){let t=up(e);if(!t)return;let n=t.variables.pop();n&&this.compiler.nodeReferee(n).getErrors()}tryToBindEnumFieldRef(e){if(D(e))return[];if(O(e)){let t=e.expression.variable?.value?.toLowerCase();if(t&&So.includes(t))return[]}let t=up(e);if(!t){if(k(e)){let t=e,n=[];return t.leftExpression&&n.push(...this.compiler.nodeReferee(t.leftExpression).getErrors()),t.rightExpression&&n.push(...this.compiler.nodeReferee(t.rightExpression).getErrors()),n}return[]}let n=t.variables.pop(),r=t.variables.pop();if(!n||!r)return[];let i=[],a=t.variables;for(let e of a)i.push(...this.compiler.nodeReferee(e).getErrors());return i.push(...this.compiler.nodeReferee(r).getErrors()),i.push(...this.compiler.nodeReferee(n).getErrors()),i}bindInlineRef(e){return zm(e).flatMap(e=>{let t=e.variables.pop(),n=e.variables.pop();if(!t)return[];let r=e.variables;if(n){let e=[];for(let t of r)e.push(...this.compiler.nodeReferee(t).getErrors());return e.push(...this.compiler.nodeReferee(n).getErrors()),e.push(...this.compiler.nodeReferee(t).getErrors()),e}return this.compiler.nodeReferee(t).getErrors()})}bindSubElements(e){return e.flatMap(e=>e.type?this.compiler.bindNode(e).getErrors():[])}},E_=class{constructor(e,t,n,r){this.compiler=e,this.declarationNode=t,this.symbol=n,this.filepath=r,this.table={name:void 0,schemaName:void 0,alias:null,fields:[],token:void 0,indexes:[],partials:[],checks:[]},this.pkColumns=[]}interpret(){this.table.token=this.symbol?.token??F(this.declarationNode);let e=[];if(this.symbol){let{name:e,schema:t}=this.symbol.interpretedName(this.compiler,this.filepath);this.table.name=e,this.table.schemaName=t}else e.push(...this.interpretName(this.declarationNode.name));let t=[...e,...this.interpretAlias(this.declarationNode.alias),...this.interpretSettingList(this.declarationNode.attributeList),...this.interpretBody(this.declarationNode.body)];return this.pkColumns.length>=2&&(this.table.indexes.push({columns:this.pkColumns.map(({name:e,token:t})=>({value:e,type:`column`,token:t})),token:{start:{offset:-1,line:-1,column:-1},end:{offset:-1,line:-1,column:-1},filepath:this.declarationNode.filepath},pk:!0}),this.pkColumns.forEach(e=>{e.pk=!1})),this.attachTableAndColumnMetadata(),n.create(this.table,t)}attachTableAndColumnMetadata(){if(!this.symbol)return;a_(this.compiler,this.table,this.symbol,O_,this.filepath);let e=this.compiler.symbolMembers(this.symbol).getFiltered(t)??[];for(let t of e){if(!t.isKind(I.Column))continue;let e=this.table.fields.find(e=>e.name===t.name);e&&a_(this.compiler,e,t,k_,this.filepath)}}interpretName(e){let{name:t,schemaName:n}=Pp(e);return n.length>1?(this.table.name=t,this.table.schemaName=n.join(`.`),[new T(w.UNSUPPORTED,`Nested schema is not supported`,e)]):(this.table.name=t,this.table.schemaName=n.length?n[0]:null,[])}interpretAlias(e){if(!e)return[];let t=mp(e);return t&&(this.table.alias=t),[]}interpretSettingList(e){let t=E(e).getValue();this.table.headerColor=t[i.HeaderColor]?.length?Fp(t[i.HeaderColor]?.at(0)?.value):void 0;let[n]=t[i.Note]||[];return this.table.note=n&&{value:Np(N(n?.value)),token:F(n)},this.table.metadata=Rp(t,Object.keys(O_)),[]}interpretBody(e){let[t,n]=_(e.body,e=>e instanceof H);return[...this.interpretFields(t),...this.interpretSubElements(n)]}interpretSubElements(e){return e.flatMap(e=>{switch(e.getElementKind()){case r.Note:return this.table.note={value:Np(N(e.body instanceof U?e.body.body[0].callee:e.body.callee)),token:F(e)},[];case r.Indexes:return this.interpretIndexes(e);case r.Checks:return this.interpretChecks(e);case r.Records:return[];default:return[]}})}interpretInjection(e,n){let r=[],i={order:n,token:F(e)};i.name=P(e.expression)||``;let a=e.expression?this.compiler.nodeReferee(e.expression).getFiltered(t):void 0,o=this.compiler.parseFile(this.filepath).getValue().ast,s=this.compiler.nodeSymbol(o).getFiltered(t);if(a&&!s.inNestedSchema(this.compiler,a)){let n=this.compiler.nodeSymbol(this.declarationNode).getFiltered(t),o=n?(this.compiler.symbolUses(n).getFiltered(t)??[]).find(e=>e.filepath.equals(this.filepath)):void 0;r.push(new T(w.UNSUPPORTED,`TablePartial ${i.name} from '${a.filepath}' should be imported. Currently, importing a table that uses a table partial requires that table partial to also be in scope.`,o?.useSpecifierDeclaration??e))}return this.table.partials.push(i),r}interpretFields(e){let n=this.symbol??this.compiler.nodeSymbol(this.declarationNode).getFiltered(t),r=(n?this.compiler.symbolMembers(n).getFiltered(t)??[]:[]).filter(e=>e.isKind(I.Column)).length?[]:[new T(w.EMPTY_TABLE,`A Table must have at least one column`,this.declarationNode)],i=e.flatMap((e,t)=>jf(e.callee)?this.interpretInjection(e.callee,t):this.interpretColumn(e));return[...r,...i]}interpretColumn(e){let n=[],r={},a=this.compiler.nodeSymbol(e).getFiltered(t);r.name=mp(e.callee),r.token=F(e);let o=Lp(this.compiler,e.args[0]);r.type=o.getValue(),n.push(...o.getErrors()),r.pk=a?.pk(this.compiler)||!1,r.unique=a?.isUniqueSet(this.compiler)||!1,r.increment=a?.isIncrementSet(this.compiler)||void 0,r.not_null=a?.isNotNullSet(this.compiler),r.dbdefault=a?.default(this.compiler),r.note=a?.note(this.compiler);let s=this.compiler.nodeSettings(e).getFiltered(t)??{};r.metadata=Rp(s,D_);let c=this.compiler.parseFile(this.filepath).getValue().ast,l=this.compiler.nodeSymbol(c).getFiltered(t);return r.inline_refs=(s[i.Ref]||[]).flatMap(e=>{let r=this.compiler.nodeMetadata(e).getFiltered(t);if(!r)return[];let i=r.owners(this.compiler);if(l&&i.length>0&&!i.some(e=>e===l))return[];let a=this.compiler.interpretMetadata(r,this.filepath);if(a.hasValue(t))return[];n.push(...a.getErrors());let o=a.getValue();if(!o?.endpoints?.[0])return[];let s=o.endpoints[0],c=r.op(this.compiler);return c?{schemaName:s.schemaName,tableName:s.tableName,fieldNames:s.fieldNames,relation:c,token:s.token}:[]}),r.inline_deps=(s[i.Dep]||[]).flatMap(e=>{let r=this.compiler.nodeMetadata(e).getFiltered(t);if(!r)return[];let i=r.owners(this.compiler);if(l&&i.length>0&&!i.some(e=>e===l))return[];let a=this.compiler.interpretMetadata(r,this.filepath);if(a.hasValue(t))return[];n.push(...a.getErrors());let o=a.getValue();if(!o?.edges?.[0])return[];let s=e.value;if(!(s instanceof V))return[];let c=s.op?.value;if(c!==`->`&&c!==`<-`)return[];let u=o.edges[0],d=c===`->`?u.downstream:u.upstream;return{schemaName:d.schemaName,tableName:d.tableName,fieldNames:d.fieldNames,direction:c,token:d.token}}),r.checks=(s[i.Check]||[]).map(e=>({token:F(e),expression:e.value.value.value})),this.table.fields.push(r),r.pk&&this.pkColumns.push(r),n}interpretIndexes(e){let n=this.compiler.nodeMetadata(e).getFiltered(t);if(!n)return[];let r=this.compiler.interpretMetadata(n,this.filepath);if(r.hasValue(t))return[];let i=r.getValue();return Array.isArray(i)?this.table.indexes.push(...i):i&&this.table.indexes.push(i),r.getErrors()}interpretChecks(e){let n=this.compiler.nodeMetadata(e).getFiltered(t);if(!n)return[];let r=this.compiler.interpretMetadata(n,this.filepath);if(r.hasValue(t))return[];let i=r.getValue();return Array.isArray(i)?this.table.checks.push(...i):i&&this.table.checks.push(i),r.getErrors()}},D_=[i.Note,i.Ref,i.PrimaryKey,i.PK,i.NotNull,i.Null,i.Unique,i.Increment,i.Default,i.Check],O_={[i.Note]:{isValidBuiltinFieldValue:D,message:`'note' must be a string literal`,assignBuiltinField(e,t,n){e.note={value:t,token:n}}},[i.HeaderColor]:{isValidBuiltinFieldValue:Pf,message:`'headercolor' must be a color literal`,assignBuiltinField(e,t){e.headerColor=t}}},k_={[i.Note]:{isValidBuiltinFieldValue:D,message:`'note' must be a quoted string`,assignBuiltinField(e,t,n){e.note={value:t,token:n}}}},A_={getDuplicateError(e,t,n){return new T(w.DUPLICATE_NAME,`Table '${e}' already exists in schema '${t}'`,n)},getColumnDuplicateError(e,t){return new T(w.DUPLICATE_COLUMN_NAME,`Duplicate column '${e}'`,t)},getPartialInjectionDuplicateError(e,t){return new T(w.DUPLICATE_TABLE_PARTIAL_INJECTION_NAME,`Duplicate table partial injection '${e}'`,t)}},j_={nodeSymbol(i,a){let o=i.nodeFullname(a).getFiltered(t)?.at(-1);return A(a,r.Table)?new n(i.symbolFactory.create(qp,{declaration:a,name:o},a.filepath)):j(a,r.Table)?jf(a.callee)?new n(i.symbolFactory.create(em,{declaration:a,name:o},a.filepath)):new n(i.symbolFactory.create(Jp,{declaration:a,name:o},a.filepath)):n.create(e)},symbolMembers(r,i){if(!i.isKind(I.Table))return n.create(e);let a=i.declaration;if(!(a instanceof z))return new n([]);let o=cp(a),s=[],c=[];for(let e of o){let n=r.nodeSymbol(e),i=n.getFiltered(t);i&&(s.push(i),c.push(...n.getErrors()))}let l=new Map;for(let e of s){if(!e.isKind(I.Column,I.PartialInjection)||!e.declaration)continue;let t=`${e.kind}:${e.name}`;if(e.name!==void 0){let n=e.declaration instanceof z&&e.declaration.name?e.declaration.name:e.declaration,r=l.get(t);r?e.isKind(I.Column)?(c.push(A_.getColumnDuplicateError(e.name,r)),c.push(A_.getColumnDuplicateError(e.name,n))):(c.push(A_.getPartialInjectionDuplicateError(e.name,r)),c.push(A_.getPartialInjectionDuplicateError(e.name,n))):l.set(t,n)}}let u=[];for(let e=0;e<s.length;e++){let n=s[e];if(!(n.declaration instanceof H)||!jf(n.declaration.callee))continue;let i=n.declaration.callee.expression,o=P(i);if(i===void 0||o===void 0)continue;let l=r.nodeReferee(i).getFiltered(t);if(!l){c.push(new T(w.BINDING_ERROR,`TablePartial '${o}' does not exist in Schema 'public'`,i||a));continue}let d=r.symbolMembers(l).getFiltered(t);if(d){let t=d.flatMap(e=>{if(!e.declaration)return[];let t=e.name;return t===void 0?e:r.symbolFactory.create(um,{kind:I.Column,injectionDeclaration:n.declaration,declaration:e.declaration,name:t},a.filepath)});u.push({index:e,partialMembers:t})}}for(let e=u.length-1;e>=0;e--){let{index:t,partialMembers:n}=u[e];s.splice(t,0,...n)}let d=new Set(s.filter(e=>e.isKind(I.Column)&&!(e instanceof um)).map(e=>e.name));return new n(Pa([...s.filter(e=>!(e instanceof um&&d.has(e.name)))].reverse(),e=>e.isKind(I.Column)?e.name:e).reverse(),c)},nodeReferee(a,o){if(Qf(o,i.Dep)&&o.parent instanceof z&&o.parent.isKind(r.Table)&&!Yf(o,r.Table)){let e=a.parseFile(o.filepath).getValue().ast,r=a.nodeSymbol(e).getValue();return r===t?n.create(void 0):F_(a,r,o)}if(!Yf(o,r.Table))return n.create(e);let s=a.parseFile(o.filepath).getValue().ast,c=a.nodeSymbol(s).getValue();return c===t?n.create(void 0):O(o)&&o.parentNode instanceof V&&o.parentNode.op?.value===`~`?M_(a,c,o):Xf(o,1)?N_(a,c,o):Qf(o,i.Ref)?P_(a,c,o):Qf(o,i.Dep)?F_(a,c,o):Xf(o,0)?n.create(e):I_(a,c,o)},bindNode(i,a){if(!A(a,r.Table))return n.create(e);let o=new T_(i,a).bind(),s=i.nodeSymbol(a).getFiltered(t);return s&&o.push(...i.symbolMembers(s).getErrors()),n.create(void 0,o)},interpretSymbol(t,r,i){return r instanceof qp?r.declaration instanceof z?new E_(t,r.declaration,r,i).interpret():n.create(void 0):n.create(e)}};function M_(e,r,i){let a=P(i)??``,o=e.symbolMembers(r).getFiltered(t);if(o){let t=o.find(e=>e instanceof Wp&&e.isPublicSchema());if(t){let r=e.lookupMembers(t,I.TablePartial,a);if(r)return n.create(r)}}let s=e.lookupMembers(r,I.TablePartial,a);return s?n.create(s):new n(void 0,[new T(w.BINDING_ERROR,`TablePartial '${a}' does not exist in Schema 'public'`,i)])}function N_(e,t,r){if(!O(r))return new n(void 0);let i=mp(r)??``;if(!k(r.parentNode))return n.create(e.lookupMembers(t,I.Enum,i));let a=Bm(e,r);if(a){if(a.isKind(I.Schema)){let t=rp(r),o=e.lookupMembers(a,t?I.Enum:I.Schema,i);return o?n.create(o):new n(void 0,[new T(w.BINDING_ERROR,t?`Enum '${i}' does not exist in Schema 'public'`:`Schema '${i}' does not exist in Schema 'public'`,r)])}return new n(void 0)}if(r.parentNode.leftExpression===r){let a=e.lookupMembers(t,I.Schema,i);return a?n.create(a):new n(void 0,[new T(w.BINDING_ERROR,`Schema '${i}' does not exist in Schema 'public'`,r)])}return new n(void 0)}function P_(e,i,a){if(!O(a))return new n(void 0);let o=mp(a)??``;if(!k(a.parentNode)){let s=a.parent,c=s instanceof z&&s.isKind(r.Table)?e.nodeSymbol(s).getFiltered(t):void 0;if(c){let t=e.lookupMembers(c,I.Column,o);if(t)return n.create(t)}let l=e.lookupMembers(i,I.Column,o);return l?n.create(l):new n(void 0,[new T(w.BINDING_ERROR,`Column '${o}' does not exist in Table 'public.${c?.name??``}'`,a)])}return Vm(e,i,a,I.Column)}function F_(e,i,a){if(!O(a))return new n(void 0);let o=Yf(a,r.Table),s=mp(a)??``;if(!k(a.parentNode))if(o){let o=a.parent,c=o instanceof z&&o.isKind(r.Table)?e.nodeSymbol(o).getFiltered(t):void 0;if(c){let t=e.lookupMembers(c,I.Column,s);if(t)return n.create(t)}let l=e.lookupMembers(i,I.Column,s);return l?n.create(l):new n(void 0,[new T(w.BINDING_ERROR,`Column '${s}' does not exist in Table 'public.${c?.name??``}'`,a)])}else{let t=e.lookupMembers(i,I.Table,s);return t?n.create(t):new n(void 0,[new T(w.BINDING_ERROR,`Table '${s}' does not exist in Schema 'public'`,a)])}return Vm(e,i,a,o?I.Column:I.Table)}function I_(e,t,r){if(!O(r))return new n(void 0);let i=mp(r)??``;if(!k(r.parentNode))return So.includes(i.toLowerCase())?new n(void 0):n.create(e.lookupMembers(t,I.Enum,i));let a=Bm(e,r);if(a){if(a.isKind(I.Schema)){let t=e.lookupMembers(a,[I.Enum,I.Schema],i);return t?n.create(t):new n(void 0,[new T(w.BINDING_ERROR,`Enum or schema '${i}' does not exist`,r)])}if(a.isKind(I.Enum)){let t=e.lookupMembers(a,I.EnumField,i);return t?n.create(t):new n(void 0,[new T(w.BINDING_ERROR,`Enum field '${i}' does not exist in Enum 'public.${a.name}'`,r)])}return new n(void 0)}let o=r.parentNode;if(o.leftExpression===r){if(k(o.parentNode)&&o.parentNode.leftExpression===o){let a=e.lookupMembers(t,I.Schema,i);return a?n.create(a):new n(void 0,[new T(w.BINDING_ERROR,`Schema '${i}' does not exist in Schema 'public'`,r)])}let a=e.lookupMembers(t,I.Enum,i);return a?n.create(a):new n(void 0,[new T(w.BINDING_ERROR,`Enum '${i}' does not exist in Schema 'public'`,r)])}return new n(void 0)}function L_(e){return e?e.split(``).every(e=>Cf(e)||wf(e))&&!wf(e[0]):!1}function R_(e){return L_(e)?e:`"${V_(e)}"`}function z_(...e){return e.map(R_).join(`.`)}function B_(e){let t=``,n=0,r={n:`
8
+ `);return new Sf(w.INVALID_REF_RELATIONSHIP,c,t,{category:bf.RefColumnMismatch,explanation:l,quickFixes:o})}function Bh(e,t,n,r){let{schema:i,name:a}=r?t.interpretedName(e,r):{schema:t.schema(e),name:t.name??``},o=n.map(e=>{let t=e.name??``;return i?`${i}.${a}.${t}`:`${a}.${t}`});return o.length===1?o[0]:`(${o.join(`, `)})`}function Vh(e,t){return e.length>1?`Composite ${t}`:t}function Hh(e,t){let{colName:n}=t;return new Sf(w.INVALID_RECORDS_FIELD,`NULL not allowed for NOT NULL column \`${n}\` without a default value or auto-increment`,e,{category:bf.RecordValueTypeMismatch,explanation:`\`${n}\` is marked NOT NULL and has no default value or auto-increment.`})}function Uh(e,t){let{colName:n}=t;return new Sf(w.INVALID_RECORDS_FIELD,`Invalid enum value for column \`${n}\``,e,{category:bf.RecordValueTypeMismatch,explanation:`\`${n}\` uses an enum type. The value must match one of the defined enum members.`})}function Wh(e,t){let{colName:n,typeName:r}=t;return new Sf(w.INVALID_RECORDS_FIELD,`Invalid numeric value for column \`${n}\``,e,{category:bf.RecordValueTypeMismatch,explanation:`\`${n}\` has type \`${r}\` which expects a numeric value. Provide a valid integer or decimal.`})}function Gh(e,t){let{colName:n,value:r}=t;return new Sf(w.INVALID_RECORDS_FIELD,`Invalid integer value \`${r}\` for column \`${n}\`: expected integer, got decimal`,e,{category:bf.RecordValueTypeMismatch,explanation:`\`${n}\` is an integer column and cannot store decimal values.`})}function Kh(e,t){let{colName:n,typeName:r,value:i,precision:a,scale:o,totalDigits:s}=t;return new Sf(w.INVALID_RECORDS_FIELD,`Numeric value \`${i}\` for column \`${n}\` exceeds precision: expected at most ${a} total digits, got ${s}`,e,{category:bf.RecordValueTypeMismatch,explanation:`\`${n}\` is defined as \`${r}(${a},${o})\`, allowing at most ${a} total digits. The value \`${i}\` has ${s}.`})}function qh(e,t){let{colName:n,typeName:r,value:i,precision:a,scale:o,decimalDigits:s}=t;return new Sf(w.INVALID_RECORDS_FIELD,`Numeric value \`${i}\` for column \`${n}\` exceeds scale: expected at most ${o} decimal digits, got ${s}`,e,{category:bf.RecordValueTypeMismatch,explanation:`\`${n}\` is defined as \`${r}(${a},${o})\`, allowing at most ${o} decimal places. The value \`${i}\` has ${s}.`})}function Jh(e,t){let{colName:n}=t;return new Sf(w.INVALID_RECORDS_FIELD,`Invalid boolean value for column \`${n}\``,e,{category:bf.RecordValueTypeMismatch,explanation:`\`${n}\` is a boolean column. Use \`true\`/\`false\`, \`1\`/\`0\`, or \`'yes'\`/\`'no'\`.`})}function Yh(e,t){let{colName:n}=t;return new Sf(w.INVALID_RECORDS_FIELD,`Invalid datetime value for column \`${n}\``,e,{category:bf.RecordValueTypeMismatch,explanation:`\`${n}\` expects a date or time value. Supported formats include \`'YYYY-MM-DD'\`, \`'HH:MM:SS'\`, \`'YYYY-MM-DD HH:MM:SS'\`, \`'MM/DD/YYYY'\`, \`'D MMM YYYY'\`, or \`'MMM D, YYYY'\`.`})}function Xh(e,t){let{colName:n}=t;return new Sf(w.INVALID_RECORDS_FIELD,`Invalid string value for column \`${n}\``,e,{category:bf.RecordValueTypeMismatch,explanation:`\`${n}\` is a string column. Values must be wrapped in quotes.`})}function Zh(e,t){let{colName:n,typeName:r,typeArg:i,maxLength:a,actualLength:o}=t;return new Sf(w.INVALID_RECORDS_FIELD,`String value for column \`${n}\` exceeds maximum length: expected at most ${a} bytes (UTF-8), got ${o} bytes`,e,{category:bf.RecordValueTypeMismatch,explanation:`\`${n}\` is defined as \`${r}(${i})\`, limiting values to ${a} bytes in UTF-8 encoding. The provided value uses ${o} bytes.`})}function Qh(e,t,n){let{table:r,columns:i}=n,a=Bh(e,r,i),o=Vh(i,`PK`);return new Sf(w.INVALID_RECORDS_FIELD,`NULL in ${o}: \`${a}\` cannot be NULL`,t,{category:bf.RecordConstraintViolation,explanation:`\`${a}\` is a primary key and cannot be NULL.`})}function $h(e,t,n){let{table:r,columns:i,valueStr:a}=n,o=Bh(e,r,i),s=Vh(i,`PK`);return new Sf(w.INVALID_RECORDS_FIELD,`Duplicate ${s}: \`${o}\` = \`${a}\``,t,{category:bf.RecordConstraintViolation,explanation:`\`${o}\` is a primary key and must be unique across all rows.`})}function eg(e,t,n){let{table:r,columns:i}=n,a=Bh(e,r,i),o=Vh(i,`PK`);return new Sf(w.INVALID_RECORDS_FIELD,`${o}: Column \`${a}\` is missing from record and has no default value`,t,{category:bf.RecordConstraintViolation,explanation:`\`${a}\` is a primary key with no default or auto-increment, so it must be specified in the records block.`})}function tg(e,t,n){let{leftTable:r,leftColumns:i,rightTable:a,rightColumns:o,filepath:s,valueStr:c}=n,l=Bh(e,r,i,s),u=Bh(e,a,o,s);return new Sf(w.INVALID_RECORDS_FIELD,`FK violation: \`${l}\` = \`${c}\` must not be NULL`,t,{category:bf.RecordConstraintViolation,explanation:`\`${l}\` references \`${u}\`, so every row must have a non-null value.`})}function ng(e,t,n){let{leftTable:r,leftColumns:i,rightTable:a,rightColumns:o,filepath:s,valueStr:c}=n,l=Bh(e,r,i,s),u=Bh(e,a,o,s);return new Sf(w.INVALID_RECORDS_FIELD,`FK violation: \`${l}\` = \`${c}\` does not exist in \`${u}\``,t,{category:bf.RecordConstraintViolation,explanation:`\`${l}\` is a foreign key referencing \`${u}\`. Every non-null value in an FK must match an existing row in the referenced column.`})}function rg(e,t,n){let{table:r,columns:i,valueStr:a}=n,o=Bh(e,r,i),s=Vh(i,`UNIQUE`);return new Sf(w.INVALID_RECORDS_FIELD,`Duplicate ${s}: \`${o}\` = \`${a}\``,t,{category:bf.RecordConstraintViolation,explanation:`\`${o}\` has a unique constraint, so no two rows can have the same value.`})}function ig(e){return e.values.map(t=>{let n={};return e.columns.forEach((e,r)=>{n[e]=t[r]}),n})}function ag(e,t){return e?`${e}.${t}`:`${y}.${t}`}function og(e,t,n){return n.map(n=>{let r=t[n.name??``]?.value;if(r==null){let t=n.default(e);if(t)return JSON.stringify(t.value)}return JSON.stringify(r)}).join(`|`)}function sg(e,t,n){return n.some(n=>{let r=t[n.name??``]?.value;return r==null&&n.default(e)?!1:r==null})}function cg(e,t,n){let r=n.map(n=>{let r=n.name??``,i=t[r]===void 0?n.default(e)?.value:t[r].value;return JSON.stringify(i)}).join(`, `);return n.length===1?r:`(${r})`}function lg(e,t){let n=t.filter(t=>e[t]).map(t=>e[t]);return n.length>0?n:Object.values(e).filter(Boolean)}function ug(e,t){let{token:n}=t;return e.nodeAtPosition(n.filepath,n.start.offset)??e.parse.ast(n.filepath)}function dg(e,t,n,r){return Oa(r.values)?[]:xa(gg(t,e),i=>fg(e,t,n,i,r))}function fg(e,t,n,r,i){let a=ig(i),o=hg(e,t,n,r,i);if(!Oa(o))return o;let s=r.filter(t=>!t.increment(e)&&!t.default(e)),[c,l]=_(a,t=>sg(e,t,s)),u=pg(e,t,s,c),d=r.some(t=>t.increment(e))?[]:mg(e,t,r,l);return[...u,...d]}function pg(e,t,n,r){if(Oa(r))return[];let i=n.map(e=>e.name??``);return xa(r,r=>lg(r,i).map(r=>Qh(e,ug(e,r),{table:t,columns:n})))}function mg(e,t,n,r){let i=n.map(e=>e.name??``);return xa(ma(wa(r,t=>og(e,t,n)),e=>e.length>1),r=>xa(r,r=>{let a=cg(e,r,n);return lg(r,i).map(r=>$h(e,ug(e,r),{table:t,columns:n,valueStr:a}))}))}function hg(e,t,n,r,i){let a=r.map(e=>e.name??``),o=new Set(i.columns),s=da(a,Array.from(o));if(Oa(s))return[];let c=new Set(s),l=r.filter(e=>c.has(e.name??``)).filter(t=>!t.increment(e)&&!t.default(e));return Oa(l)?[]:i.values.map(()=>eg(e,n,{table:t,columns:l}))}function gg(e,t){let n=e.mergedColumns(t),r=ka(n,e=>e.name??``);return[...n.filter(e=>e.pk(t)).map(e=>[e]),...e.mergedIndexes(t).flatMap(e=>{let n=t.interpretMetadata(e,e.declaration.filepath).getValue();return Array.isArray(n)?n.filter(e=>e.pk).map(e=>Qr(e.columns.map(e=>r[e.value]))):[]})]}function _g(e,t,n){if(Oa(n.values))return[];let r=ig(n);return xa(vg(e,t),n=>yg(e,t,n,r))}function vg(e,t){let n=t.mergedColumns(e),r=ka(n,e=>e.name??``);return[...n.filter(t=>t.isUniqueSet(e)).map(e=>[e]),...t.mergedIndexes(e).flatMap(t=>{let n=e.interpretMetadata(t,t.declaration.filepath).getValue();return Array.isArray(n)?n.filter(e=>e.unique).map(e=>Qr(e.columns.map(e=>r[e.value]))):[]})]}function yg(e,t,n,r){let i=n.map(e=>e.name??``);return xa(ma(wa(r.filter(t=>!sg(e,t,n)),t=>og(e,t,n)),e=>e.length>1),r=>xa(r,r=>{let a=cg(e,r,n);return lg(r,i).map(r=>rg(e,ug(e,r),{table:t,columns:n,valueStr:a}))}))}function bg(e,t,n){let r=new Map;for(let i of e.values()){let{name:e,schema:a}=i.tableSymbol.interpretedName(t,n);r.set(ag(a,e),i)}return r}function xg(e,t,n,r){let i=bg(n,e,r),a=new Set;for(let[e,t]of i)t.record&&!Oa(t.record.values)&&a.add(e);return xa(t.filter(e=>e.endpoints?e.endpoints.some(e=>a.has(ag(e.schemaName,e.tableName))):!1),t=>Sg(e,t,i,r))}function Sg(e,t,n,r){if(!t.endpoints)return[];let[i,a]=t.endpoints,o=n.get(ag(i.schemaName,i.tableName)),s=n.get(ag(a.schemaName,a.tableName));if(!o||!s)return[];let c=o.tableSymbol.mergedColumns(e),l=s.tableSymbol.mergedColumns(e),u=Qr(i.fieldNames.map(e=>c.find(t=>t.name===e))),d=Qr(a.fieldNames.map(e=>l.find(t=>t.name===e))),{min:f,max:p}=nf(i.relation),{min:m,max:ee}=nf(a.relation),te=p===1&&m===0||p!==`*`&&ee!==`*`,ne=ee===1&&f===0;return[...te?[]:Cg(e,o,u,s,d,i.relation,a.relation,r),...ne?[]:Cg(e,s,d,o,u,a.relation,i.relation,r)]}function Cg(e,t,n,r,i,a,o,s){if(!t.record||Oa(t.record.values))return[];let{min:c,max:l}=nf(a),{min:u,max:d}=nf(o),f=Qr(n.map(e=>e.name));Qr(i.map(e=>e.name));let p=u===0,m=ig(t.record),ee=r.record?ig(r.record):[],te=new Set(ee.map(t=>og(e,t,i))),ne={leftTable:t.tableSymbol,leftColumns:n,rightTable:r.tableSymbol,rightColumns:i,filepath:s};return xa(m,t=>{if(sg(e,t,n)){if(p)return[];let r=cg(e,t,n);return lg(t,f).map(t=>tg(e,ug(e,t),{...ne,valueStr:r}))}if(d===`*`&&c===0&&l===`*`)return[];let r=og(e,t,n);if(te.has(r))return[];let i=cg(e,t,n);return lg(t,f).map(t=>ng(e,ug(e,t),{...ne,valueStr:i}))})}function wg(e){return e instanceof qm&&e.declaration instanceof z&&e.declaration.body instanceof U}function Tg(e,t){let n=e.edges??[];if(n.length===0||!wg(t))return[];let r=[],i=ag(n[0].downstream.schemaName,n[0].downstream.tableName);for(let e=1;e<n.length;e++){let a=ag(n[e].downstream.schemaName,n[e].downstream.tableName);if(a!==i){r.push(new T(w.DEP_MIXED_DOWNSTREAM_TABLES,`All edges in a Dep block must target the same downstream table, but found '${i}' and '${a}'`,t.declaration));break}}let a=e=>e.upstream.fieldNames.length>0||e.downstream.fieldNames.length>0,o=a(n[0]);for(let e=1;e<n.length;e++)if(a(n[e])!==o){r.push(new T(w.DEP_MIXED_LEVEL,`All edges in a Dep block must be at the same level (all table-level or all column-level)`,t.declaration));break}return r}function Eg(e,t){let n=Ag(e);if(n){let e=n.elementList??[],r=e.findIndex(e=>e instanceof B?hp(e)?.toLowerCase()===t.toLowerCase():!1);if(r!==-1)return{kind:`attribute`,settingsList:n,settingNode:e[r],settingIndex:r}}if(e instanceof z){let n=jg(e,t);if(n)return{kind:`body`,declaration:n}}}function Dg(e,t){let n=Ag(e);if(n){let e=n.end-1;return{start:e,end:e,newText:`, ${t}`}}if(e instanceof z){let n=e.body;if(n&&!(n instanceof H))return{start:n.start,end:n.start,newText:`[${t}] `}}return{start:e.end,end:e.end,newText:` [${t}]`}}function Og(e,t,n,r){if(n===null)return kg(e,t,r);let i=n===void 0?t:`${t}: ${n}`,a=Eg(e,t);if(a){let e=a.kind===`attribute`?a.settingNode:a.declaration;return{start:e.start,end:e.end,newText:i}}return Dg(e,i)}function kg(e,t,n){let r=Eg(e,t);if(!r)return;if(r.kind===`body`)return{start:r.declaration.start,end:r.declaration.end,newText:``};let{settingsList:i,settingIndex:a}=r,o=i.elementList??[];if(o.length===1){let e=i.start;for(;e>0&&n[e-1]===` `;)e--;return{start:e,end:i.end,newText:``}}let s=o[a];if(a===0){let e=o[a+1];return{start:s.start,end:e.start,newText:``}}return{start:o[a-1].end,end:s.end,newText:``}}function Ag(e){if(e instanceof H)return e.args.find(e=>e instanceof W);if(e instanceof z){if(e.attributeList)return e.attributeList;let t=e.body;if(t instanceof H)return t.args.find(e=>e instanceof W)}}function jg(e,t){let n=e.body;if(!(!n||Af(e)||n instanceof H)){for(let e of n.body)if(e instanceof z&&e.type?.value?.toLowerCase()===t.toLowerCase())return e}}function Mg(e,t,n,r){let{allowOtherColFix:i=!0,allowOwnColFix:a=!0}=r??{},o=t.op(e),s=t.opToken(),c=t.cardinalities(e);if(!o||!s||!c)return[];let l=n===`left`?c[0]:c[1],u=nf(l),d=n===`left`?c[1]:c[0],f=nf(d),p=n===`left`?t.leftColumns(e):t.rightColumns(e),m=n===`left`?t.rightColumns(e):t.leftColumns(e),ee=n===`left`?t.leftToken():t.rightToken(),te=n===`left`?t.rightToken():t.leftToken(),ne=[];function re(e,t,n){ne.push(n(e));for(let r of t)r.declaration&&r.declaration!==e&&ne.push(n(r.declaration))}let ie=f.max===`*`||u.max===1&&f.max===1&&n===`left`;if(u.min>=1&&ie&&m.length>0&&m.every(t=>t.nullable(e))){let r=m.map(t=>t.qualifiedName(e).join(`.`)).join(`, `),a=[s&&Pg(s,_f(l),d,n,`Make '${r}' optional in the ref`),...i?m.map(t=>Fg(t,e)).filter(e=>!!e):[]].filter(e=>!!e);re(te,m,n=>Ih(e,n,{relOp:o,columns:m,refNode:t.declaration,quickFixes:a}))}if(u.min===0&&ie&&m.length>0&&m.every(t=>t.nullable(e)===!1)){let r=m.map(t=>t.qualifiedName(e).join(`.`)).join(`, `),a=[s&&Pg(s,vf(l),d,n,`Make '${r}' required in the ref`),...i?m.map(t=>Ig(t,e)).filter(e=>!!e):[]].filter(e=>!!e);re(te,m,n=>Lh(e,n,{relOp:o,columns:m,refNode:t.declaration,quickFixes:a}))}if(u.max===`*`&&Lg(e,p)){let r=p.map(t=>t.qualifiedName(e).join(`.`)).join(`, `),i=[s&&Pg(s,`1`,d,n,`Make '${r}' one in the ref`)].filter(e=>!!e);re(ee,p,n=>Rh(e,n,{relOp:o,columns:p,refNode:t.declaration,quickFixes:i}))}if(u.max===1&&!Lg(e,p)){let r=p.map(t=>t.qualifiedName(e).join(`.`)).join(`, `),i=[s&&Pg(s,yf(l),d,n,`Make '${r}' many in the ref`),a&&p.length===1&&Rg(p[0],e)].filter(e=>!!e);re(ee,p,n=>zh(e,n,{relOp:o,columns:p,refNode:t.declaration,quickFixes:i}))}return ne}function Ng(e,t){return[...Mg(e,t,`right`,{allowOtherColFix:!1}),...Mg(e,t,`left`,{allowOwnColFix:!1})]}function Pg(e,t,n,r,i){let a=gf(r===`left`?t:n,r===`right`?t:n);return{title:`${i} (change operator to '${a}')`,shortTitle:`Change to ${a}`,filepath:e.filepath,edits:[{start:e.start,end:e.end,newText:a}],isPreferred:!0}}function Fg(e,t){if(!e.declaration||e.pk(t)||e.increment(t)||e.isNotNullSet(t))return;let n=t.getSource(e.declaration.filepath);if(!n)return;let r=e.qualifiedName(t).join(`.`),i=kg(e.declaration,`null`,n);if(i)return{title:`Mark '${r}' as NOT NULL`,shortTitle:`Add not null`,filepath:e.declaration.filepath,edits:[i]};let a=Dg(e.declaration,`not null`);if(a)return{title:`Mark '${r}' as NOT NULL`,shortTitle:`Add not null`,filepath:e.declaration.filepath,edits:[a]}}function Ig(e,t){if(!e.declaration)return;let n=t.getSource(e.declaration.filepath);if(!n)return;let r=kg(e.declaration,`not null`,n);if(r)return{title:`Mark '${e.qualifiedName(t).join(`.`)}' as NULL`,shortTitle:`Make nullable`,filepath:e.declaration.filepath,edits:[r]}}function Lg(e,n){if(n.length===0)return!1;if(n.length===1&&n[0].unique(e))return!0;let r=n[0],i=(r instanceof um?r.injectionDeclaration:r.declaration)?.parentOfKind(z);if(!i)return!1;let a=e.nodeSymbol(i).getFiltered(t);if(!(a instanceof qp))return!1;let o=a.mergedIndexes(e),s=new Set(n.map(e=>e.name));for(let t of o){let r=e.interpretMetadata(t,n[0].filepath).getValue();if(!(!r||!Array.isArray(r))){for(let e of r)if(!(!e.unique&&!e.pk)&&e.columns.length===s.size&&e.columns.every(e=>s.has(e.value)))return!0}}return!1}function Rg(e,t){if(!e.declaration||e.isUniqueSet(t)||e.pk(t))return;let n=Dg(e.declaration,`unique`);if(n)return{title:`Mark '${e.qualifiedName(t).join(`.`)}' as UNIQUE`,shortTitle:`Add unique`,filepath:e.declaration.filepath,edits:[n]}}var zg=class{constructor(e,t,n){this.errors=[],this.warnings=[],this.infos=[],this.compiler=e,this.programSymbol=t,this.programNode=t.declaration,this.filepath=n,this.db={schemas:[],tables:[],notes:[],refs:[],deps:[],enums:[],tableGroups:[],aliases:[],tablePartials:[],records:[],diagramViews:[],token:F(this.programNode),externals:{tables:[],enums:[],tableGroups:[],tablePartials:[],notes:[]}}}interpret(){return this.interpretAllSymbols(),this.interpretAllMetadata(),this.interpretAllAliases(),this.validatePartialRefs(),this.infos.push(...this.validateRecords()),new n(this.db,this.errors,this.warnings,this.infos)}interpretAllSymbols(){for(let e of this.programSymbol.declaration.declarations){let n=this.compiler.nodeSymbol(e).getFiltered(t);if(!n||n instanceof lm)continue;let r=this.compiler.interpretSymbol(n,this.filepath);if(r.hasValue(t))continue;this.errors.push(...r.getErrors()),this.warnings.push(...r.getWarnings()),this.infos.push(...r.getInfos());let i=r.getValue();i&&this.pushElement(n,i)}let e=new Set,n=(this.compiler.symbolMembers(this.programSymbol).getFiltered(t)??[]).filter(e=>e instanceof Wp);for(;n.length>0;){let r=n.shift(),i=this.compiler.symbolMembers(r).getFiltered(t)??[];for(let t of i){if(t instanceof Wp){n.push(t);continue}if(!(t instanceof lm))continue;let r=t.originalSymbol.intern();e.has(r)||(e.add(r),this.interpretUseSymbol(t))}}}interpretUseSymbol(e){let n=e.originalSymbol,r=this.compiler.interpretSymbol(n,this.filepath);if(!r.hasValue(t)){this.errors.push(...r.getErrors()),this.warnings.push(...r.getWarnings()),this.infos.push(...r.getInfos());let t=r.getValue();t&&this.pushElement(e,t)}let i=n instanceof qp?n.interpretedName(this.compiler,n.filepath):{name:n.name??``,schema:null},a=n instanceof qp?n.interpretedName(this.compiler,this.filepath):{name:e.name??n.name??``,schema:null},o={name:i.name,schemaName:i.schema,filepath:n.filepath,visibleNames:[{schemaName:a.schema,name:a.name}]};jh(this.db,e,o)}interpretAllMetadata(){let e=this.compiler.symbolMetadata(this.programSymbol)??[],n=new Set,r=new Set,i=new Map;for(let t of e){if(!(t instanceof Zm))continue;let e=t.table(this.compiler);if(!e)continue;let n=e.originalSymbol.intern(),r=i.get(n);r?r.count++:i.set(n,{count:1,tableName:e.name??``})}for(let a of e){let e=this.compiler.interpretMetadata(a,this.filepath);if(e.hasValue(t))continue;this.errors.push(...e.getErrors()),this.warnings.push(...e.getWarnings()),this.infos.push(...e.getInfos());let o=e.getValue();if(o!==void 0)switch(a.kind){case Wm.Ref:{let e=o,[t,r]=e.endpoints??[];if(t&&r){let e=[t.schemaName,t.tableName,t.fieldNames.join(`,`),r.schemaName,r.tableName,r.fieldNames.join(`,`)].join(`|`),i=[r.schemaName,r.tableName,r.fieldNames.join(`,`),t.schemaName,t.tableName,t.fieldNames.join(`,`)].join(`|`);if(n.has(e)||n.has(i)){this.errors.push(new T(w.SAME_ENDPOINT,`Ref with same endpoints already exists`,a.declaration));break}n.add(e)}this.db.refs.push(e);break}case Wm.Dep:{let e=o,t=a instanceof qm?a.edgeExpressions():[],n=-1;if((e.edges??[]).some((e,t)=>{let{upstream:i,downstream:a}=e,o=[i.schemaName,i.tableName,i.fieldNames.join(`,`),a.schemaName,a.tableName,a.fieldNames.join(`,`)].join(`|`);return r.has(o)?(n=t,!0):(r.add(o),!1)}),n>=0){let e=t[n]??a.declaration;this.errors.push(new T(w.SAME_ENDPOINT,`Dep with same endpoints already exists`,e));break}let i=Tg(e,a);if(i.length>0){this.errors.push(...i);break}this.db.deps.push(e);break}case Wm.Records:if(a instanceof Zm){let e=a.table(this.compiler)?.originalSymbol.intern()??``,t=e?i.get(e):void 0;if(t&&t.count>1){this.errors.push(new T(w.DUPLICATE_RECORDS_FOR_TABLE,`Duplicate Records blocks for the same Table '${t.tableName}' - A Table can only have one Records block`,a.declaration));break}}this.db.records.push(o);break;case Wm.Project:this.db.project=o;break;case Wm.MetadataElement:default:break}}}interpretAllAliases(){let e=this.compiler.symbolMembers(this.programSymbol).getFiltered(t)??[];for(let t of e){if(!(t instanceof cm)||!t.isKind(I.Table))continue;let{name:e,schema:n}=t.originalSymbol.interpretedName(this.compiler,this.filepath),r={name:t.name,kind:jm.Table,value:{tableName:e,schemaName:n}};this.db.aliases.push(r)}}validateRecords(){let e=[],n=new Map,r=this.compiler.symbolMembers(this.programSymbol).getFiltered(t)??[];for(let e of r){if(!(e instanceof Wp))continue;let r=this.compiler.symbolMembers(e).getFiltered(t)??[];for(let e of r){if(!e.isKind(I.Table))continue;let t=e.originalSymbol;if(!(t instanceof qp))continue;let r=t.intern();n.has(r)||n.set(r,{tableSymbol:t,record:void 0,recordBlock:t.declaration})}}let i=this.compiler.symbolMetadata(this.programSymbol);for(let r of i){if(!(r instanceof Zm))continue;let i=r.table(this.compiler);if(!(i instanceof qp))continue;let a=this.compiler.interpretMetadata(r,this.filepath);if(a.hasValue(t))continue;let o=a.getValue();if(!o)continue;e.push(...dg(this.compiler,i,r.declaration,o)),e.push(..._g(this.compiler,i,o));let s=i.originalSymbol.intern(),c=n.get(s);c?c.record=o:n.set(s,{tableSymbol:i,record:o,recordBlock:r.declaration})}let a=this.collectPartialRefs(n);return e.push(...xg(this.compiler,[...this.db.refs,...a],n,this.filepath)),e}validatePartialRefs(){let e=this.compiler.symbolMetadata(this.programSymbol).filter(e=>e instanceof Jm);for(let t of e)this.infos.push(...Ng(this.compiler,t))}collectPartialRefs(e){let t=this.compiler.symbolMetadata(this.programSymbol).filter(e=>e instanceof Jm),n=[];for(let{tableSymbol:r}of e.values())for(let e of r.resolvedPartials(this.compiler))for(let i of t){if(i.leftTablePartial(this.compiler)?.originalSymbol!==e.originalSymbol)continue;let t=i.leftColumns(this.compiler),a=i.rightTable(this.compiler),o=i.rightColumns(this.compiler),s=i.op(this.compiler);if(!a||!s||t.length===0||o.length===0)continue;let c=r.mergedColumns(this.compiler);if(!t.some(e=>c.some(t=>t instanceof um&&t.declaration===e.declaration)))continue;let l=uf(s);if(!l)continue;let u=a instanceof $p&&a.originalSymbol===e.originalSymbol?r:a,d=r.interpretedName(this.compiler,this.filepath),f=u.interpretedName(this.compiler,this.filepath),p={schemaName:d.schema,tableName:d.name,fieldNames:t.map(e=>e.name??``),relation:l[0],token:F(i.leftToken())},m={schemaName:f.schema,tableName:f.name,fieldNames:o.map(e=>e.name??``),relation:l[1],token:F(i.rightToken())};n.push({token:F(i.declaration),endpoints:[p,m]})}return n}pushElement(e,t){if(!Array.isArray(t))switch(e.kind){case I.Table:this.db.tables.push(t);break;case I.Enum:this.db.enums.push(t);break;case I.TableGroup:this.db.tableGroups.push(t);break;case I.TablePartial:this.db.tablePartials.push(t);break;case I.StickyNote:this.db.notes.push(t);break;case I.DiagramView:this.db.diagramViews.push(t);break;default:break}}},Bg={nodeSymbol(t,r){return Jf(r)?n.create(t.symbolFactory.create(sm,{declaration:r,name:r.filepath.absolute},r.filepath)):n.create(e)},symbolMembers(r,i){if(!i.isKind(I.Program))return n.create(e);if(!(i.declaration instanceof R))return n.create([]);let a=new Map;for(let e of r.reachableFiles(i.filepath)){let{schemaMembers:t}=r.usableMembers(e).getValue();for(let e of t)a.has(e.name)||a.set(e.name,e)}let o=a.get(y);if(!o)return n.create([...a.values()]);let s=r.symbolMembers(o).getFiltered(t)?.filter(e=>!e.isKind(I.Schema));return s?n.create([...a.values(),...s]):n.create([...a.values()])},bindNode(t,r){return Jf(r)?new kh(r,t).resolve():n.create(e)},interpretSymbol(t,r,i){if(!(r instanceof sm))return n.create(e);if(!(r.declaration instanceof R))return n.create(void 0);if(!Rm(t,r.declaration)){let e=t.reachableFiles(i).flatMap(e=>t.bindFile(e).getErrors());return n.create(void 0,e)}return new zg(t,r,i).interpret()}},Vg=class{constructor(e,t){this.compiler=e,this.declarationNode=t}bind(){return this.declarationNode.body instanceof U?this.bindBody(this.declarationNode.body):[]}bindBody(e){if(!e||e instanceof H)return[];let t=e.body.filter(e=>e instanceof z);return this.bindSubElements(t)}bindSubElements(e){return e.flatMap(e=>e.type?this.compiler.bindNode(e).getErrors():[])}},Hg=class{constructor(e,t,n){this.declarationNode=t,this.compiler=e,this.filepath=n,this.project={enums:[],refs:[],tableGroups:[],tables:[],tablePartials:[]}}interpret(){this.project.token=F(this.declarationNode);let e=[...this.interpretName(this.declarationNode.name),...this.interpretBody(this.declarationNode.body)];return new n(this.project,e)}interpretName(e){if(!e)return this.project.name=null,[];let{name:t}=Pp(e);return this.project.name=t,[]}interpretSubElement(e){let r=this.compiler.nodeSymbol(e).getFiltered(t);if(r){let e=this.compiler.interpretSymbol(r,this.filepath);if(!e.hasValue(t))return e}return n.create(void 0)}interpretBody(e){return e.body.flatMap(e=>{let t=e;switch(t.getElementKind()){case r.Table:{let e=this.interpretSubElement(t);return e.getValue()&&this.project.tables?.push(e.getValue()),e.getErrors()}case r.TableGroup:{let e=this.interpretSubElement(t);return e.getValue()&&this.project.tableGroups?.push(e.getValue()),e.getErrors()}case r.Enum:{let e=this.interpretSubElement(t);return e.getValue()&&this.project.enums?.push(e.getValue()),e.getErrors()}case r.Note:{let e=t.body instanceof U?t.body.body[0]?.callee:t.body?.callee,n=e?N(e):void 0;return n&&(this.project.note={value:Np(n),token:F(t)}),[]}case r.TablePartial:{let e=this.interpretSubElement(t);return e.getValue()&&this.project.tablePartials?.push(e.getValue()),e.getErrors()}default:return this.project[t.type.value.toLowerCase()]=N(t.body.callee),[]}})}},Ug={nodeMetadata(t,i){return A(i,r.Project)?n.create(new $m(i)):n.create(e)},bindNode(t,i){return A(i,r.Project)?n.create(void 0,new Vg(t,i).bind()):n.create(e)},interpretMetadata(t,r,i){return!(r instanceof $m)||!(r.declaration instanceof z)?n.create(e):new Hg(t,r.declaration,i).interpret()}},Wg=class{constructor(e,t){this.compiler=e,this.declarationNode=t,this.boundColumns=new Map}bind(){let e=[];return this.declarationNode.name&&e.push(...this.bindRecordsName(this.declarationNode.name)),this.declarationNode.body instanceof U&&e.push(...this.bindBody(this.declarationNode.body)),e}bindRecordsName(e){return this.declarationNode.parent instanceof R?this.bindTopLevelName(e):this.bindInsideTableName(e)}bindTopLevelName(e){let n=pp(e);if(!n)return[];let r=n.variables.pop(),i=n.variables;if(!r)return[];let a=this.compiler.nodeReferee(r).getErrors();if(a.length>0)return a;let o=this.compiler.nodeReferee(r).getFiltered(t);if(!o)return[];let s=op(o.declaration)??`<invalid name>`,c=i.flatMap(e=>this.compiler.nodeReferee(e).getErrors());for(let e of n.args){let n=mp(e)??`<unnamed>`,r=this.compiler.nodeReferee(e).getFiltered(t);if(!r){c.push(new T(w.BINDING_ERROR,`Column '${n}' does not exist in Table '${s}'`,e));continue}let i=this.boundColumns.get(r);i&&(c.push(new T(w.DUPLICATE_COLUMN_REFERENCES_IN_RECORDS,`Column '${n}' is referenced more than once in a Records for Table '${s}'`,i)),c.push(new T(w.DUPLICATE_COLUMN_REFERENCES_IN_RECORDS,`Column '${n}' is referenced more than once in a Records for Table '${s}'`,e))),this.boundColumns.set(r,e)}return c}bindInsideTableName(e){let n=this.declarationNode.parent;if(!(n instanceof z)||!n.isKind(r.Table)||!this.compiler.nodeSymbol(n).getFiltered(t)||!Bf(e))return[];let i=op(n)??`<invalid name>`,a=[];for(let n of e.elementList){let e=mp(n)??`<unnamed>`;if(!this.compiler.nodeReferee(n).getFiltered(t)){a.push(new T(w.BINDING_ERROR,`Column '${e}' does not exist in Table '${i}'`,n));continue}}return a}bindBody(e){if(!e)return[];if(e instanceof H)return this.bindDataRow(e);let t=e.body.filter(e=>e instanceof H),n=e.body.filter(e=>e instanceof z);return[...this.bindDataRows(t),...this.bindSubElements(n)]}bindDataRows(e){return e.flatMap(e=>this.bindDataRow(e))}bindDataRow(e){return e.callee?(e.callee instanceof xm?e.callee.elementList:[e.callee]).flatMap(zm).flatMap(e=>{let t=e.variables.pop(),n=e.variables.pop();return!t||!n?[]:[...e.variables.flatMap(e=>this.compiler.nodeReferee(e).getErrors()),...n?this.compiler.nodeReferee(n).getErrors():[],...this.compiler.nodeReferee(t).getErrors()]}):[]}bindSubElements(e){return e.flatMap(e=>e.type?this.compiler.bindNode(e).getErrors():[])}},Gg=class{constructor(e,t,n){this.compiler=e,this.metadata=t,this.element=t.declaration,this.filepath=n}interpret(){let e=[],t=[],r=Kg(this.element,this.compiler,this.filepath);if(!r||r.columns.length===0)return new n(void 0,e);let i=[];for(let n of this.element.body.body){let a=qg(n,r.columns,this.compiler);e.push(...a.getErrors()),t.push(...a.getInfos());let o=a.getValue();o.row&&i.push(o.row)}let a=F(this.element),o=this.metadata.example(this.compiler)||void 0;return new n({schemaName:r.schemaName,tableName:r.tableName,columns:r.columns.map(e=>e.name??``),values:i,example:o,token:a},e,[],t)}};function Kg(e,n,i){let a=e.name,o=e.parent;if(o instanceof z&&o.isKind(r.Table)){let e=n.nodeSymbol(o).getFiltered(t);if(!(e instanceof qp))return;let r=e.mergedColumns(n),s=e.interpretedName(n,i),c=s.schema,l=s.name;return a?{schemaName:c,tableName:l,columns:a.elementList.flatMap(e=>r.find(t=>t.name===P(e))||[])}:{schemaName:c,tableName:l,columns:r}}let s=pp(a);if(!s)return;let c=s.variables.map(e=>e.expression.variable?.value??``),l=c.at(-1)??``,u=c.length>1?c.slice(0,-1).join(`.`):void 0,d=n.parseFile(e.filepath).getValue().ast,f=n.nodeSymbol(d).getFiltered(t);if(!f)return;let p;if(u){let e=n.lookupMembers(f,I.Schema,u);e&&(p=n.lookupMembers(e,I.Table,l))}if(p||=n.lookupMembers(f,I.Table,l),!p)return;let m=p.originalSymbol;if(!(m instanceof qp))return;let ee=m.mergedColumns(n),te=s.args.map(e=>ee.find(t=>t.name===P(e))).filter(Boolean),ne=m.interpretedName(n,i);return{schemaName:ne.schema,tableName:ne.name,columns:te}}function qg(e,t,r){let i=[],a=[],o={},s=e.callee instanceof xm?e.callee.elementList:[e.callee];if(s.length!==t.length)return i.push(new T(w.INVALID_RECORDS_FIELD,`Expected ${t.length} values but got ${s.length}`,e)),new n({row:null,columnNodes:{}},i,[],a);let c=[];for(let e=0;e<t.length;e++){let n=s[e],l=t[e];o[l.name??``]=n;let u=Yg(n,l,r);i.push(...u.getErrors()),a.push(...u.getInfos());let d=u.getValue();c.push(d??{value:null,type:`expression`,token:F(n)})}return new n({row:c,columnNodes:o},i,[],a)}function Jg(e){return e instanceof ym&&e.value?.value||``}function Yg(e,t,r){let i=F(e),a=t.type(r),o=(a?.name??``).split(`(`)[0],s=!!a?.enumSymbol,c=t.increment(r),l=t.isNotNullSet(r)===!0,u=t.default(r),d=t.name??``,f=Uo(o,s),p=Ep(e),m=p===null?Jg(e):p,ee=p===null?`expression`:f;if(e instanceof ym)return new n({value:e.value?.value||``,type:`expression`,token:i},[],[]);if(gp(e)||_p(e)&&!zo(o)){let t=u&&u.value.toString().toLowerCase()!==`null`,r=Ho(o);return l&&!t&&!c&&!r?new n({value:null,type:f,token:i},[],[],[Hh(e,{colName:d})]):new n({value:null,type:f,token:i},[],[])}if(s){let a=t.type(r)?.enumSymbol,o=a?a.members(r).filter(e=>e.isKind(I.EnumField)).map(e=>e.name).filter(Boolean):[],s=N(e);return s===void 0&&(s=O(e)?e.expression.variable.value:void 0),o.includes(s)?new n({value:s,type:f,token:i},[],[]):new n({value:s,type:f,token:i},[],[],[Uh(e,{colName:d})])}if(Fo(o)||Io(o)||Ho(o)){let t=yp(e);if(t===null)return new n({value:m,type:ee,token:i},[],[],[Wh(e,{colName:d,typeName:o})]);if(Fo(o)&&!Number.isInteger(t))return new n({value:Math.floor(t),type:f,token:i},[],[],[Gh(e,{colName:d,value:t})]);let r=Io(o)&&a?.args?.length?{precision:Number(a.args[0]),scale:a.args.length>=2?Number(a.args[1]):0}:void 0;if(Io(o)&&r){let{precision:a,scale:s}=r,c=t.toString().split(`.`),l=c[0].replace(/^-/,``),u=c[1]||``,p=l.length+u.length,m=u.length;if(p>a)return new n({value:t,type:f,token:i},[],[],[Kh(e,{colName:d,typeName:o,value:t,precision:a,scale:s,totalDigits:p})]);if(m>s)return new n({value:t,type:f,token:i},[],[],[qh(e,{colName:d,typeName:o,value:t,precision:a,scale:s,decimalDigits:m})])}return new n({value:t,type:f,token:i},[],[])}if(Ro(o)){let t=wp(e);return t===null?new n({value:m,type:ee,token:i},[],[],[Jh(e,{colName:d})]):new n({value:t,type:f,token:i},[],[])}if(Vo(o)){let t=Ap(e);return t===null?new n({value:m,type:ee,token:i},[],[],[Yh(e,{colName:d})]):new n({value:t,type:f,token:i},[],[])}if(zo(o)){let t=Ep(e);if(t===null)return new n({value:m,type:ee,token:i},[],[],[Xh(e,{colName:d})]);let r=a?.args?.length?Number(a.args[0]):void 0;if(r&&!Number.isNaN(r)){let s=r,c=new TextEncoder().encode(t).length;if(c>s)return new n({value:t,type:f,token:i},[],[],[Zh(e,{colName:d,typeName:o,typeArg:String(a.args[0]),maxLength:s,actualLength:c})])}return new n({value:t,type:`string`,token:i},[],[])}return new n({value:m,type:ee,token:i},[],[])}var Xg={nodeMetadata(t,i){if(!A(i,r.Records))return n.create(e);let a=i.parent;if(a&&A(a,r.Table))return n.create(new Zm(i));let o=i.name;return o instanceof Cm&&o.callee?n.create(new Zm(i)):n.create(e)},nodeReferee(i,a){if(!O(a))return n.create(e);let o=a.parentOfKind(z);if(!o?.isKind(r.Records))return n.create(e);let s=i.parseFile(a.filepath).getValue().ast,c=i.nodeSymbol(s).getFiltered(t);if(!c)return n.create(void 0);let l=a.parentOfKind(bm);if(l?.parentNode===o)return Zg(i,o,a);let u=a.parentOfKind(Cm);return u?.parentNode===o&&l?.parentNode===u?Qg(i,u,a):u?.parentNode===o&&u.callee?.containsEq(a)?$g(i,c,a):t_(i,c,a)},bindNode(t,i){return A(i,r.Records)?n.create(void 0,new Wg(t,i).bind()):n.create(e)},interpretMetadata(t,r,i){return r instanceof Zm?r.declaration instanceof z?new Gg(t,r,i).interpret():n.create(void 0):n.create(e)}};function Zg(e,i,a){let o=i.parent;if(o instanceof z&&o.isKind(r.Table)){let n=e.nodeSymbol(o).getFiltered(t);if(n)return e_(e,n,a)}return new n(void 0)}function Qg(e,r,i){if(r.callee){let n;if(O(r.callee))n=e.nodeReferee(r.callee).getFiltered(t);else{let i=lp(r.callee);if(i&&i.length>0){let r=i[i.length-1];n=e.nodeReferee(r).getFiltered(t)}}if(n)return e_(e,n,i)}return new n(void 0)}function $g(e,t,r){if(!O(r))return new n(void 0);let i=mp(r)??``;if(!k(r.parentNode)){let a=e.lookupMembers(t,[I.Table,I.Schema],i);return a?n.create(a):new n(void 0,[new T(w.BINDING_ERROR,`Table '${i}' does not exist in Schema 'public'`,r)])}let a=Bm(e,r);if(!a){let a=e.lookupMembers(t,[I.Table,I.Schema],i);return a?n.create(a):new n(void 0,[new T(w.BINDING_ERROR,`Table or schema '${i}' does not exist`,r)])}if(a.isKind(I.Schema)){let t=e.lookupMembers(a,[I.Table,I.Schema],i);return t?n.create(t):new n(void 0,[new T(w.BINDING_ERROR,`Table or schema '${i}' does not exist`,r)])}return new n(void 0)}function e_(e,t,r){if(!O(r))return new n(void 0);let i=mp(r)??``,a=e.lookupMembers(t,I.Column,i);return a?n.create(a):new n(void 0,[new T(w.BINDING_ERROR,`Column '${i}' does not exist in Table 'public.${t.name}'`,r)])}function t_(e,t,r){if(!O(r))return new n(void 0);let i=mp(r)??``;if(!k(r.parentNode))return new n(void 0);let a=Bm(e,r);if(a){if(a.isKind(I.Schema)){let t=e.lookupMembers(a,[I.Enum,I.Schema],i);return t?n.create(t):new n(void 0,[new T(w.BINDING_ERROR,`Enum or schema '${i}' does not exist`,r)])}if(a.isKind(I.Enum)){let t=e.lookupMembers(a,I.EnumField,i);return t?n.create(t):new n(void 0,[new T(w.BINDING_ERROR,`Enum field '${i}' does not exist in Enum 'public.${a.name}'`,r)])}return new n(void 0)}let o=r.parentNode;if(o.leftExpression===r){if(k(o.parentNode)&&o.parentNode.leftExpression===o){let a=e.lookupMembers(t,I.Schema,i);return a?n.create(a):new n(void 0,[new T(w.BINDING_ERROR,`Schema '${i}' does not exist in Schema 'public'`,r)])}let a=e.lookupMembers(t,I.Enum,i);return a?n.create(a):new n(void 0,[new T(w.BINDING_ERROR,`Enum '${i}' does not exist in Schema 'public'`,r)])}return new n(void 0)}var n_=class{constructor(e,t){this.compiler=e,this.declarationNode=t}bind(){return!(this.declarationNode.parent instanceof R)&&!this.declarationNode.parent?.isKind(r.Project)?[]:this.bindBody(this.declarationNode.body)}bindBody(e){if(!e)return[];if(e instanceof H)return this.bindFields([e]);let[t,n]=_(e.body,e=>e instanceof H);return[...this.bindFields(t),...this.bindSubElements(n)]}bindFields(e){return e.flatMap(e=>e.callee?[e.callee,...e.args].flatMap(zm).flatMap(e=>{let t=e.tupleElements.length?e.tupleElements:e.variables.pop(),n=e.variables.pop();return!t||!n?[]:(Array.isArray(t)||(t=[t]),[...e.variables,n,...t].flatMap(e=>this.compiler.nodeReferee(e).getErrors()))}):[])}bindSubElements(e){return e.flatMap(e=>e.type?this.compiler.bindNode(e).getErrors():[])}};function r_(e,t){if(e.length===0)return[];let n;switch(t){case Vp.Table:n=[...Array(e.length-1).fill(I.Schema),I.Table];break;case Vp.Column:n=e.length===1?[I.Column]:[...Array(e.length-2).fill(I.Schema),I.Table,I.Column];break;case Vp.TableGroup:n=[...Array(e.length-1).fill(I.Schema),I.TableGroup];break;case Vp.Note:n=[...Array(e.length-1).fill(I.Schema),I.StickyNote];break;default:break}return e.map((e,t)=>({name:e,symbolKind:n[t]}))}function i_(e,t){let n=[],r=new Set,i=t=>{let a=t.intern();if(!r.has(a)){r.add(a);for(let n of e.fileDependencies(t))i(n);n.push(t)}};i(t);let a=new Map;return n.forEach((e,t)=>a.set(e.intern(),t)),a}function a_(e,n,r,i,a){let o=Um(e,a);if(!o||!e.isSymbolVisible(o,r))return;let s=new Set(e.reachableFiles(o.filepath).map(e=>e.absolute)),c=e.symbolMetadata(r).filter(e=>e instanceof Qm).filter(e=>s.has(e.declaration.filepath.absolute));if(!c.length)return;let l=i_(e,a),u=[...c].sort((e,t)=>{let n=l.get(e.declaration.filepath.intern())??-1,r=l.get(t.declaration.filepath.intern())??-1;return n===r?e.declaration.start-t.declaration.start:n-r}).map(n=>e.interpretMetadata(n,a).getFiltered(t)).filter(e=>!!e);if(!u.length)return;let d={};for(let e of u)for(let[t,{value:n,token:r}]of Object.entries(e))d[t.toLowerCase()]={originalKey:t,value:n,token:r};let f=n.metadata?Object.fromEntries(Object.entries(n.metadata).map(([e,t])=>[e.toLowerCase(),{originalKey:e,value:t}])):{};Object.entries(d).forEach(([e,t])=>{let{originalKey:r,value:a,token:o}=t;i[e]?i[e].assignBuiltinField(n,a,o):f[e]={originalKey:r,value:a}}),n.metadata=Object.fromEntries(Object.values(f).map(e=>[e.originalKey,e.value]))}var o_={[i.Color]:{isValidBuiltinFieldValue:Pf,message:`'color' must be a color literal`,assignBuiltinField(e,t){e.color=t}},[i.Note]:{isValidBuiltinFieldValue:D,message:`'note' must be a string literal`,assignBuiltinField(e,t,n){e.note={value:t,token:n}}}},s_=[i.Color,i.Note,i.Delete,i.Update,i.Inactive],c_=class{constructor(e,t,n){this.compiler=e,this.filepath=n,this.metadata=t,this.declarationNode=t.declaration,this.ref={}}interpret(){this.ref.token=F(this.declarationNode);let e=[...this.interpretName(),...this.interpretBody()],r=this.compiler.nodeSymbol(this.declarationNode).getFiltered(t);r instanceof Qp&&a_(this.compiler,this.ref,r,o_,this.filepath);let{infos:i}=this.validateRefConstraints();return n.create(this.ref,e,void 0,i)}interpretName(){if(!(this.declarationNode instanceof z))return[];let e=[],t=M(this.declarationNode.name)??[];return this.ref.name=t.pop()||null,t.length>1&&e.push(new T(w.UNSUPPORTED,`Nested schema is not supported`,this.declarationNode.name)),this.ref.schemaName=t.join(`.`)||null,e}interpretBody(){let e=this.metadata.op(this.compiler),t=this.metadata.leftColumns(this.compiler),n=this.metadata.leftTable(this.compiler),r=this.metadata.rightColumns(this.compiler),a=this.metadata.rightTable(this.compiler);if(La(t,r).every(([e,t])=>e?.originalSymbol===t?.originalSymbol))return[new T(w.SAME_ENDPOINT,`Two endpoints are the same`,this.declarationNode)];let o=uf(e),s=n?.interpretedName(this.compiler,this.filepath),c=a?.interpretedName(this.compiler,this.filepath),l=F(this.metadata.leftToken()),u=F(this.metadata.rightToken());if(this.ref.endpoints=this.declarationNode instanceof z?[{schemaName:s?.schema??null,tableName:s?.name??``,fieldNames:t.map(e=>e.name??``),relation:o[0],token:l},{schemaName:c?.schema??null,tableName:c?.name??``,fieldNames:r.map(e=>e.name??``),relation:o[1],token:u}]:[{schemaName:c?.schema??null,tableName:c?.name??``,fieldNames:r.map(e=>e.name??``),relation:o[1],token:u},{schemaName:s?.schema??null,tableName:s?.name??``,fieldNames:t.map(e=>e.name??``),relation:o[0],token:l}],!(this.declarationNode instanceof z))return[];let d=cp(this.declarationNode)[0];if(d.args[0]){let e=E(d.args[0]).getValue(),t=e.delete?.at(0)?.value;this.ref.onDelete=t instanceof gm?ap(t):P(t);let n=e.update?.at(0)?.value;this.ref.onUpdate=n instanceof gm?ap(n):P(n),this.ref.color=e.color?.length?Fp(e.color?.at(0)?.value):void 0;let[r]=e[i.Note]||[];this.ref.note=r&&{value:Np(N(r?.value)),token:F(r)},this.ref.inactive=e.inactive?.length?!0:void 0,this.ref.metadata=Rp(e,s_)}return[]}validateRefConstraints(){return this.metadata.cardinalities(this.compiler)?{infos:[...Mg(this.compiler,this.metadata,`right`),...Mg(this.compiler,this.metadata,`left`)]}:{infos:[]}}};function l_(e){let t=e.parent;for(;t;){if(t instanceof z&&t.isKind(r.Ref))return t.body?.containsEq(e)??!1;t=t.parent}return!1}var u_={getDuplicateError(e,t,n){return new T(w.DUPLICATE_NAME,`Ref '${e}' already exists in schema '${t}'`,n)}},d_={nodeSymbol(i,a){if(!A(a,r.Ref)||!(a.parentNode instanceof R))return n.create(e);let o=i.nodeFullname(a).getFiltered(t)?.at(-1);return o===void 0?n.create(e):new n(i.symbolFactory.create(Qp,{declaration:a,name:o},a.filepath))},symbolMembers(t,r){return r.isKind(I.Ref)?new n([]):n.create(e)},nodeReferee(r,i){if(!O(i)&&!k(i)||!l_(i)||i.parentOfKind(B))return n.create(e);let a=r.parseFile(i.filepath).getValue().ast,o=r.nodeSymbol(a).getValue();return o===t?n.create(void 0):f_(r,o,i)},nodeMetadata(t,a){if(A(a,r.Ref)){let t=cp(a)[0];if(!(t instanceof H)||!t.callee)return n.create(e);let r=t.callee;return!(r instanceof _m)||!r.op?n.create(e):n.create(new Km(a))}if(a instanceof B){if(!(a.name instanceof gm)||ap(a.name)?.toLowerCase()!==i.Ref)return n.create(e);let t=a.parent;return t instanceof z&&t.isKind(r.TablePartial)?n.create(new Jm(a)):n.create(new Km(a))}return n.create(e)},bindNode(t,i){return A(i,r.Ref)?n.create(void 0,new n_(t,i).bind()):n.create(e)},interpretMetadata(t,r,i){return r instanceof Km?new c_(t,r,i).interpret():n.create(e)}};function f_(e,r,i){if(!O(i))return new n(void 0);let a=mp(i)??``;if(!k(i.parentNode)){let r=i.parentNode;if(r&&k(r.parentNode)&&r.parentNode.rightExpression===r){let o=r.parentNode.leftExpression,s=k(o)?o.rightExpression??o:o,c=e.nodeReferee(s).getFiltered(t);if(c?.isKind(I.Table)){let r=e.lookupMembers(c,I.Column,a);if(r)return n.create(r);let o=c.declaration?e.nodeFullname(c.declaration).getFiltered(t)?.join(`.`)??c.name:c.name;return new n(void 0,[new T(w.BINDING_ERROR,`Column '${a}' does not exist in Table '${o}'`,i)])}}return n.create(void 0)}let o=Bm(e,i);if(o){if(o.isKind(I.Schema)){let t=e.lookupMembers(o,[I.Table,I.Schema],a);return t?n.create(t):new n(void 0,[new T(w.BINDING_ERROR,`Table or schema '${a}' does not exist`,i)])}if(o.isKind(I.Table)){let r=e.lookupMembers(o,I.Column,a);return r?n.create(r):new n(void 0,[new T(w.BINDING_ERROR,`Column '${a}' does not exist in Table '${o.declaration?e.nodeFullname(o.declaration).getFiltered(t)?.join(`.`)??o.name:o.name}'`,i)])}return new n(void 0)}let s=i.parentNode;if(s.leftExpression===i){if(k(s.parentNode)&&s.parentNode.leftExpression===s){let t=e.lookupMembers(r,I.Schema,a);return t?n.create(t):new n(void 0,[new T(w.BINDING_ERROR,`Schema '${a}' does not exist`,i)])}let t=Hm(e,r);if(t){let r=e.lookupMembers(t,I.Table,a);if(r)return n.create(r)}let o=e.lookupMembers(r,I.Table,a);return o?n.create(o):new n(void 0,[new T(w.BINDING_ERROR,`Table '${a}' does not exist in Schema 'public'`,i)])}return new n(void 0)}var p_={visibleName(e,n){let r=n instanceof L?n:n.useSpecifierDeclaration instanceof mm?n.useSpecifierDeclaration:n.declaration;if(r instanceof mm&&n instanceof Up&&r.getSymbolKind()!==n.kind&&(r=n.declaration),!r)return;let i=e.nodeAlias(r).getFiltered(t);return i===void 0?e.nodeFullname(r).getFiltered(t):[i]}},m_={nodeSymbol(r,i){if(!qf(i)||!i.name)return n.create(e);let a=i.getSymbolKind();if(a===void 0||a===I.Schema)return n.create(e);let o=r.nodeReferee(i.name).getFiltered(t);return o&&!o.canBeImported?n.create(e):n.create(r.symbolFactory.create(lm,{useSpecifierDeclaration:i,declaration:o?.declaration,usedSymbol:o,kind:a,name:p_.visibleName(r,i)?.at(-1)},i.filepath),o===void 0?[new T(w.BINDING_ERROR,`Failed to resolve the import of ${a} '${(M(i.name)||[]).map(R_).join(`.`)}'`,i)]:[])},symbolMembers(r,i){if(!(i instanceof lm))return n.create(e);if(i.originalSymbol===i)return n.create([]);let a=r.symbolMembers(i.originalSymbol).getFiltered(t);return n.create(a??[])},nodeReferee(r,i){if(!O(i)&&!Gf(i))return n.create(e);let a=i.parentOfKind(mm);if(!a)return n.create(e);let o=a.name===i||k(a.name)&&a.name.rightExpression===i?a.getSymbolKind():I.Schema;if(o===void 0)return n.create(void 0);let s=M(i.parentOfKind(_m)),c=P(i)??s?.at(-1);if(c===void 0)return n.create(void 0);let l=i.parentOfKind(pm);if(l?.importPath?.value===void 0)return n.create(void 0);let u=r.layout.resolveFileSpecifier(i.filepath,l.importPath.value);if(!u)return n.create(void 0,[new T(w.BINDING_ERROR,`Import path must be relative, got '${l.importPath.value}'`,i)]);if(!r.layout.exists(u))return n.create(void 0,[new T(w.NONEXISTENT_MODULE,`${o} '${s?.join(`.`)??c}' does not exist in file ${u.toKey()}. Does the file exist?`,i)]);let d=i.parentOfKind(_m)?.leftExpression;if(d&&d!==i){let e=r.nodeReferee(d).getFiltered(t);if(!e)return n.create(void 0);let a=r.lookupMembers(e,o,c);return a?n.create(a):new n(void 0,[new T(w.BINDING_ERROR,`Could not find ${o} '${c}'`,i)])}let f=h_(r,u,c,o);return f?n.create(f):n.create(void 0,[new T(w.BINDING_ERROR,`${o} '${c}' does not exist in file ${u.toKey()}`,i)])},bindNode(t,r){if(Kf(r)){let e=[];if(r.importPath?.value){let n=t.layout.resolveFileSpecifier(r.filepath,r.importPath.value);(!n||!t.layout.exists(n))&&e.push(new T(w.NONEXISTENT_MODULE,`Failed to resolve the non-existent file '${r.importPath.value}'`,r.importPath))}if(r.specifiers){let n=t.bindNode(r.specifiers);e.push(...n.getErrors())}return new n(void 0,e)}return r instanceof hm?new n(void 0,r.specifiers.flatMap(e=>t.bindNode(e).getErrors())):qf(r)?r.name?new n(void 0,t.nodeReferee(r.name).getErrors()):new n(void 0):n.create(e)},interpretSymbol(r,i,a){if(!(i instanceof lm))return n.create(e);if(!i.usedSymbol)return n.create(void 0);let o=r.interpretSymbol(i.usedSymbol,a);if(o.hasValue(t))return n.create(void 0);let s=o.getValue();if(!s||Array.isArray(s))return o;let c=i.name;return c&&`name`in s?n.create({...s,name:c}):n.create(s)}};function h_(e,n,r,i,a=new Set){if(!n||a.has(n.intern()))return;a.add(n.intern());let o=e.usableMembers(n).getFiltered(t);if(!o)return;let s=o.nonSchemaMembers.find(e=>e.name===r&&e.isKind(i));if(s)return s;let c=o.schemaMembers.find(e=>e.name===r);if(c?.isKind(I.Schema))return c;for(let t of o.reuses.selective)if(M(t.alias??t.name)?.at(-1)===r){let o=t.parentOfKind(pm);if(o?.importPath?.value){let s=h_(e,e.layout.resolveFileSpecifier(n,o.importPath.value),M(t.name)?.at(-1)??r,i,a);if(s)return s}}for(let{importPath:t}of o.reuses.wildcard){let n=h_(e,t,r,i,a);if(n)return n}}function g_(e){if(e instanceof bo){let{ast:n}=this.parseFile(e).getValue(),r=this.nodeSymbol(n).getFiltered(t);return this.usableMembers(r)}let r=e.filepath,i=v_(this,r),{nonSchemaMembers:a,schemaMembers:o}=e instanceof sm?y_(this,e,i):b_(this,e,i);return new n({nonSchemaMembers:a,schemaMembers:o,reuses:{selective:i.selectiveReuses,wildcard:i.wildcardReuses},uses:{selective:i.selectiveUses,wildcard:i.wildcardUses}},i.parseResult.getErrors(),i.parseResult.getWarnings())}function __(e,n,r){let i=n.qualifiedName,a;if(a=r instanceof mm?p_.visibleName(e,r):e.nodeFullname(r).getFiltered(t),!a)return{kind:`none`};let o=r instanceof mm&&r.getSymbolKind()===I.Schema?a:a.length<=1?[y]:a.slice(0,-1);return o.length<i.length||!i.every((e,t)=>e===o[t])?{kind:`none`}:o.length===i.length?{kind:`direct`}:{kind:`child`,schemaName:o[i.length]}}function v_(e,t){let n=e.parseFile(t),{ast:r}=n.getValue(),i=[],a=[],o=[],s=[];for(let n of r.body){if(!(n instanceof pm)||!n.specifiers||!n.importPath)continue;let r=e.layout.resolveFileSpecifier(t,n.importPath.value);r&&(n.isReuse?n.specifiers instanceof Tm?i.push({importPath:r,node:n.specifiers}):a.push(...n.specifiers.specifiers):n.specifiers instanceof Tm?o.push({importPath:r,node:n.specifiers}):s.push(...n.specifiers.specifiers))}return{ast:r,parseResult:n,selectiveReuses:a,wildcardReuses:i,selectiveUses:s,wildcardUses:o}}function y_(e,n,r){let{ast:i,selectiveUses:a,selectiveReuses:o}=r,s=n.filepath,c=new Map([[y,e.symbolFactory.create(Wp,{name:y},s)]]);for(let n of i.declarations){let r=e.nodeFullname(n).getFiltered(t)||[],i=r.length<=1?y:r[0];c.has(i)||c.set(i,e.symbolFactory.create(Wp,{name:i},s))}x_(e,[...a,...o],c,s);for(let t of e.reachableFiles(s))for(let n of C_(e,t))c.has(n)||c.set(n,e.symbolFactory.create(Wp,{name:n},s));let l=[...c.values()],u=[],d=l.find(e=>e.isPublicSchema());if(d){let n=e.usableMembers(d).getFiltered(t);n&&u.push(...n.nonSchemaMembers)}return{nonSchemaMembers:u,schemaMembers:l}}function b_(e,n,r){let{ast:i,selectiveUses:a,selectiveReuses:o}=r,s=[],c=new Map;for(let r of i.body){if(!(r instanceof z))continue;let i=__(e,n,r),a=e.nodeAlias(r).getFiltered(t),o=e.nodeSymbol(r).getFiltered(t);if(a!==void 0&&n.isPublicSchema()&&o&&s.push(e.symbolFactory.create(cm,{kind:o.kind,declaration:r,aliasedSymbol:o,name:a},n.filepath)),i.kind!==`none`)if(i.kind===`direct`){let n=e.nodeSymbol(r).getFiltered(t);if(!n)continue;s.push(n)}else c.has(i.schemaName)||c.set(i.schemaName,e.symbolFactory.create(Wp,{name:i.schemaName,parent:n},n.filepath))}S_(e,[...a,...o],n,c);let l=n.qualifiedName;for(let t of e.reachableFiles(n.filepath))for(let r of w_(e,t,l))c.has(r)||c.set(r,e.symbolFactory.create(Wp,{name:r,parent:n},n.filepath));return{nonSchemaMembers:s,schemaMembers:[...c.values()]}}function x_(e,t,n,r){for(let i of t){if(i.getSymbolKind()!==I.Schema)continue;let t=p_.visibleName(e,i)?.at(0);t!==void 0&&!n.has(t)&&n.set(t,e.symbolFactory.create(Wp,{name:t},r))}}function S_(e,t,n,r){let i=n.qualifiedName;for(let a of t){if(a.getSymbolKind()!==I.Schema)continue;let t=p_.visibleName(e,a);if(!t||t.length<=i.length||!i.every((e,n)=>e===t[n]))continue;let o=t[i.length];o&&!r.has(o)&&r.set(o,e.symbolFactory.create(Wp,{name:o,parent:n},n.filepath))}}function C_(e,n){let{ast:r}=e.parseFile(n).getValue(),i=[];for(let n of r.declarations){let r=e.nodeFullname(n).getFiltered(t)||[],a=r.length<=1?y:r[0];i.push(a)}let a=v_(e,n);for(let t of[...a.selectiveUses,...a.selectiveReuses]){if(t.getSymbolKind()!==I.Schema)continue;let n=p_.visibleName(e,t)?.at(0);n!==void 0&&i.push(n)}return i}function w_(e,n,r){let{ast:i}=e.parseFile(n).getValue(),a=[];for(let n of i.declarations){let i=e.nodeFullname(n).getFiltered(t)||[],o=i.length<=1?[y]:i.slice(0,-1);o.length>r.length&&r.every((e,t)=>e===o[t])&&a.push(o[r.length])}let o=v_(e,n);for(let t of[...o.selectiveUses,...o.selectiveReuses]){if(t.getSymbolKind()!==I.Schema)continue;let n=p_.visibleName(e,t);!n||n.length<=r.length||r.every((e,t)=>e===n[t])&&a.push(n[r.length])}return a}var T_=class{constructor(e,t){this.compiler=e,this.declarationNode=t}bind(){let e=[];return this.declarationNode.attributeList&&e.push(...this.bindHeaderSettings(this.declarationNode.attributeList)),this.declarationNode.body instanceof U&&e.push(...this.bindBody(this.declarationNode.body)),e}bindHeaderSettings(e){return E(e).getValue().dep?.flatMap(e=>e.value?this.bindInlineDepValue(e.value):[])??[]}bindInlineDepValue(e){return zm(e).flatMap(e=>[...e.variables,...e.tupleElements].flatMap(e=>this.compiler.nodeReferee(e).getErrors()))}bindBody(e){if(!e)return[];if(e instanceof H)return this.bindFields([e]);let[t,n]=_(e.body,e=>e instanceof H);return[...this.bindFields(t),...this.bindSubElements(n)]}bindFields(e){return(e=>e.flatMap(e=>{if(!e.callee)return[];let t=[],n=[e.callee,...e.args];if(g(n)instanceof W){let e=E(g(n)).getValue();t.push(...e.ref?.flatMap(e=>e.value?this.bindInlineRef(e.value):[])||[]),t.push(...e.dep?.flatMap(e=>e.value?this.bindInlineDepValue(e.value):[])||[]),t.push(...e.default?.flatMap(e=>e.value?this.tryToBindEnumFieldRef(e.value):[])||[]),n.pop()}return n[1]&&this.tryToBindColumnType(n[1]),t}))(e.filter(e=>!jf(e.callee)))}tryToBindColumnType(e){let t=up(e);if(!t)return;let n=t.variables.pop();n&&this.compiler.nodeReferee(n).getErrors()}tryToBindEnumFieldRef(e){if(D(e))return[];if(O(e)){let t=e.expression.variable?.value?.toLowerCase();if(t&&So.includes(t))return[]}let t=up(e);if(!t){if(k(e)){let t=e,n=[];return t.leftExpression&&n.push(...this.compiler.nodeReferee(t.leftExpression).getErrors()),t.rightExpression&&n.push(...this.compiler.nodeReferee(t.rightExpression).getErrors()),n}return[]}let n=t.variables.pop(),r=t.variables.pop();if(!n||!r)return[];let i=[],a=t.variables;for(let e of a)i.push(...this.compiler.nodeReferee(e).getErrors());return i.push(...this.compiler.nodeReferee(r).getErrors()),i.push(...this.compiler.nodeReferee(n).getErrors()),i}bindInlineRef(e){return zm(e).flatMap(e=>{let t=e.variables.pop(),n=e.variables.pop();if(!t)return[];let r=e.variables;if(n){let e=[];for(let t of r)e.push(...this.compiler.nodeReferee(t).getErrors());return e.push(...this.compiler.nodeReferee(n).getErrors()),e.push(...this.compiler.nodeReferee(t).getErrors()),e}return this.compiler.nodeReferee(t).getErrors()})}bindSubElements(e){return e.flatMap(e=>e.type?this.compiler.bindNode(e).getErrors():[])}},E_=class{constructor(e,t,n,r){this.compiler=e,this.declarationNode=t,this.symbol=n,this.filepath=r,this.table={name:void 0,schemaName:void 0,alias:null,fields:[],token:void 0,indexes:[],partials:[],checks:[]},this.pkColumns=[]}interpret(){this.table.token=this.symbol?.token??F(this.declarationNode);let e=[];if(this.symbol){let{name:e,schema:t}=this.symbol.interpretedName(this.compiler,this.filepath);this.table.name=e,this.table.schemaName=t}else e.push(...this.interpretName(this.declarationNode.name));let t=[...e,...this.interpretAlias(this.declarationNode.alias),...this.interpretSettingList(this.declarationNode.attributeList),...this.interpretBody(this.declarationNode.body)];return this.pkColumns.length>=2&&(this.table.indexes.push({columns:this.pkColumns.map(({name:e,token:t})=>({value:e,type:`column`,token:t})),token:{start:{offset:-1,line:-1,column:-1},end:{offset:-1,line:-1,column:-1},filepath:this.declarationNode.filepath},pk:!0}),this.pkColumns.forEach(e=>{e.pk=!1})),this.attachTableAndColumnMetadata(),n.create(this.table,t)}attachTableAndColumnMetadata(){if(!this.symbol)return;a_(this.compiler,this.table,this.symbol,O_,this.filepath);let e=this.compiler.symbolMembers(this.symbol).getFiltered(t)??[];for(let t of e){if(!t.isKind(I.Column))continue;let e=this.table.fields.find(e=>e.name===t.name);e&&a_(this.compiler,e,t,k_,this.filepath)}}interpretName(e){let{name:t,schemaName:n}=Pp(e);return n.length>1?(this.table.name=t,this.table.schemaName=n.join(`.`),[new T(w.UNSUPPORTED,`Nested schema is not supported`,e)]):(this.table.name=t,this.table.schemaName=n.length?n[0]:null,[])}interpretAlias(e){if(!e)return[];let t=mp(e);return t&&(this.table.alias=t),[]}interpretSettingList(e){let t=E(e).getValue();this.table.headerColor=t[i.HeaderColor]?.length?Fp(t[i.HeaderColor]?.at(0)?.value):void 0;let[n]=t[i.Note]||[];return this.table.note=n&&{value:Np(N(n?.value)),token:F(n)},this.table.metadata=Rp(t,Object.keys(O_)),[]}interpretBody(e){let[t,n]=_(e.body,e=>e instanceof H);return[...this.interpretFields(t),...this.interpretSubElements(n)]}interpretSubElements(e){return e.flatMap(e=>{switch(e.getElementKind()){case r.Note:return this.table.note={value:Np(N(e.body instanceof U?e.body.body[0].callee:e.body.callee)),token:F(e)},[];case r.Indexes:return this.interpretIndexes(e);case r.Checks:return this.interpretChecks(e);case r.Records:return[];default:return[]}})}interpretInjection(e,n){let r=[],i={order:n,token:F(e)};i.name=P(e.expression)||``;let a=e.expression?this.compiler.nodeReferee(e.expression).getFiltered(t):void 0,o=this.compiler.parseFile(this.filepath).getValue().ast,s=this.compiler.nodeSymbol(o).getFiltered(t);if(a&&!s.inNestedSchema(this.compiler,a)){let n=this.compiler.nodeSymbol(this.declarationNode).getFiltered(t),o=n?(this.compiler.symbolUses(n).getFiltered(t)??[]).find(e=>e.filepath.equals(this.filepath)):void 0;r.push(new T(w.UNSUPPORTED,`TablePartial ${i.name} from '${a.filepath}' should be imported. Currently, importing a table that uses a table partial requires that table partial to also be in scope.`,o?.useSpecifierDeclaration??e))}return this.table.partials.push(i),r}interpretFields(e){let n=this.symbol??this.compiler.nodeSymbol(this.declarationNode).getFiltered(t),r=(n?this.compiler.symbolMembers(n).getFiltered(t)??[]:[]).filter(e=>e.isKind(I.Column)).length?[]:[new T(w.EMPTY_TABLE,`A Table must have at least one column`,this.declarationNode)],i=e.flatMap((e,t)=>jf(e.callee)?this.interpretInjection(e.callee,t):this.interpretColumn(e));return[...r,...i]}interpretColumn(e){let n=[],r={},a=this.compiler.nodeSymbol(e).getFiltered(t);r.name=mp(e.callee),r.token=F(e);let o=Lp(this.compiler,e.args[0]);r.type=o.getValue(),n.push(...o.getErrors()),r.pk=a?.pk(this.compiler)||!1,r.unique=a?.isUniqueSet(this.compiler)||!1,r.increment=a?.isIncrementSet(this.compiler)||void 0,r.not_null=a?.isNotNullSet(this.compiler),r.dbdefault=a?.default(this.compiler),r.note=a?.note(this.compiler);let s=this.compiler.nodeSettings(e).getFiltered(t)??{};r.metadata=Rp(s,D_);let c=this.compiler.parseFile(this.filepath).getValue().ast,l=this.compiler.nodeSymbol(c).getFiltered(t);return r.inline_refs=(s[i.Ref]||[]).flatMap(e=>{let r=this.compiler.nodeMetadata(e).getFiltered(t);if(!r)return[];let i=r.owners(this.compiler);if(l&&i.length>0&&!i.some(e=>e===l))return[];let a=this.compiler.interpretMetadata(r,this.filepath);if(a.hasValue(t))return[];n.push(...a.getErrors());let o=a.getValue();if(!o?.endpoints?.[0])return[];let s=o.endpoints[0],c=r.op(this.compiler);return c?{schemaName:s.schemaName,tableName:s.tableName,fieldNames:s.fieldNames,relation:c,token:s.token}:[]}),r.inline_deps=(s[i.Dep]||[]).flatMap(e=>{let r=this.compiler.nodeMetadata(e).getFiltered(t);if(!r)return[];let i=r.owners(this.compiler);if(l&&i.length>0&&!i.some(e=>e===l))return[];let a=this.compiler.interpretMetadata(r,this.filepath);if(a.hasValue(t))return[];n.push(...a.getErrors());let o=a.getValue();if(!o?.edges?.[0])return[];let s=e.value;if(!(s instanceof V))return[];let c=s.op?.value;if(c!==`->`&&c!==`<-`)return[];let u=o.edges[0],d=c===`->`?u.downstream:u.upstream;return{schemaName:d.schemaName,tableName:d.tableName,fieldNames:d.fieldNames,direction:c,token:d.token}}),r.checks=(s[i.Check]||[]).map(e=>({token:F(e),expression:e.value.value.value})),this.table.fields.push(r),r.pk&&this.pkColumns.push(r),n}interpretIndexes(e){let n=this.compiler.nodeMetadata(e).getFiltered(t);if(!n)return[];let r=this.compiler.interpretMetadata(n,this.filepath);if(r.hasValue(t))return[];let i=r.getValue();return Array.isArray(i)?this.table.indexes.push(...i):i&&this.table.indexes.push(i),r.getErrors()}interpretChecks(e){let n=this.compiler.nodeMetadata(e).getFiltered(t);if(!n)return[];let r=this.compiler.interpretMetadata(n,this.filepath);if(r.hasValue(t))return[];let i=r.getValue();return Array.isArray(i)?this.table.checks.push(...i):i&&this.table.checks.push(i),r.getErrors()}},D_=[i.Note,i.Ref,i.PrimaryKey,i.PK,i.NotNull,i.Null,i.Unique,i.Increment,i.Default,i.Check],O_={[i.Note]:{isValidBuiltinFieldValue:D,message:`'note' must be a string literal`,assignBuiltinField(e,t,n){e.note={value:t,token:n}}},[i.HeaderColor]:{isValidBuiltinFieldValue:Pf,message:`'headercolor' must be a color literal`,assignBuiltinField(e,t){e.headerColor=t}}},k_={[i.Note]:{isValidBuiltinFieldValue:D,message:`'note' must be a quoted string`,assignBuiltinField(e,t,n){e.note={value:t,token:n}}}},A_={getDuplicateError(e,t,n){return new T(w.DUPLICATE_NAME,`Table '${e}' already exists in schema '${t}'`,n)},getColumnDuplicateError(e,t){return new T(w.DUPLICATE_COLUMN_NAME,`Duplicate column '${e}'`,t)},getPartialInjectionDuplicateError(e,t){return new T(w.DUPLICATE_TABLE_PARTIAL_INJECTION_NAME,`Duplicate table partial injection '${e}'`,t)}},j_={nodeSymbol(i,a){let o=i.nodeFullname(a).getFiltered(t)?.at(-1);return A(a,r.Table)?new n(i.symbolFactory.create(qp,{declaration:a,name:o},a.filepath)):j(a,r.Table)?jf(a.callee)?new n(i.symbolFactory.create(em,{declaration:a,name:o},a.filepath)):new n(i.symbolFactory.create(Jp,{declaration:a,name:o},a.filepath)):n.create(e)},symbolMembers(r,i){if(!i.isKind(I.Table))return n.create(e);let a=i.declaration;if(!(a instanceof z))return new n([]);let o=cp(a),s=[],c=[];for(let e of o){let n=r.nodeSymbol(e),i=n.getFiltered(t);i&&(s.push(i),c.push(...n.getErrors()))}let l=new Map;for(let e of s){if(!e.isKind(I.Column,I.PartialInjection)||!e.declaration)continue;let t=`${e.kind}:${e.name}`;if(e.name!==void 0){let n=e.declaration instanceof z&&e.declaration.name?e.declaration.name:e.declaration,r=l.get(t);r?e.isKind(I.Column)?(c.push(A_.getColumnDuplicateError(e.name,r)),c.push(A_.getColumnDuplicateError(e.name,n))):(c.push(A_.getPartialInjectionDuplicateError(e.name,r)),c.push(A_.getPartialInjectionDuplicateError(e.name,n))):l.set(t,n)}}let u=[];for(let e=0;e<s.length;e++){let n=s[e];if(!(n.declaration instanceof H)||!jf(n.declaration.callee))continue;let i=n.declaration.callee.expression,o=P(i);if(i===void 0||o===void 0)continue;let l=r.nodeReferee(i).getFiltered(t);if(!l){c.push(new T(w.BINDING_ERROR,`TablePartial '${o}' does not exist in Schema 'public'`,i||a));continue}let d=r.symbolMembers(l).getFiltered(t);if(d){let t=d.flatMap(e=>{if(!e.declaration)return[];let t=e.name;return t===void 0?e:r.symbolFactory.create(um,{kind:I.Column,injectionDeclaration:n.declaration,declaration:e.declaration,name:t},a.filepath)});u.push({index:e,partialMembers:t})}}for(let e=u.length-1;e>=0;e--){let{index:t,partialMembers:n}=u[e];s.splice(t,0,...n)}let d=new Set(s.filter(e=>e.isKind(I.Column)&&!(e instanceof um)).map(e=>e.name));return new n(Pa([...s.filter(e=>!(e instanceof um&&d.has(e.name)))].reverse(),e=>e.isKind(I.Column)?e.name:e).reverse(),c)},nodeReferee(a,o){if(Qf(o,i.Dep)&&o.parent instanceof z&&o.parent.isKind(r.Table)&&!Yf(o,r.Table)){let e=a.parseFile(o.filepath).getValue().ast,r=a.nodeSymbol(e).getValue();return r===t?n.create(void 0):F_(a,r,o)}if(!Yf(o,r.Table))return n.create(e);let s=a.parseFile(o.filepath).getValue().ast,c=a.nodeSymbol(s).getValue();return c===t?n.create(void 0):O(o)&&o.parentNode instanceof V&&o.parentNode.op?.value===`~`?M_(a,c,o):Xf(o,1)?N_(a,c,o):Qf(o,i.Ref)?P_(a,c,o):Qf(o,i.Dep)?F_(a,c,o):Xf(o,0)?n.create(e):I_(a,c,o)},bindNode(i,a){if(!A(a,r.Table))return n.create(e);let o=new T_(i,a).bind(),s=i.nodeSymbol(a).getFiltered(t);return s&&o.push(...i.symbolMembers(s).getErrors()),n.create(void 0,o)},interpretSymbol(t,r,i){return r instanceof qp?r.declaration instanceof z?new E_(t,r.declaration,r,i).interpret():n.create(void 0):n.create(e)}};function M_(e,r,i){let a=P(i)??``,o=e.symbolMembers(r).getFiltered(t);if(o){let t=o.find(e=>e instanceof Wp&&e.isPublicSchema());if(t){let r=e.lookupMembers(t,I.TablePartial,a);if(r)return n.create(r)}}let s=e.lookupMembers(r,I.TablePartial,a);return s?n.create(s):new n(void 0,[new T(w.BINDING_ERROR,`TablePartial '${a}' does not exist in Schema 'public'`,i)])}function N_(e,t,r){if(!O(r))return new n(void 0);let i=mp(r)??``;if(!k(r.parentNode))return n.create(e.lookupMembers(t,I.Enum,i));let a=Bm(e,r);if(a){if(a.isKind(I.Schema)){let t=rp(r),o=e.lookupMembers(a,t?I.Enum:I.Schema,i);return o?n.create(o):new n(void 0,[new T(w.BINDING_ERROR,t?`Enum '${i}' does not exist in Schema 'public'`:`Schema '${i}' does not exist in Schema 'public'`,r)])}return new n(void 0)}if(r.parentNode.leftExpression===r){let a=e.lookupMembers(t,I.Schema,i);return a?n.create(a):new n(void 0,[new T(w.BINDING_ERROR,`Schema '${i}' does not exist in Schema 'public'`,r)])}return new n(void 0)}function P_(e,i,a){if(!O(a))return new n(void 0);let o=mp(a)??``;if(!k(a.parentNode)){let s=a.parent,c=s instanceof z&&s.isKind(r.Table)?e.nodeSymbol(s).getFiltered(t):void 0;if(c){let t=e.lookupMembers(c,I.Column,o);if(t)return n.create(t)}let l=e.lookupMembers(i,I.Column,o);return l?n.create(l):new n(void 0,[new T(w.BINDING_ERROR,`Column '${o}' does not exist in Table 'public.${c?.name??``}'`,a)])}return Vm(e,i,a,I.Column)}function F_(e,i,a){if(!O(a))return new n(void 0);let o=Yf(a,r.Table),s=mp(a)??``;if(!k(a.parentNode))if(o){let o=a.parent,c=o instanceof z&&o.isKind(r.Table)?e.nodeSymbol(o).getFiltered(t):void 0;if(c){let t=e.lookupMembers(c,I.Column,s);if(t)return n.create(t)}let l=e.lookupMembers(i,I.Column,s);return l?n.create(l):new n(void 0,[new T(w.BINDING_ERROR,`Column '${s}' does not exist in Table 'public.${c?.name??``}'`,a)])}else{let t=e.lookupMembers(i,I.Table,s);return t?n.create(t):new n(void 0,[new T(w.BINDING_ERROR,`Table '${s}' does not exist in Schema 'public'`,a)])}return Vm(e,i,a,o?I.Column:I.Table)}function I_(e,t,r){if(!O(r))return new n(void 0);let i=mp(r)??``;if(!k(r.parentNode))return So.includes(i.toLowerCase())?new n(void 0):n.create(e.lookupMembers(t,I.Enum,i));let a=Bm(e,r);if(a){if(a.isKind(I.Schema)){let t=e.lookupMembers(a,[I.Enum,I.Schema],i);return t?n.create(t):new n(void 0,[new T(w.BINDING_ERROR,`Enum or schema '${i}' does not exist`,r)])}if(a.isKind(I.Enum)){let t=e.lookupMembers(a,I.EnumField,i);return t?n.create(t):new n(void 0,[new T(w.BINDING_ERROR,`Enum field '${i}' does not exist in Enum 'public.${a.name}'`,r)])}return new n(void 0)}let o=r.parentNode;if(o.leftExpression===r){if(k(o.parentNode)&&o.parentNode.leftExpression===o){let a=e.lookupMembers(t,I.Schema,i);return a?n.create(a):new n(void 0,[new T(w.BINDING_ERROR,`Schema '${i}' does not exist in Schema 'public'`,r)])}let a=e.lookupMembers(t,I.Enum,i);return a?n.create(a):new n(void 0,[new T(w.BINDING_ERROR,`Enum '${i}' does not exist in Schema 'public'`,r)])}return new n(void 0)}function L_(e){return e?e.split(``).every(e=>Cf(e)||wf(e))&&!wf(e[0]):!1}function R_(e){return L_(e)?e:`"${V_(e)}"`}function z_(...e){return e.map(R_).join(`.`)}function B_(e){let t=``,n=0,r={n:`
9
9
  `,t:` `,r:`\r`,b:`\b`,f:`\f`,v:`\v`,0:`\0`,"\\":`\\`,'"':`"`,"'":`'`,"`":"`"};for(;n<e.length;)if(e[n]===`\\`&&n+1<e.length){let i=e[n+1];if(i===`u`&&n+5<e.length){let r=e.slice(n+2,n+6);if(/^[0-9a-fA-F]{4}$/.test(r)){t+=String.fromCharCode(parseInt(r,16)),n+=6;continue}}i in r?(t+=r[i],n+=2):(t+=i,n+=2)}else t+=e[n],n++;return t}function V_(e){let t=``;for(let n=0;n<e.length;n++){let r=e[n];switch(r){case`\\`:t+=`\\\\`;break;case`"`:t+=`\\"`;break;case`'`:t+=`\\'`;break;case`
10
10
  `:t+=`\\n`;break;case` `:t+=`\\t`;break;case`\r`:t+=`\\r`;break;case`\b`:t+=`\\b`;break;case`\f`:t+=`\\f`;break;case`\v`:t+=`\\v`;break;case`\0`:t+=`\\0`;break;default:t+=r}}return t}function H_(e){if(e==null)return`null`;if(typeof e==`boolean`)return e?`true`:`false`;if(typeof e==`number`)return String(e);if(typeof e==`string`)return`'${V_(e)}'`;let{value:t,type:n}=e;if(t==null||Ro(n)&&typeof t==`string`&&t.toLowerCase()===`null`)return`null`;if(n===`expression`)return typeof t==`string`&&t.toLowerCase().trim()===`null`?`null`:`\`${t}\``;if(Ro(n)){let e=wp(t);return e===null?`\`${t}\``:e?`true`:`false`}if(Lo(n)){let e=bp(t);return e===null?`\`${t}\``:String(e)}if(Vo(n)){let e=Ap(t);return e===null?`\`${t}\``:`'${V_(e)}'`}let r=Ep(t);return r===null?`\`${t}\``:`'${V_(r)}'`}function U_(e){return(e.match(/"(?:[^"\\]|\\.)*"|[^."]+/g)||[]).map(e=>e.startsWith(`"`)&&e.endsWith(`"`)?B_(e.slice(1,-1)):e.trim()).filter(e=>e.length>0)}function W_(e,t){let n=new Set,r=[];for(let i of t)for(let t of e.reachableFiles(i))n.has(t.intern())||(n.add(t.intern()),r.push(t));return r}function G_(e){let r=[],i=[],a=[];e.bindProject();for(let n of e.parseProject().values()){i.push(...n.getErrors()),a.push(...n.getWarnings());let o=e.nodeSymbol(n.getValue().ast).getFiltered(t);if(!o)continue;let s=e.symbolMembers(o);i.push(...s.getErrors()),a.push(...s.getWarnings());for(let n of s.getFiltered(t)??[])if(r.push(n),n instanceof Wp&&n.isKind(I.Schema)){let o=e.symbolMembers(n);i.push(...o.getErrors()),a.push(...o.getWarnings()),r.push(...o.getFiltered(t)??[])}}return new n(r,i,a)}function K_(e){return e.includes(`
11
11
  `)?`'''\n${e}\n'''`:`'${V_(e)}'`}var q_=class{constructor(e,t){this.compiler=e,this.declarationNode=t}bind(){return this.declarationNode.body instanceof U?this.bindBody(this.declarationNode.body):[]}bindBody(e){if(!e)return[];if(e instanceof H)return this.bindFields([e]);let[t,n]=_(e.body,e=>e instanceof H);return[...this.bindFields(t),...this.bindSubElements(n)]}bindFields(e){return e.flatMap(e=>e.callee?[e.callee,...e.args].flatMap(zm).flatMap(e=>{let n=e.variables.pop();if(!n)return[];let r=this.compiler.nodeReferee(n),i=r.getFiltered(t);return i instanceof lm?[...r.getErrors(),new T(w.BINDING_ERROR,`TableGroup cannot reference imported table '${i.name}'`,n)]:r.getErrors()}):[])}bindSubElements(e){return e.flatMap(e=>e.type?this.compiler.bindNode(e).getErrors():[])}},J_={[i.Note]:{isValidBuiltinFieldValue:D,message:`'note' must be a string literal`,assignBuiltinField(e,t,n){e.note={value:t,token:n}}},[i.Color]:{isValidBuiltinFieldValue:Pf,message:`'color' must be a color literal`,assignBuiltinField(e,t){e.color=t}}},Y_=class{constructor(e,t,n){this.compiler=e,this.symbol=t,this.declarationNode=t.declaration,this.filepath=n,this.tableGroup={tables:[]}}interpret(){let e=[];return this.tableGroup.token=F(this.declarationNode),e.push(...this.interpretName(this.declarationNode.name),...this.interpretSettingList(this.declarationNode.attributeList),...this.interpretBody(this.declarationNode.body)),a_(this.compiler,this.tableGroup,this.symbol,J_,this.filepath),n.create(this.tableGroup,e)}interpretName(e){let t=[],{name:n,schemaName:r}=Pp(e);return r.length>=2&&(this.tableGroup.name=n,this.tableGroup.schemaName=r.join(`.`),t.push(new T(w.UNSUPPORTED,`Nested schema is not supported`,this.declarationNode.name))),this.tableGroup.name=n,this.tableGroup.schemaName=r[0]||null,t}interpretBody(e){let[t,n]=_(e.body,e=>e instanceof H);return[...this.interpretFields(t),...this.interpretSubElements(n)]}interpretSubElements(e){return e.flatMap(e=>{switch(e.getElementKind()){case r.Note:this.tableGroup.note={value:Np(N(e.body instanceof U?e.body.body[0].callee:e.body.callee)),token:F(e)};break;default:break}return[]})}interpretFields(e){let n=[];return this.tableGroup.tables=e.map(e=>{let r=e.callee?this.compiler.nodeReferee(e.callee).getFiltered(t):void 0;if(r?.isKind(I.Table)){let{name:e,schema:t}=r.interpretedName(this.compiler,this.filepath);return{name:e,schemaName:t}}let i=M(e.callee);return i.length>2&&n.push(new T(w.UNSUPPORTED,`Nested schema is not supported`,e)),{name:i.pop(),schemaName:i.join(`.`)||null}}),n}interpretSettingList(e){let t=E(e).getValue();this.tableGroup.color=t.color?.length?Fp(t.color?.at(0)?.value):void 0;let[n]=t.note||[];return this.tableGroup.note=n&&{value:Np(N(n?.value)),token:F(n)},this.tableGroup.metadata=Rp(t,Object.keys(J_)),[]}},X_={getDuplicateError(e,t,n){return new T(w.DUPLICATE_NAME,`TableGroup '${e}' already exists in schema '${t}'`,n)},getFieldDuplicateError(e,t){return new T(w.DUPLICATE_NAME,`Duplicate TableGroupField '${e}'`,t)}},Z_={nodeSymbol(i,a){let o=i.nodeFullname(a).getFiltered(t),s=o?.at(-1);return A(a,r.TableGroup)?new n(i.symbolFactory.create(Yp,{declaration:a,name:s},a.filepath)):j(a,r.TableGroup)?new n(i.symbolFactory.create(Xp,{declaration:a,name:o?.map(R_).join(`.`)},a.filepath)):n.create(e)},symbolMembers(r,i){if(i.isKind(I.TableGroup)){let e=i.declaration;if(!(e instanceof z))return new n([]);let a=cp(e),o=[],s=[];for(let e of a){let n=r.nodeSymbol(e),i=n.getFiltered(t);i&&(o.push(i),s.push(...n.getErrors()))}let c=new Map;for(let e of o){if(!e.isKind(I.TableGroupField)||!e.declaration)continue;let t=`${e.kind}:${e.name}`;if(e.name!==void 0){let n=e.declaration instanceof z&&e.declaration.name?e.declaration.name:e.declaration,r=c.get(t);r?(s.push(X_.getFieldDuplicateError(e.name,r)),s.push(X_.getFieldDuplicateError(e.name,n))):c.set(t,n)}}return new n(o,s)}return i.isKind(I.TableGroupField)?new n([]):n.create(e)},nodeReferee(i,a){if(!O(a)||!Yf(a,r.TableGroup)||a.parent&&Zf(a))return n.create(e);let o=i.parseFile(a.filepath).getValue().ast,s=i.nodeSymbol(o).getValue();return s===t?n.create(void 0):Q_(i,s,a)},bindNode(i,a){if(!A(a,r.TableGroup))return n.create(e);let o=new q_(i,a).bind(),s=i.nodeSymbol(a).getFiltered(t);return s&&o.push(...i.symbolMembers(s).getErrors()),n.create(void 0,o)},interpretSymbol(t,i,a){let o=i.declaration;return!A(o,r.TableGroup)||!(i instanceof Yp)?n.create(e):Rm(t,o)?new Y_(t,i,a).interpret():n.create(void 0)}};function Q_(e,r,i){let a=mp(i)??``;if(!k(i.parentNode)){let o=e.symbolMembers(r).getFiltered(t);if(o){let t=o.find(e=>e instanceof Wp&&e.isPublicSchema());if(t){let r=e.lookupMembers(t,I.Table,a);if(r)return n.create(r)}}let s=e.lookupMembers(r,I.Table,a);return s?n.create(s):new n(void 0,[new T(w.BINDING_ERROR,`Table '${a}' does not exist in Schema 'public'`,i)])}let o=Bm(e,i);if(o){if(o.isKind(I.Schema)){let t=e.lookupMembers(o,[I.Table,I.Schema],a);return t?n.create(t):new n(void 0,[new T(w.BINDING_ERROR,`Table or schema '${a}' does not exist`,i)])}return new n(void 0)}let s=e.lookupMembers(r,I.Schema,a);return s?n.create(s):new n(void 0,[new T(w.BINDING_ERROR,`Schema '${a}' does not exist`,i)])}var $_=class{constructor(e,t){this.compiler=e,this.declarationNode=t}bind(){return this.declarationNode.body instanceof U?this.bindBody(this.declarationNode.body):[]}bindBody(e){if(!e)return[];if(e instanceof H)return this.bindFields([e]);let[t,n]=_(e.body,e=>e instanceof H);return[...this.bindFields(t),...this.bindSubElements(n)]}bindFields(e){return e.flatMap(e=>{if(!e.callee)return[];let t=[],n=[e.callee,...e.args];if(g(n)instanceof W){let e=E(g(n)).getValue();t.push(...e.ref?.flatMap(e=>e.value?this.bindInlineRef(e.value):[])||[]),n.pop()}return n[1]&&this.tryToBindColumnType(n[1]),t})}tryToBindColumnType(e){let t=up(e);if(!t)return[];let n=t.variables.pop();return n?this.compiler.nodeReferee(n).getErrors():[]}bindInlineRef(e){return zm(e).flatMap(e=>{let t=e.variables.pop(),n=e.variables.pop();if(!t)return[];if(n){let r=[],i=e.variables;for(let e of i)r.push(...this.compiler.nodeReferee(e).getErrors());let a=this.compiler.nodeReferee(n);return r.push(...a.getErrors()),a.getValue()&&r.push(...this.compiler.nodeReferee(t).getErrors()),r}return this.compiler.nodeReferee(t).getErrors()})}bindSubElements(e){return e.flatMap(e=>e.type?this.compiler.bindNode(e).getErrors():[])}},ev=class{constructor(e,t,n){this.compiler=e,this.declarationNode=t.declaration,this.symbol=t,this.filepath=n,this.tablePartial={name:void 0,fields:[],token:void 0,indexes:[],checks:[]},this.pkColumns=[]}interpret(){this.tablePartial.token=this.symbol?.token??F(this.declarationNode),this.symbol?this.tablePartial.name=this.symbol.interpretedName(this.compiler,this.filepath).name:this.interpretName(this.declarationNode.name);let e=[...this.interpretSettingList(this.declarationNode.attributeList),...this.interpretBody(this.declarationNode.body)];return this.pkColumns.length>=2&&(this.tablePartial.indexes.push({columns:this.pkColumns.map(({name:e,token:t})=>({value:e,type:`column`,token:t})),pk:!0,token:{start:{offset:-1,line:-1,column:-1},end:{offset:-1,line:-1,column:-1},filepath:this.declarationNode.filepath}}),this.pkColumns.forEach(e=>{e.pk=!1})),new n(this.tablePartial,e)}interpretName(e){let{name:t}=Pp(e);return this.tablePartial.name=t,[]}interpretSettingList(e){let t=E(e).getValue(),n=va(t[i.HeaderColor]);this.tablePartial.headerColor=n?Fp(n.value):void 0;let[r]=t[i.Note]||[];return this.tablePartial.note=r&&{value:Np(N(r?.value)),token:F(r)},[]}interpretBody(e){let[t,n]=_(e.body,e=>e instanceof H);return[...this.interpretFields(t),...this.interpretSubElements(n)]}interpretSubElements(e){return e.flatMap(e=>{switch(e.getElementKind()){case r.Note:return this.tablePartial.note={value:Np(N(e.body instanceof U?e.body.body[0].callee:e.body.callee)),token:F(e)},[];case r.Indexes:return this.interpretIndexes(e);case r.Checks:return this.interpretChecks(e);default:return[]}})}interpretFields(e){return e.flatMap(e=>this.interpretColumn(e))}interpretColumn(e){let n=[],r={},a=this.compiler.nodeSymbol(e).getFiltered(t);r.name=mp(e.callee)??``,r.token=F(e);let o=Lp(this.compiler,e.args[0]);r.type=o.getValue(),n.push(...o.getErrors()),r.pk=a?.pk(this.compiler)||void 0,r.unique=a?.isUniqueSet(this.compiler)||void 0,r.increment=a?.isIncrementSet(this.compiler)||void 0,r.not_null=a?.isNotNullSet(this.compiler),r.dbdefault=a?.default(this.compiler),r.note=a?.note(this.compiler);let s=this.compiler.nodeSettings(e).getFiltered(t)??{};r.metadata=Rp(s,D_);let c=this.compiler.parseFile(this.filepath).getValue().ast,l=this.compiler.nodeSymbol(c).getFiltered(t);return r.inline_refs=(s[i.Ref]||[]).flatMap(e=>{let n=this.compiler.nodeMetadata(e).getFiltered(t);if(!(n instanceof Jm))return[];let r=n.owners(this.compiler);if(l&&r.length>0&&!r.some(e=>e===l))return[];let i=n.op(this.compiler);if(!i)return[];let a=n.rightTable(this.compiler),o=n.rightColumns(this.compiler);if(!a||o.length===0)return[];let s=a.interpretedName(this.compiler,this.filepath);return{schemaName:s.schema,tableName:s.name,fieldNames:o.map(e=>e.name??``),relation:i,token:F(n.rightToken())}}),r.inline_deps=[],r.checks=(s[i.Check]||[]).map(e=>({token:F(e),expression:e.value.value.value})),this.tablePartial.fields?.push(r),r.pk&&this.pkColumns.push(r),n}interpretIndexes(e){let n=this.compiler.nodeMetadata(e).getFiltered(t);if(!n)return[];let r=this.compiler.interpretMetadata(n,this.filepath);if(r.hasValue(t))return[];let i=r.getValue();return Array.isArray(i)?this.tablePartial.indexes?.push(...i):i&&this.tablePartial.indexes?.push(i),r.getErrors()}interpretChecks(e){let n=this.compiler.nodeMetadata(e).getFiltered(t);if(!n)return[];let r=this.compiler.interpretMetadata(n,this.filepath);if(r.hasValue(t))return[];let i=r.getValue();return Array.isArray(i)?this.tablePartial.checks?.push(...i):i&&this.tablePartial.checks?.push(i),r.getErrors()}},tv={getDuplicateError(e,t,n){return new T(w.DUPLICATE_NAME,`TablePartial '${e}' already exists in schema '${t}'`,n)}},nv={nodeSymbol(i,a){let o=i.nodeFullname(a).getFiltered(t)?.at(-1);return A(a,r.TablePartial)?new n(i.symbolFactory.create($p,{declaration:a,name:o},a.filepath)):j(a,r.TablePartial)?new n(i.symbolFactory.create(Jp,{declaration:a,name:o},a.filepath)):n.create(e)},symbolMembers(i,a){if(a.isKind(I.TablePartial)){let e=a.declaration;if(!(e instanceof z))return new n([]);let r=cp(e),o=[],s=[];for(let e of r){let n=i.nodeSymbol(e),r=n.getFiltered(t);r&&(o.push(r),s.push(...n.getErrors()))}let c=new Map;for(let e of o){if(!e.isKind(I.Column)||!e.declaration)continue;let t=e.name;if(t!==void 0){let n=e.declaration instanceof z&&e.declaration.name?e.declaration.name:e.declaration,r=c.get(t);r?(s.push(A_.getColumnDuplicateError(t,r)),s.push(A_.getColumnDuplicateError(t,n))):c.set(t,n)}}return new n(o,s)}return a.isKind(I.Column)&&A(a.declaration?.parent,r.TablePartial)?new n([]):n.create(e)},nodeReferee(a,o){if(!O(o)&&!k(o)||!Yf(o,r.TablePartial))return n.create(e);let s=a.parseFile(o.filepath).getValue().ast,c=a.nodeSymbol(s).getValue();return c===t?n.create(void 0):Xf(o,1)?rv(a,c,o):Qf(o,i.Ref)?iv(a,c,o):av(a,c,o)},bindNode(i,a){if(!A(a,r.TablePartial))return n.create(e);let o=new $_(i,a).bind(),s=i.nodeSymbol(a).getFiltered(t);return s&&o.push(...i.symbolMembers(s).getErrors()),n.create(void 0,o)},interpretSymbol(t,r,i){return r instanceof $p?r.declaration instanceof z?new ev(t,r,i).interpret():n.create(void 0):n.create(e)}};function rv(e,t,r){if(!O(r))return new n(void 0);let i=mp(r)??``;if(!k(r.parentNode))return n.create(e.lookupMembers(t,I.Enum,i));let a=Bm(e,r);if(a){if(a.isKind(I.Schema)){let t=e.lookupMembers(a,[I.Enum,I.Schema],i);return t?n.create(t):new n(void 0,[new T(w.BINDING_ERROR,`Enum or schema '${i}' does not exist`,r)])}return new n(void 0)}if(r.parentNode.leftExpression===r){let a=e.lookupMembers(t,I.Schema,i);return a?n.create(a):new n(void 0,[new T(w.BINDING_ERROR,`Schema '${i}' does not exist in Schema 'public'`,r)])}return new n(void 0)}function iv(e,i,a){if(!O(a))return new n(void 0);let o=mp(a)??``;if(!k(a.parentNode)){let s=a.parent;if(s instanceof z&&s.isKind(r.TablePartial)){let r=e.nodeSymbol(s).getFiltered(t);if(r){let t=e.lookupMembers(r,I.Column,o);return t?n.create(t):new n(void 0,[new T(w.BINDING_ERROR,`Column '${o}' does not exist in TablePartial '${r.name}'`,a)])}}let c=e.lookupMembers(i,I.Column,o);return c?n.create(c):new n(void 0,[new T(w.BINDING_ERROR,`Column '${o}' does not exist`,a)])}let s=Bm(e,a);if(s){if(s.isKind(I.Schema)){let t=e.lookupMembers(s,[I.Table,I.Schema],o);return t?n.create(t):new n(void 0,[new T(w.BINDING_ERROR,`Table or schema '${o}' does not exist`,a)])}if(s.isKind(I.Table)){let t=e.lookupMembers(s,I.Column,o);return t?n.create(t):new n(void 0,[new T(w.BINDING_ERROR,`Column '${o}' does not exist in Table 'public.${s.name}'`,a)])}return new n(void 0)}let c=a.parentNode;if(c.leftExpression===a){if(k(c.parentNode)&&c.parentNode.leftExpression===c){let t=e.lookupMembers(i,I.Schema,o);return t?n.create(t):new n(void 0,[new T(w.BINDING_ERROR,`Schema '${o}' does not exist in Schema 'public'`,a)])}let t=e.lookupMembers(i,I.Table,o);return t?n.create(t):new n(void 0,[new T(w.BINDING_ERROR,`Table '${o}' does not exist in Schema 'public'`,a)])}return new n(void 0)}function av(e,t,r){if(!O(r))return new n(void 0);let i=mp(r)??``;if(!k(r.parentNode))return So.includes(i.toLowerCase())?new n(void 0):n.create(e.lookupMembers(t,I.Enum,i));let a=Bm(e,r);if(a){if(a.isKind(I.Schema)){let t=e.lookupMembers(a,[I.Enum,I.Schema],i);return t?n.create(t):new n(void 0,[new T(w.BINDING_ERROR,`Enum or schema '${i}' does not exist`,r)])}if(a.isKind(I.Enum)){let t=e.lookupMembers(a,I.EnumField,i);return t?n.create(t):new n(void 0,[new T(w.BINDING_ERROR,`Enum field '${i}' does not exist in Enum 'public.${a.name}'`,r)])}return new n(void 0)}let o=r.parentNode;if(o.leftExpression===r){if(k(o.parentNode)&&o.parentNode.leftExpression===o){let a=e.lookupMembers(t,I.Schema,i);return a?n.create(a):new n(void 0,[new T(w.BINDING_ERROR,`Schema '${i}' does not exist in Schema 'public'`,r)])}let a=e.lookupMembers(t,I.Enum,i);return a?n.create(a):new n(void 0,[new T(w.BINDING_ERROR,`Enum '${i}' does not exist in Schema 'public'`,r)])}return new n(void 0)}var ov={symbolMembers(r,i){if(!i.isKind(I.Schema)||!(i instanceof Wp))return n.create(e);let a=i.qualifiedName,o=r.usableMembers(i).getFiltered(t);if(!o)return n.create([]);let s=[...o.nonSchemaMembers],c=new Map(o.schemaMembers.map(e=>[e.name,e])),l=[];for(let e of o.reuses.selective){let n=cv(r,i,e,c);l.push(...n.getErrors());let a=n.getFiltered(t);a&&s.push(a),s.push(...lv(r,i,e))}for(let{importPath:e,node:t}of o.reuses.wildcard)s.push(...dv(r,i,e,t,c));for(let e of o.uses.selective){let n=cv(r,i,e,c);l.push(...n.getErrors());let a=n.getFiltered(t);a&&s.push(a),s.push(...lv(r,i,e))}for(let{importPath:e,node:t}of o.uses.wildcard)s.push(...dv(r,i,e,t,c));s.push(...c.values());let u=[...s];{let e=s.filter(e=>e.isKind(I.TableGroup));if(!i.isPublicSchema()){let n=r.usableMembers(i.filepath).getFiltered(t)?.schemaMembers.find(e=>e.isPublicSchema());if(n){let i=r.symbolMembers(n).getFiltered(t);i&&e.push(...i.filter(e=>e.isKind(I.TableGroup)))}}for(let t of e)u.push(...pv(r,i,t))}let d=new Set,f=u.filter(e=>{if(e instanceof lm&&e.isKind(I.Schema))return!1;let t=`${e.originalSymbol.intern()}:${e.name??``}`;return d.has(t)?!1:(d.add(t),!0)}),p=new Map;for(let e of f){let t=`${e.kind}:${e.name}`;if(p.get(t)){if(e.isKind(I.Schema))continue;let t=e instanceof lm?e.useSpecifierDeclaration??e.declaration:e.declaration instanceof z&&e.declaration.name?e.declaration.name:e.declaration;t&&e.name!==void 0&&l.push(sv(e.kind,e.name,a.join(`.`),t))}else p.set(t,e)}return new n(f,l)}};function sv(e,t,n,r){switch(e){case I.Table:return A_.getDuplicateError(t,n,r);case I.Enum:return Sh.getDuplicateError(t,n,r);case I.TablePartial:return tv.getDuplicateError(t,n,r);case I.TableGroup:return X_.getDuplicateError(t,n,r);case I.DiagramView:return mh.getDuplicateError(t,n,r);case I.Ref:return u_.getDuplicateError(t,n,r);default:return new T(w.DUPLICATE_NAME,`Duplicate ${e} '${t}' in schema '${n}'`,r)}}function cv(e,r,i,a){let o=__(e,r,i);if(o.kind===`none`)return n.create(void 0);if(o.kind===`direct`){let r=e.nodeSymbol(i);return r.hasValue(t)?n.create(void 0):r}return a.has(o.schemaName)||a.set(o.schemaName,e.symbolFactory.create(Wp,{name:o.schemaName,parent:r},r.filepath)),n.create(void 0)}function lv(e,n,r,i=new Set){if(r.getSymbolKind()!==I.Schema||!r.name)return[];let a=e.nodeReferee(r.name).getFiltered(t);if(!a||!a.isKind(I.Schema))return[];let o=p_.visibleName(e,r),s=n.qualifiedName;return!o||o.length!==s.length||!o.every((e,t)=>e===s[t])?[]:uv(e,n,r,a,i)}function uv(e,n,r,i,a){let o=i.intern();if(a.has(o))return[];a.add(o);let s=e.usableMembers(i).getFiltered(t);if(!s)return[];let c=s.nonSchemaMembers.filter(e=>e.canBeImported).map(t=>e.symbolFactory.create(lm,{kind:t.kind,declaration:t.originalSymbol.declaration,usedSymbol:t.originalSymbol,useSpecifierDeclaration:r,name:t.name},n.filepath));for(let t of[...c])t.isKind(I.TableGroup)&&c.push(...pv(e,n,t));for(let i of s.reuses.selective)if(c.push(...lv(e,n,i,a)),__(e,n,i).kind===`direct`){let a=e.nodeSymbol(i).getFiltered(t);a&&a.canBeImported&&c.push(e.symbolFactory.create(lm,{kind:a.kind,declaration:a.originalSymbol.declaration,usedSymbol:a.originalSymbol,useSpecifierDeclaration:r,name:a.name},n.filepath))}for(let{importPath:t}of s.reuses.wildcard){let i=fv(e,t,n.qualifiedName);i&&c.push(...uv(e,n,r,i,a))}return c}function dv(e,n,r,i,a,o=new Set){if(o.has(r))return[];o.add(r);let s=fv(e,r,n.qualifiedName);if(!s)return[];let c=e.usableMembers(s).getFiltered(t);if(!c)return[];let l=c.nonSchemaMembers.filter(e=>e.canBeImported).map(t=>e.symbolFactory.create(lm,{kind:t.kind,declaration:t.declaration,usedSymbol:t,useSpecifierDeclaration:i,name:t.name},n.filepath));for(let t of c.schemaMembers)a.has(t.name)||a.set(t.name,e.symbolFactory.create(Wp,{name:t.name},n.filepath));let{reuses:{selective:u,wildcard:d}}=c;for(let r of u){let i=cv(e,n,r,a).getFiltered(t);i&&l.push(i),l.push(...lv(e,n,r))}for(let{importPath:t}of d)l.push(...dv(e,n,t,i,a,o));return l}function fv(e,n,r){if(r.length===0)return;let i=e.usableMembers(n).getFiltered(t);if(!i)return;let{schemaMembers:a}=i,o,s=[...r];for(;s.length>0;){let t=s.shift();if(o=a.find(e=>e.name===t),!o)return;let n=e.usableMembers(o).getValue();({schemaMembers:a}=n)}return o}function pv(e,n,r){if(!r.isKind(I.TableGroup)||!(r instanceof lm))return[];let i=r.originalSymbol,a=e.symbolMembers(i).getFiltered(t);if(!a)return[];let o=n.qualifiedName,s=[];for(let t of a){if(!t.isKind(I.TableGroupField)||!t.declaration)continue;let n=t.declaration.callee;if(!n)continue;let i=M(n);if(!i||i.length===0)continue;let a=i.length<=1?[y]:i.slice(0,-1);if(a.length!==o.length||!o.every((e,t)=>e===a[t]))continue;let c=t.declaration.filepath,l=mv(e,c,i);l&&s.push(e.symbolFactory.create(lm,{kind:I.Table,declaration:l.declaration,usedSymbol:l,useSpecifierDeclaration:void 0,name:l.name},r.filepath))}return s}function mv(e,n,r){if(r.length===1){let i=e.usableMembers(n).getFiltered(t);return i?i.nonSchemaMembers.find(e=>e.isKind(I.Table)&&e.name===r[0]):void 0}let i=r[0],a=r[r.length-1],o=e.usableMembers(n).getFiltered(t);if(!o)return;let s=o.schemaMembers.find(e=>e.name===i);if(!s)return;let c=e.usableMembers(s).getFiltered(t);if(c)return c.nonSchemaMembers.find(e=>e.isKind(I.Table)&&e.name===a)}var hv=class{constructor(e,t){this.compiler=e,this.declarationNode=t}bind(){return this.bindBody(this.declarationNode.body)}bindBody(e){if(!e||e instanceof H)return[];let t=e.body.filter(e=>e instanceof z);return this.bindSubElements(t)}bindSubElements(e){return e.flatMap(e=>e.type?this.compiler.bindNode(e).getErrors():[])}},gv={[i.Color]:{isValidBuiltinFieldValue:Ff,message:`'color' must be a color literal or 'none'`,assignBuiltinField(e,t){e.color=t}}},_v=class{constructor(e,t,n){this.compiler=e,this.symbol=t,this.declarationNode=t.declaration,this.filepath=n,this.note={name:void 0,content:void 0,token:void 0}}interpret(){this.note.token=F(this.declarationNode);let e=[...this.interpretName(),...this.interpretSettingList(this.declarationNode.attributeList),...this.interpretBody(this.declarationNode.body)];return a_(this.compiler,this.note,this.symbol,gv,this.filepath),n.create(this.note,e)}interpretName(){let{name:e}=this.symbol.interpretedName(this.compiler,this.filepath);return this.note.name=e,[]}interpretSettingList(e){let t=E(e).getValue();return t.color?.length&&(this.note.color=Fp(t.color.at(0)?.value)),this.note.metadata=Rp(t,Object.keys(gv)),[]}interpretBody(e){let[t,n]=_(e.body,e=>e instanceof H);return t.length!==1||n.length>0?[new T(w.INVALID_NOTE,`Invalid note syntax`,e)]:[...this.interpretNote(t[0])]}interpretNote(e){let t=N(e.callee)??``;return this.note.content=Np(t),[]}},vv={nodeSymbol(i,a){return!A(a,r.Note)||!(a.parentNode instanceof R)?n.create(e):new n(i.symbolFactory.create(Zp,{declaration:a,name:i.nodeFullname(a).getFiltered(t)?.at(-1)},a.filepath))},symbolMembers(t,r){return r.isKind(I.StickyNote)?new n([]):n.create(e)},bindNode(t,i){return A(i,r.Note)?n.create(void 0,new hv(t,i).bind()):n.create(e)},interpretSymbol(t,r,i){return r instanceof Zp?r.declaration instanceof z?new _v(t,r,i).interpret():n.create(void 0):n.create(e)}};function yv(e){let t=e.targetKind?.value?.toLowerCase();return Object.values(Vp).find(e=>e.toLowerCase()===t)}function bv(e,t,n){let r=[];return t.length>1&&r.push(...t.map(t=>new T(n.duplicate,`'${e}' can only appear once`,t))),t.forEach(t=>{Hf(t.value)||r.push(new T(n.invalidValue,`Custom setting '${e}' must be a string or a color literal`,t.value||t.name||t))}),r}var xv=class{constructor(e,t){this.compiler=e,this.declarationNode=t}bind(){return[...this.bindTargetElement(this.declarationNode.name),...this.bindBody(this.declarationNode.body)]}bindTargetElement(e){let n=yv(this.declarationNode),r=M(e);if(!r?.length||!n)return[];let i=this.compiler.nodeMetadata(this.declarationNode).getFiltered(t);if(!(i instanceof Qm))return[];let a=this.compiler.metadataTargets(i);return a.length>1?[new T(w.BINDING_ERROR,`Ref '${r.join(`.`)}' is ambiguous: it has multiple definitions`,e??this.declarationNode)]:a.length===0?[new T(w.BINDING_ERROR,`Cannot find metadata target element: \`${n} ${r.join(`.`)}\``,e??this.declarationNode)]:[]}bindBody(e){return e instanceof U?e.body.filter(e=>e instanceof z).flatMap(e=>e.type?this.compiler.bindNode(e).getErrors():[]):[]}},Sv=class{constructor(e,t,n){this.compiler=e,this.declarationNode=t.declaration,this.filepath=n,this.values={}}interpret(){let e=this.interpretValues(this.declarationNode.body);return new n(this.values,e)}interpretValues(e){if(!(e instanceof U))return[];for(let t of e.body){if(!(t instanceof z))continue;let e=t.type?.value;if(!e)continue;let n=zp(t.body instanceof H?t.body.callee:void 0);n&&(this.values[e]={value:e===`note`?Np(n):n,token:F(t)})}return[]}},Cv=[m_,j_,Ch,Xg,Eh,ah,d_,lh,Ug,Z_,nv,vv,{nodeMetadata(t,i){return A(i,r.Metadata)?new n(new Qm(i)):new n(e)},nodeReferee(i,a){if(!O(a))return new n(e);let o=a.parentOfKind(z);if(!o||!A(o,r.Metadata)||!o.name?.containsEq(a))return new n(e);if(!M(o.name)||!yv(o))return new n(void 0);let s=i.nodeMetadata(o).getFiltered(t);if(!(s instanceof Qm))return new n(void 0);let c=i.metadataTargets(s);return new n(c.length===1?c[0]:void 0)},bindNode(t,i){return A(i,r.Metadata)?new n(void 0,new xv(t,i).bind()):new n(e)},interpretMetadata(t,i,a){return i instanceof Qm?A(i.declaration,r.Metadata)?new Sv(t,i,a).interpret():new n(void 0):new n(e)}},ov,hh,Bg];function wv(t,...r){for(let i of Cv){let a=i[t];if(a){let t=a(...r);if(t instanceof n&&!t.hasValue(e))return t}}return n.create(e)}function Tv(r){let i=wv(`nodeSymbol`,this,r);return i.hasValue(e)?n.create(t):i}function Ev(r){let i=wv(`symbolMembers`,this,r);return i.hasValue(e)?n.create(t):i}function Dv(r){let i=wv(`nodeReferee`,this,r);return i.hasValue(e)?n.create(t):i}function Ov(r){let i=wv(`bindNode`,this,r);return i.hasValue(e)?n.create(t):i}function kv(r){let i=wv(`nodeMetadata`,this,r);return i.hasValue(e)?n.create(t):i}function Av(r,i){let a=wv(`interpretSymbol`,this,r,i);return a.hasValue(e)?n.create(t):a}function jv(r,i){let a=wv(`interpretMetadata`,this,r,i);return a.hasValue(e)?n.create(t):a}var Mv=class{constructor(e,t){this.compiler=e,this.declarationNode=t}validate(){return[...this.validateContext(),...this.validateName(this.declarationNode.name),...this.validateAlias(this.declarationNode.alias),...this.validateSettingList(this.declarationNode.attributeList),...this.validateBody(this.declarationNode.body)]}validateContext(){let e=new T(w.INVALID_CHECKS_CONTEXT,`A Checks can only appear inside a Table or a TablePartial`,this.declarationNode);return this.declarationNode.parent instanceof R?[e]:this.declarationNode.parent?.isKind(r.Table,r.TablePartial)?[]:[e]}validateName(e){return e?[new T(w.UNEXPECTED_NAME,`A Checks shouldn't have a name`,e)]:[]}validateAlias(e){return e?[new T(w.UNEXPECTED_ALIAS,`A Checks shouldn't have an alias`,e)]:[]}validateSettingList(e){return e?[new T(w.UNEXPECTED_SETTINGS,`A Checks shouldn't have a setting list`,e)]:[]}validateBody(e){if(!e)return[];if(e instanceof H)return[new T(w.UNEXPECTED_SIMPLE_BODY,`A Checks must have a complex body`,e)];let[t,n]=_(e.body,e=>e instanceof H);return[...this.validateFields(t),...this.validateSubElements(n)]}validateFields(e){return e.flatMap(e=>{if(!e.callee)return[];let t=[],n=[e.callee,...e.args];return g(n)instanceof W&&t.push(...this.validateFieldSetting(n.pop())),(n.length>1||!(n[0]instanceof ym))&&t.push(new T(w.INVALID_CHECKS_FIELD,`A check field must be a function expression`,e)),t})}validateFieldSetting(e){let t=E(e),n=t.getErrors(),r=t.getValue();for(let[e,t]of Object.entries(r))switch(e){case`name`:t.length>1&&t.forEach(t=>n.push(new T(w.DUPLICATE_CHECK_SETTING,`'${e}' can only appear once`,t))),t.forEach(t=>{D(t.value)||n.push(new T(w.INVALID_CHECK_SETTING_VALUE,`'${e}' must be a string`,t))});break;default:t.forEach(t=>n.push(new T(w.UNKNOWN_CHECK_SETTING,`Unknown check setting '${e}'`,t)))}return n}validateSubElements(e){return e.flatMap(e=>e.type?this.compiler.validateNode(e).getErrors():[])}},Nv={validateNode(t,i){if(!A(i,r.Checks))return n.create(e);let a=new Mv(t,i);return n.create(void 0,a.validate())},nodeFullname(t,i){return A(i,r.Checks)?i.name?new n(void 0,[new T(w.UNEXPECTED_NAME,`A Checks shouldn't have a name`,i.name)]):new n(void 0):n.create(e)},nodeAlias(t,i){return A(i,r.Checks)?i.alias?new n(void 0,[new T(w.UNEXPECTED_ALIAS,`A Checks shouldn't have an alias`,i.alias)]):new n(void 0):n.create(e)},nodeSettings(t,i){return A(i,r.Checks)?i.attributeList?new n({},[new T(w.UNEXPECTED_SETTINGS,`A Checks shouldn't have a setting list`,i.attributeList)]):new n({}):n.create(e)}},Pv=class{constructor(e,t){this.declarationNode=t,this.compiler=e}validate(){return[...this.validateContext(),...this.validateName(this.declarationNode.name),...this.validateAlias(this.declarationNode.alias),...this.validateSettingList(this.declarationNode.attributeList),...this.validateBody(this.declarationNode.body)]}validateContext(){return this.declarationNode.parent instanceof z&&this.declarationNode.parent.isKind(r.Project,r.Dep)?[]:[new T(w.INVALID_CUSTOM_CONTEXT,`A Custom element can only appear in a Project or a Dep`,this.declarationNode)]}validateName(e){return e?[new T(w.UNEXPECTED_NAME,`A Custom element shouldn't have a name`,e)]:[]}validateAlias(e){return e?[new T(w.UNEXPECTED_NAME,`A Custom element shouldn't have an alias`,e)]:[]}validateSettingList(e){return e?[new T(w.UNEXPECTED_SETTINGS,`A Custom element shouldn't have a setting list`,e)]:[]}validateBody(e){if(!e)return[];if(e instanceof U)return[new T(w.UNEXPECTED_COMPLEX_BODY,`A Custom element can only have an inline field`,e)];let t=[];return!(this.declarationNode.parent instanceof z&&this.declarationNode.parent.isKind(r.Dep))&&!D(e.callee)&&t.push(new T(w.INVALID_CUSTOM_ELEMENT_VALUE,`A Custom element value can only be a string`,e)),e.args.length>0&&t.push(...e.args.map(e=>new T(w.INVALID_CUSTOM_ELEMENT_VALUE,`A Custom element value can only be a string`,e))),t}};function Fv(e){return e instanceof z&&!!e.type?.value}var Iv={validateNode(t,r){if(!Fv(r))return n.create(e);let i=new Pv(t,r);return n.create(void 0,i.validate())},nodeFullname(t,r){return Fv(r)?r.name?new n(void 0,[new T(w.UNEXPECTED_NAME,`A custom field shouldn't have a name`,r.name)]):new n(void 0):n.create(e)},nodeAlias(t,r){return Fv(r)?r.alias?new n(void 0,[new T(w.UNEXPECTED_NAME,`A custom field shouldn't have an alias`,r.alias)]):new n(void 0):n.create(e)},nodeSettings(t,r){return Fv(r)?r.attributeList?new n({},[new T(w.UNEXPECTED_SETTINGS,`A custom field shouldn't have a setting list`,r.attributeList)]):new n({}):n.create(e)}},Lv=class{constructor(e,t){this.compiler=e,this.declarationNode=t}validate(){return[...this.validateContext(),...this.validateName(this.declarationNode.name),...this.validateAlias(this.declarationNode.alias),...this.validateSettings(),...this.validateBody(this.declarationNode.body)]}validateSettings(){let e=[];this.declarationNode.attributeList&&e.push(...Rv(this.declarationNode.attributeList).getErrors());let t=this.declarationNode.body;if(t instanceof H){let n=t.args.find(e=>e instanceof W);n&&e.push(...Rv(n).getErrors())}return e}validateContext(){return this.declarationNode.parent instanceof R?[]:[new T(w.INVALID_DEP_CONTEXT,`A Dep must appear top-level`,this.declarationNode)]}validateName(e){return e?e instanceof Tm?[new T(w.INVALID_NAME,`Wildcard (*) is not allowed as a Dep name`,e)]:kf(e)?[]:[new T(w.INVALID_NAME,`A Dep's name is optional or must be an identifier or a quoted identifier`,e)]:[]}validateAlias(e){return e?[new T(w.UNEXPECTED_ALIAS,`A Dep shouldn't have an alias`,e)]:[]}validateBody(e){if(!e)return[];if(e instanceof H)return this.validateFields([e]);let[t,n]=_(e.body,e=>e instanceof H);return[...this.validateFields(t),...this.validateSubElements(n)]}validateFields(e){let t=[];return e.forEach(e=>{if(!e.callee){t.push(new T(w.INVALID_DEP_FIELD,`A Dep field must be a binary relationship`,e));return}if(!(e.callee instanceof _m)){t.push(new T(w.INVALID_DEP_FIELD,`A Dep field must be a binary relationship`,e.callee));return}let n=e.callee;n.op?.value!==`->`&&n.op?.value!==`<-`&&t.push(new T(w.INVALID_DEP_FIELD,`Dep edges must use the '->' or '<-' operator`,e.callee));let r=up(n.leftExpression)??{variables:[],tupleElements:[]},i=up(n.rightExpression)??{variables:[],tupleElements:[]};r.variables.length===0&&r.tupleElements.length===0&&t.push(new T(w.INVALID_DEP_FIELD,`Invalid Dep endpoint`,n.leftExpression||e.callee)),i.variables.length===0&&i.tupleElements.length===0&&t.push(new T(w.INVALID_DEP_FIELD,`Invalid Dep endpoint`,n.rightExpression||e.callee));let a=[...e.args];g(a)instanceof W?a.pop():a[0]instanceof W&&a.shift(),a.length>0&&t.push(...a.map(e=>new T(w.INVALID_DEP_FIELD,`A Dep field should only have a single binary relationship`,e)))}),t}validateSubElements(e){return e.flatMap(e=>{if(!e.type)return[];let t=e.type.value?.toLowerCase(),n=e.body;if(!t)return[];switch(t){case i.Color:return e.name||!(n instanceof H)||!n.callee||!Pf(n.callee)?[new T(w.INVALID_SETTINGS,`Invalid color value. Expected a hex color (e.g. #fff or #aabbcc)`,e)]:[];case i.Note:{let t;return n instanceof H?t=n.callee:n instanceof U&&n.body[0]instanceof H&&(t=n.body[0].callee),!t||!D(t)?[new T(w.INVALID_SETTINGS,`Invalid note value. Expected a quoted string`,e)]:[]}default:return e.name||n instanceof U?[new T(w.INVALID_SETTINGS,`'${t}' must be a simple field (e.g. ${t}: value). Block syntax is not allowed`,e)]:!(n instanceof H)||!n.callee?[]:!D(n.callee)&&!Pf(n.callee)&&!Lf(n.callee)&&!kf(n.callee)?[new T(w.INVALID_SETTINGS,`Invalid value for '${t}'. Expected a string, number, color, or identifier`,e)]:[]}})}};function Rv(e){let t=E(e),r=[...t.getErrors()],a=t.getValue();for(let[e,t]of Object.entries(a))for(let n of t)switch(e){case i.Color:Pf(n.value)||r.push(new T(w.INVALID_SETTINGS,`Invalid color value. Expected a hex color (e.g. #fff or #aabbcc)`,n));break;case i.Note:D(n.value)||r.push(new T(w.INVALID_SETTINGS,`Invalid note value. Expected a quoted string`,n));break;default:n.value&&!D(n.value)&&!Pf(n.value)&&!Lf(n.value)&&!kf(n.value)&&r.push(new T(w.INVALID_SETTINGS,`Invalid value for setting '${e}'. Expected a string, number, color, or identifier`,n));break}return new n(a,r)}var zv={validateNode(t,i){return A(i,r.Dep)?n.create(void 0,new Lv(t,i).validate()):n.create(e)},nodeFullname(t,i){return A(i,r.Dep)?i.name?kf(i.name)?new n(M(i.name)):new n(void 0,[new T(w.INVALID_NAME,`A Dep's name is optional or must be an identifier or a quoted identifier`,i.name)]):new n(void 0):j(i,r.Dep)?new n(void 0):n.create(e)},nodeAlias(t,i){return A(i,r.Dep)?i.alias?new n(void 0,[new T(w.UNEXPECTED_ALIAS,`A Dep shouldn't have an alias`,i.alias)]):new n(void 0):j(i,r.Dep)?new n(void 0):n.create(e)},nodeSettings(t,i){if(A(i,r.Dep))return i.attributeList?Rv(i.attributeList):new n({});if(j(i,r.Dep)){let e=[...i.args],t;return g(e)instanceof W?t=g(e):e[0]instanceof W&&(t=e[0]),t?Rv(t):new n({})}return n.create(e)}},Bv=class{constructor(e,t){this.compiler=e,this.declarationNode=t}validate(){let e=[...this.validateContext(),...this.validateName(this.declarationNode.name),...this.validateAlias(this.declarationNode.alias),...this.validateSettingList(this.declarationNode.attributeList)],t=this.validateBody(this.declarationNode.body);return e.push(...t.errors),{errors:e,warnings:t.warnings}}validateContext(){return this.declarationNode.parent instanceof z?[new T(w.INVALID_DIAGRAMVIEW_CONTEXT,`DiagramView must appear top-level`,this.declarationNode)]:[]}validateName(e){return e?kf(e)?[]:[new T(w.INVALID_NAME,`A DiagramView name must be a single identifier`,e)]:[new T(w.NAME_NOT_FOUND,`A DiagramView must have a name`,this.declarationNode)]}validateAlias(e){return e?[new T(w.UNEXPECTED_ALIAS,`A DiagramView shouldn't have an alias`,e)]:[]}validateSettingList(e){let t=E(e),n=t.getErrors(),r=t.getValue();for(let e of Object.keys(r))n.push(...r[e].map(t=>new T(w.UNEXPECTED_SETTINGS,`Unknown '${e}' setting for DiagramView`,t)));return n}validateBody(e){if(!e)return{errors:[],warnings:[]};if(e instanceof H)return{errors:[new T(w.UNEXPECTED_SIMPLE_BODY,`A DiagramView's body must be a block`,e)],warnings:[]};let[t,n]=_(e.body,e=>e instanceof H),r=this.validateSubElements(n);return{errors:[...this.validateFields(t),...r.errors],warnings:r.warnings}}validateFields(e){return e.flatMap(e=>Uf(e.callee)?e.args.length>0?e.args.map(e=>new T(w.INVALID_DIAGRAMVIEW_FIELD,`DiagramView field should only have a single name`,e)):[]:[new T(w.INVALID_DIAGRAMVIEW_FIELD,`Fields are not allowed at DiagramView level. Use Tables, Notes, TableGroups, or Schemas blocks instead.`,e)])}validateSubElements(e){let t=[],n=[],r=[`tables`,`notes`,`tablegroups`,`schemas`];for(let i of e){if(!i.type)continue;let e=i.type.value.toLowerCase();if(!r.includes(e)){t.push(new T(w.INVALID_DIAGRAMVIEW_FIELD,`Unknown block type "${i.type.value}" in DiagramView. Allowed: Tables, Notes, TableGroups, Schemas`,i));continue}let a=this.validateSubBlock(i);t.push(...a.errors),n.push(...a.warnings)}return{errors:t,warnings:n}}validateSubBlock(e){let t=[],n=[];if(Af(e))return t.push(new T(w.UNEXPECTED_SIMPLE_BODY,`${e.type?.value} body must be a block`,e.bodyColon)),{errors:t,warnings:n};if(!e.body||!(e.body instanceof U))return{errors:t,warnings:n};let r=e.body,i=r.body.some(e=>e instanceof H&&Uf(e.callee)),a=r.body.some(e=>e instanceof H&&!Uf(e.callee));return i&&a&&n.push(new xf(w.INVALID_DIAGRAMVIEW_FIELD,`Wildcard (*) combined with specific items in ${e.type?.value} block. Specific items will be ignored.`,e)),t.push(...this.validateSubBlockFields(e)),{errors:t,warnings:n}}validateSubBlockFields(e){let t=[];if(!e.body||!(e.body instanceof U))return t;let n=e.body.body.filter(e=>e instanceof H);for(let e of n)Uf(e.callee)||(e.args.length>0||!M(e.callee))&&t.push(...e.args.map(e=>new T(w.INVALID_DIAGRAMVIEW_FIELD,`DiagramView field should only have a single name`,e)));return t}},Vv={validateNode(t,i){if(!A(i,r.DiagramView))return n.create(e);let{errors:a,warnings:o}=new Bv(t,i).validate();return n.create(void 0,a,o)},nodeFullname(t,i){if(A(i,r.DiagramView)){if(!i.name)return new n(void 0,[new T(w.NAME_NOT_FOUND,`A DiagramView must have a name`,i)]);let e=M(i.name);return e?new n(e):new n(void 0)}else if(j(i,r.DiagramViewTables,r.DiagramViewNotes,r.DiagramViewSchemas,r.DiagramViewTableGroups)){let e=M(i.callee);return e?(e.length===1&&j(i,r.DiagramViewTables)&&e.unshift(y),new n(e)):new n(void 0)}return new n(e)},nodeAlias(t,i){return A(i,r.DiagramView)?new n(void 0):n.create(e)}},Hv=class{constructor(e,t){this.compiler=e,this.declarationNode=t}validate(){return[...this.validateContext(),...this.validateName(this.declarationNode.name),...this.validateAlias(this.declarationNode.alias),...this.validateSettingList(this.declarationNode.attributeList),...this.validateBody(this.declarationNode.body)]}validateContext(){return this.declarationNode.parent instanceof z?[new T(w.INVALID_PROJECT_CONTEXT,`An Enum can only appear top-level`,this.declarationNode)]:[]}validateName(e){return e?e instanceof Tm?[new T(w.INVALID_NAME,`Wildcard (*) is not allowed as an Enum name`,e)]:Df(e)?[]:[new T(w.INVALID_NAME,`An Enum name must be of the form <enum> or <schema>.<enum>`,e)]:[new T(w.NAME_NOT_FOUND,`An Enum must have a name`,this.declarationNode)]}validateAlias(e){return e?[new T(w.UNEXPECTED_ALIAS,`An Enum shouldn't have an alias`,e)]:[]}validateSettingList(e){return e?[new T(w.UNEXPECTED_SETTINGS,`An Enum shouldn't have a setting list`,e)]:[]}validateBody(e){if(!e)return[];if(e instanceof H)return this.validateFields([e]);let[t,n]=_(e.body,e=>e instanceof H);return[...this.validateFields(t),...this.validateSubElements(n)]}validateFields(e){if(e.length===0)return[new T(w.EMPTY_ENUM,`An Enum must have at least one element`,this.declarationNode)];let t=[];for(let n of e){n.callee&&!O(n.callee)&&t.push(new T(w.INVALID_ENUM_ELEMENT_NAME,`An enum field must be an identifier or a quoted identifier`,n.callee));let e=[...n.args];g(e)instanceof W?(t.push(...this.validateFieldSettings(g(e))),e.pop()):e[0]instanceof W&&(t.push(...this.validateFieldSettings(e[0])),e.shift()),e.length>0&&t.push(...e.map(e=>new T(w.INVALID_ENUM_ELEMENT,`An Enum must have only a field and optionally a setting list`,e)))}return t}validateFieldSettings(e){let t=E(e),n=t.getErrors(),r=t.getValue();for(let e in r){let t=r[e];if(t)switch(e){case i.Note:t.length>1&&t.forEach(e=>n.push(new T(w.DUPLICATE_ENUM_ELEMENT_SETTING,`'note' can only appear once`,e))),t.forEach(e=>{D(e.value)||n.push(new T(w.INVALID_ENUM_ELEMENT_SETTING,`'note' must be a string`,e))});break;default:t.forEach(t=>n.push(new T(w.UNKNOWN_ENUM_ELEMENT_SETTING,`Unknown enum field setting '${e}'`,t)))}}return n}validateSubElements(e){return e.flatMap(e=>e.type?this.compiler.validateNode(e).getErrors():[])}},Uv={validateNode(t,i){return A(i,r.Enum)?n.create(void 0,new Hv(t,i).validate()):n.create(e)},nodeFullname(t,i){if(A(i,r.Enum)){if(!i.name)return new n(void 0,[new T(w.NAME_NOT_FOUND,`An Enum must have a name`,i)]);if(!Df(i.name))return new n(void 0,[new T(w.INVALID_NAME,`An Enum name must be of the form <enum> or <schema>.<enum>`,i.name)]);let e=M(i.name);return e?.length===1&&e.unshift(y),new n(e)}if(j(i,r.Enum)){let e=P(i.callee);return new n(e===void 0?void 0:[e])}return n.create(e)},nodeAlias(t,i){return A(i,r.Enum)?i.alias?new n(void 0,[new T(w.UNEXPECTED_ALIAS,`An Enum shouldn't have an alias`,i.alias)]):new n(void 0):j(i,r.Enum)?new n(void 0):n.create(e)},nodeSettings(t,a){if(A(a,r.Enum))return a.attributeList?new n({},[new T(w.UNEXPECTED_SETTINGS,`An Enum shouldn't have a setting list`,a.attributeList)]):new n({});if(j(a,r.Enum)){let e=[...a.args],t;if(g(e)instanceof W?t=g(e):e[0]instanceof W&&(t=e[0]),!t)return new n({});let r=E(t),o=r.getErrors(),s=r.getValue(),c={};for(let[e,t]of Object.entries(s))switch(e){case i.Note:t.length>1&&t.forEach(e=>o.push(new T(w.DUPLICATE_ENUM_ELEMENT_SETTING,`'note' can only appear once`,e))),t.forEach(e=>{D(e.value)||o.push(new T(w.INVALID_ENUM_ELEMENT_SETTING,`'note' must be a string`,e))}),c[e]=t;break;default:t.forEach(t=>o.push(new T(w.UNKNOWN_ENUM_ELEMENT_SETTING,`Unknown enum field setting '${e}'`,t)))}return new n(c,o)}return n.create(e)}},Wv=class{constructor(e,t){this.compiler=e,this.declarationNode=t}validate(){return[...this.validateContext(),...this.validateName(this.declarationNode.name),...this.validateAlias(this.declarationNode.alias),...this.validateSettingList(this.declarationNode.attributeList),...this.validateBody(this.declarationNode.body)]}validateContext(){let e=new T(w.INVALID_INDEXES_CONTEXT,`An Indexes can only appear inside a Table or a TablePartial`,this.declarationNode);return this.declarationNode.parent instanceof R?[e]:this.declarationNode.parent?.isKind(r.Table,r.TablePartial)?[]:[e]}validateName(e){return e?[new T(w.UNEXPECTED_NAME,`An Indexes shouldn't have a name`,e)]:[]}validateAlias(e){return e?[new T(w.UNEXPECTED_ALIAS,`An Indexes shouldn't have an alias`,e)]:[]}validateSettingList(e){return e?[new T(w.UNEXPECTED_SETTINGS,`An Indexes shouldn't have a setting list`,e)]:[]}validateBody(e){if(!e)return[];if(e instanceof H)return[new T(w.UNEXPECTED_SIMPLE_BODY,`An Indexes must have a complex body`,e)];let[t,n]=_(e.body,e=>e instanceof H);return[...this.validateFields(t),...this.validateSubElements(n)]}validateFields(e){return e.flatMap(e=>{if(!e.callee)return[];let t=[],n=[e.callee,...e.args];return g(n)instanceof W&&t.push(...this.validateFieldSetting(n.pop())),n.forEach(e=>{for(;e instanceof Cm;)e.argumentList&&dp(e.argumentList)===void 0&&t.push(new T(w.INVALID_INDEXES_FIELD,`An index field must be an identifier, a quoted identifier, a functional expression or a tuple of such`,e.argumentList)),e=e.callee;dp(e)===void 0&&t.push(new T(w.INVALID_INDEXES_FIELD,`An index field must be an identifier, a quoted identifier, a functional expression or a tuple of such`,e))}),t})}validateFieldSetting(e){let t=E(e),n=t.getErrors(),r=t.getValue();for(let[e,t]of Object.entries(r))switch(e){case`note`:case`name`:t.length>1&&t.forEach(t=>n.push(new T(w.DUPLICATE_INDEX_SETTING,`'${e}' can only appear once`,t))),t.forEach(t=>{D(t.value)||n.push(new T(w.INVALID_INDEX_SETTING_VALUE,`'${e}' must be a string`,t))});break;case`unique`:case`pk`:t.length>1&&t.forEach(t=>n.push(new T(w.DUPLICATE_INDEX_SETTING,`'${e}' can only appear once`,t))),t.forEach(t=>{t.value!==void 0&&n.push(new T(w.INVALID_INDEX_SETTING_VALUE,`'${e}' must not have a value`,t))});break;case`type`:t.length>1&&t.forEach(e=>n.push(new T(w.DUPLICATE_INDEX_SETTING,`'type' can only appear once`,e))),t.forEach(e=>{O(e.value)||n.push(new T(w.INVALID_INDEX_SETTING_VALUE,`'type' must be "btree" or "hash"`,e))});break;default:t.forEach(t=>n.push(new T(w.UNKNOWN_INDEX_SETTING,`Unknown index setting '${e}'`,t)))}return n}validateSubElements(e){return e.flatMap(e=>e.type?this.compiler.validateNode(e).getErrors():[])}},Gv={validateNode(t,i){return A(i,r.Indexes)?n.create(void 0,new Wv(t,i).validate()):n.create(e)},nodeFullname(t,i){return A(i,r.Indexes)?i.name?new n(void 0,[new T(w.UNEXPECTED_NAME,`An Indexes shouldn't have a name`,i.name)]):new n(void 0):j(i,r.Indexes)?new n(void 0):n.create(e)},nodeAlias(t,i){return A(i,r.Indexes)?i.alias?new n(void 0,[new T(w.UNEXPECTED_ALIAS,`An Indexes shouldn't have an alias`,i.alias)]):new n(void 0):j(i,r.Indexes)?new n(void 0):n.create(e)},nodeSettings(t,a){if(A(a,r.Indexes))return a.attributeList?new n({},[new T(w.UNEXPECTED_SETTINGS,`An Indexes shouldn't have a setting list`,a.attributeList)]):new n({});if(j(a,r.Indexes)){let e=[a.callee,...a.args],t;if(g(e)instanceof W&&(t=g(e)),!t)return new n({});let r=E(t),o=r.getErrors(),s=r.getValue(),c={};for(let[e,t]of Object.entries(s))switch(e){case i.Note:case i.Name:t.length>1&&t.forEach(t=>o.push(new T(w.DUPLICATE_INDEX_SETTING,`'${e}' can only appear once`,t))),t.forEach(t=>{D(t.value)||o.push(new T(w.INVALID_INDEX_SETTING_VALUE,`'${e}' must be a string`,t))}),c[e]=t;break;case i.Unique:case i.PK:t.length>1&&t.forEach(t=>o.push(new T(w.DUPLICATE_INDEX_SETTING,`'${e}' can only appear once`,t))),t.forEach(t=>{t.value!==void 0&&o.push(new T(w.INVALID_INDEX_SETTING_VALUE,`'${e}' must not have a value`,t))}),c[e]=t;break;case i.Type:t.length>1&&t.forEach(e=>o.push(new T(w.DUPLICATE_INDEX_SETTING,`'type' can only appear once`,e))),t.forEach(e=>{O(e.value)||o.push(new T(w.INVALID_INDEX_SETTING_VALUE,`'type' must be "btree" or "hash"`,e))}),c[e]=t;break;default:t.forEach(t=>o.push(new T(w.UNKNOWN_INDEX_SETTING,`Unknown index setting '${e}'`,t)))}return new n(c,o)}return n.create(e)}},Kv=class{constructor(e,t){this.compiler=e,this.declarationNode=t}validate(){return[...this.validateContext(),...this.validateName(this.declarationNode.name),...this.validateAlias(this.declarationNode.alias),...this.validateSettingList(this.declarationNode.attributeList),...this.validateBody(this.declarationNode.body)]}validateContext(){let e=this.declarationNode.parent;return!(e instanceof R)&&!(e instanceof z&&e.isKind(r.Table,r.TableGroup,r.TablePartial,r.Project,r.Dep))?[new T(w.INVALID_NOTE_CONTEXT,`A Note can only appear inside a Table, a TableGroup, a TablePartial or a Project. Sticky note can only appear at the global scope.`,this.declarationNode)]:[]}validateName(e){return this.compiler.nodeFullname(this.declarationNode).getErrors()}validateAlias(e){return e?[new T(w.UNEXPECTED_ALIAS,`A Note shouldn't have an alias`,e)]:[]}isStickyNote(){return this.declarationNode.parent instanceof R}validateSettingList(e){if(!e)return[];if(!this.isStickyNote())return[new T(w.UNEXPECTED_SETTINGS,`A Note shouldn't have a setting list`,e)];let t=E(e),n=t.getErrors();return Sa(t.getValue(),(e,t)=>{switch(t){case i.Color:{let t=gv[i.Color];e.length>1&&n.push(...e.map(e=>new T(w.DUPLICATE_NOTE_SETTING,`'color' can only appear once`,e))),e.forEach(e=>{t.isValidBuiltinFieldValue(e.value)||n.push(new T(w.INVALID_NOTE_SETTING_VALUE,t.message,e.value||e.name))});break}default:n.push(...bv(t,e,{duplicate:w.DUPLICATE_NOTE_SETTING,invalidValue:w.INVALID_NOTE_SETTING_VALUE}))}}),n}validateBody(e){if(!e)return[];if(e instanceof H)return this.validateFields([e]);let[t,n]=_(e.body,e=>e instanceof H);return[...this.validateFields(t),...this.validateSubElements(n)]}validateFields(e){let t=[];return e.length===0?[new T(w.EMPTY_NOTE,`A Note must have a content`,this.declarationNode)]:(e.length>1&&e.slice(1).forEach(e=>t.push(new T(w.NOTE_CONTENT_REDEFINED,`A Note can only contain one string`,e))),D(e[0].callee)||t.push(new T(w.INVALID_NOTE,`A Note content must be a quoted string`,e[0])),e[0].args.length>0&&t.push(...e[0].args.map(e=>new T(w.INVALID_NOTE,`A Note can only contain one quoted string`,e))),t)}validateSubElements(e){return e.flatMap(e=>e.type?this.compiler.validateNode(e).getErrors():[])}},qv={validateNode(t,i){return A(i,r.Note)?n.create(void 0,new Kv(t,i).validate()):n.create(e)},nodeFullname(t,i){if(!A(i,r.Note))return n.create(e);if(!(i.parent instanceof R))return i.name?new n(void 0,[new T(w.UNEXPECTED_NAME,`A Note shouldn't have a name`,i.name)]):new n(void 0);if(!i.name)return new n(void 0,[new T(w.INVALID_NAME,`Sticky note must have a name`,i)]);if(i.name instanceof Tm)return new n(void 0,[new T(w.INVALID_NAME,`Wildcard (*) is not allowed as a Note name`,i.name)]);let a=M(i.name);return a===void 0?new n(void 0,[new T(w.INVALID_NAME,`Invalid name for sticky note `,i)]):new n(a)},nodeAlias(t,i){return A(i,r.Note)?i.alias?new n(void 0,[new T(w.UNEXPECTED_ALIAS,`A Note shouldn't have an alias`,i.alias)]):new n(void 0):n.create(e)},nodeSettings(t,i){return A(i,r.Note)?i.attributeList?new n({},[new T(w.UNEXPECTED_SETTINGS,`A Note shouldn't have a setting list`,i.attributeList)]):new n({}):n.create(e)}},Jv={[Vp.Table]:O_,[Vp.Column]:k_,[Vp.TableGroup]:J_,[Vp.Note]:gv,[Vp.Ref]:o_},Yv=class{constructor(e,t){this.compiler=e,this.declarationNode=t}validate(){return[...this.validateTargetKind(),...this.validateTargetName(this.declarationNode.name),...this.validateBody(this.declarationNode.body)]}validateTargetKind(){return yv(this.declarationNode)?[]:[new T(w.INVALID_METADATA_TARGET_KIND,`A Metadata target kind must be one of: ${Object.values(Vp).join(`, `)}`,this.declarationNode.targetKind??this.declarationNode)]}validateTargetName(e){return e?e instanceof Tm?[new T(w.INVALID_NAME,`Wildcard (*) is not allowed as a Metadata target's name`,e)]:yv(this.declarationNode)===Vp.Ref?kf(e)?[]:[new T(w.INVALID_NAME,`A Metadata Ref target must be a ref name (an identifier or a quoted identifier)`,e)]:Df(e)?[]:[new T(w.INVALID_NAME,`A Metadata target's name must be of the form <schema>.<table>, <table>.<column>, <schema>.<table>.<column> or a single identifier`,e)]:[new T(w.NAME_NOT_FOUND,`A Metadata target must have a name`,this.declarationNode)]}validateBody(e){if(!e)return[];if(e instanceof H)return[new T(w.UNEXPECTED_SIMPLE_BODY,`A Metadata's body must be a block`,e)];let[t,n]=_(e.body,e=>e instanceof H);return[...this.validateFields(t),...this.validateSubElements(n)]}validateFields(e){return e.map(e=>new T(w.INVALID_METADATA_FIELD,`A Metadata field must use the 'key: value' syntax`,e))}validateSubElements(e){let t={},n=yv(this.declarationNode),r=e.flatMap(e=>{if(!e.type)return[];if(e.body instanceof U)return[new T(w.UNEXPECTED_COMPLEX_BODY,`A Metadata field can only have an inline value`,e.body)];let r=e.type.value.toLowerCase();t[r]||(t[r]=[]),t[r].push(e);let i=n?Jv[n][r]:void 0;return i?i.isValidBuiltinFieldValue(e.body?.callee)?[]:[new T(w.INVALID_METADATA_FIELD,i.message,e)]:Hf(e.body?.callee)?[]:[new T(w.INVALID_METADATA_FIELD,`A Metadata field value must be a string or a color literal`,e.body??e)]});return r.push(...Object.values(t).flatMap(e=>e.length<=1?[]:e.map(e=>new T(w.DUPLICATE_METADATA_FIELD,`Duplicate Metadata fields are defined`,e)))),r}},Xv={validateNode(t,i){return A(i,r.Metadata)?new n(void 0,new Yv(t,i).validate()):new n(e)},nodeFullname(t,i){return A(i,r.Metadata)?new n(void 0):new n(e)},nodeAlias(t,i){return A(i,r.Metadata)?new n(void 0):new n(e)},nodeSettings(t,i){return A(i,r.Metadata)?new n({}):new n(e)}},Zv=class{constructor(e,t){this.ast=t,this.compiler=e}validate(){let e=[],t=[];for(let n of this.ast.body){let r=this.compiler.validateNode(n);e.push(...r.getErrors()),t.push(...r.getWarnings())}return new n(this.ast,e,t)}},Qv={validateNode(t,r){return Jf(r)?n.create(void 0,new Zv(t,r).validate().getErrors()):n.create(e)},nodeFullname(t,r){return Jf(r)?new n(void 0):n.create(e)},nodeAlias(t,r){return Jf(r)?new n(void 0):n.create(e)},nodeSettings(t,r){return Jf(r)?new n({}):n.create(e)}},$v=class{constructor(e,t){this.compiler=e,this.declarationNode=t}validate(){return[...this.validateContext(),...this.validateName(this.declarationNode.name).getErrors(),...this.validateAlias(this.declarationNode.alias).getErrors(),...this.validateSettingList(this.declarationNode.attributeList).getErrors(),...this.validateBody(this.declarationNode.body)]}validateContext(){return this.declarationNode.parent instanceof z?[new T(w.INVALID_PROJECT_CONTEXT,`A Project can only appear top-level`,this.declarationNode)]:[]}validateName(e){return e?kf(e)?new n(M(e)):new n(void 0,[new T(w.INVALID_NAME,`A Project's name is optional or must be an identifier or a quoted identifer`,e)]):new n(void 0)}validateAlias(e){return e?new n(void 0,[new T(w.UNEXPECTED_ALIAS,`A Project shouldn't have an alias`,e)]):new n(void 0)}validateSettingList(e){return e?new n({},[new T(w.UNEXPECTED_SETTINGS,`A Project shouldn't have a setting list`,e)]):new n({})}validateBody(e){if(!e)return[];if(e instanceof H)return[new T(w.UNEXPECTED_SIMPLE_BODY,`A Project's body must be a block`,e)];let[t,n]=_(e.body,e=>e instanceof H);return[...t.map(e=>new T(w.INVALID_PROJECT_FIELD,`A Project can not have inline fields`,e)),...this.validateSubElements(n)]}validateSubElements(e){return e.flatMap(e=>e.type?this.compiler.validateNode(e).getErrors():[])}},ey={validateNode(t,i){return A(i,r.Project)?n.create(void 0,new $v(t,i).validate()):n.create(e)},nodeFullname(t,i){return A(i,r.Project)?i.name?kf(i.name)?new n(M(i.name)):new n(void 0,[new T(w.INVALID_NAME,`A Project's name is optional or must be an identifier or a quoted identifer`,i.name)]):new n(void 0):j(i,r.Project)?new n(void 0):n.create(e)},nodeAlias(t,i){return A(i,r.Project)&&i.alias?new n(void 0,[new T(w.UNEXPECTED_ALIAS,`A Project shouldn't have an alias`,i.alias)]):j(i,r.Project)?new n(void 0):n.create(e)},nodeSettings(t,i){return A(i,r.Project)?i.attributeList?new n({},[new T(w.UNEXPECTED_SETTINGS,`A Project shouldn't have a setting list`,i.attributeList)]):new n({}):j(i,r.Project)?new n({}):n.create(e)}},ty=class{constructor(e,t){this.declarationNode=t,this.compiler=e}validate(){return[...this.validateContext(),...this.validateName(this.declarationNode.name),...this.validateAlias(this.declarationNode.alias),...this.validateSettingList(this.declarationNode.attributeList),...this.validateBody(this.declarationNode.body)]}validateContext(){let e=this.declarationNode.parent;return e instanceof R||e instanceof z&&e.isKind(r.Table)?[]:[new T(w.INVALID_RECORDS_CONTEXT,`Records can only appear at top-level or inside a Table`,this.declarationNode)]}validateName(e){return e instanceof Tm?[new T(w.INVALID_NAME,`Wildcard (*) is not allowed as a Records name`,e)]:this.declarationNode.parent instanceof R?this.validateTopLevelName(e):this.validateInsideTableName(e)}validateTopLevelName(e){if(!(e instanceof Cm))return[new T(w.INVALID_RECORDS_NAME,`Records at top-level must have a name in the form of table(col1, col2, ...) or schema.table(col1, col2, ...)`,e||this.declarationNode.type||this.declarationNode)];let t=[];return(!e.callee||!Df(e.callee))&&t.push(new T(w.INVALID_RECORDS_NAME,`Records table reference must be a valid table name`,e.callee||e)),(!e.argumentList||!Bf(e.argumentList))&&t.push(new T(w.INVALID_RECORDS_NAME,`Records column list must be simple column names`,e.argumentList||e)),t}validateInsideTableName(e){return e&&!Bf(e)?[new T(w.INVALID_RECORDS_NAME,`Records inside a Table can only have a column list like (col1, col2, ...)`,e)]:[]}validateAlias(e){return e?[new T(w.UNEXPECTED_ALIAS,`Records cannot have an alias`,e)]:[]}validateSettingList(e){return ny(e).getErrors()}validateBody(e){if(!e)return[];if(e instanceof H)return this.validateDataRow(e);let[t,n]=_(e.body,e=>e instanceof H);return[...this.validateDataRows(t),...this.validateSubElements(n)]}validateDataRows(e){return e.flatMap(e=>this.validateDataRow(e))}validateDataRow(e){let t=[];if(!e.callee||e.args.length>0)return t.push(new T(w.INVALID_RECORDS_FIELD,`Invalid record row structure`,e)),t;if(e.callee instanceof xm)for(let n of e.callee.elementList)this.isValidRecordValue(n)||t.push(new T(w.INVALID_RECORDS_FIELD,`Records can only contain simple values (literals, null, true, false, or enum references). Complex expressions are not allowed.`,n));else this.isValidRecordValue(e.callee)||t.push(new T(w.INVALID_RECORDS_FIELD,`Records can only contain simple values (literals, null, true, false, or enum references). Complex expressions are not allowed.`,e.callee));return t}isValidRecordValue(e){if(e instanceof Om||Lf(e)||D(e)||e instanceof ym)return!0;if(O(e)){let t=e.expression.variable.value.toLowerCase();return So.includes(t)}if(k(e)){let t=M(e);return t!==void 0&&t.length>0}return!1}validateSubElements(e){return e.flatMap(e=>e.type?this.compiler.validateNode(e).getErrors():[])}};function ny(e){let t=E(e),r=t.getErrors(),a=t.getValue();return Sa(a,(e,t)=>{switch(t){case i.Example:e.length>1&&r.push(...e.map(e=>new T(w.DUPLICATE_RECORDS_SETTING,`'example' can only appear once`,e))),e.forEach(e=>{e.value&&r.push(new T(w.INVALID_RECORDS_SETTING_VALUE,`'example' cannot have a value`,e))});break;default:r.push(...e.map(e=>new T(w.UNKNOWN_RECORDS_SETTING,`Unknown '${t}' setting`,e)))}}),new n(a,r)}var ry={validateNode(t,i){return A(i,r.Records)?n.create(void 0,new ty(t,i).validate()):n.create(e)},nodeFullname(i,a){if(A(a,r.Records)){if(a.name instanceof Tm)return new n(void 0,[new T(w.INVALID_NAME,`Wildcard (*) is not allowed as a Records name`,a.name)]);if(a.parent instanceof R){if(!(a.name instanceof Cm))return new n(void 0,[new T(w.INVALID_RECORDS_NAME,`Records at top-level must have a name in the form of table(col1, col2, ...) or schema.table(col1, col2, ...)`,a.name||a.type||a)]);let e=[];(!a.name.callee||!Df(a.name.callee))&&e.push(new T(w.INVALID_RECORDS_NAME,`Records table reference must be a valid table name`,a.name.callee||a.name)),(!a.name.argumentList||!Bf(a.name.argumentList))&&e.push(new T(w.INVALID_RECORDS_NAME,`Records column list must be simple column names`,a.name.argumentList||a.name));let t=M(a.name.callee);return t?.length===1&&t.unshift(y),new n(t,e)}else{let e=a.parent&&i.nodeFullname(a.parent).getFiltered(t);return a.name&&!Bf(a.name)?new n(e,[new T(w.INVALID_RECORDS_NAME,`Records inside a Table can only have a column list like (col1, col2, ...)`,a.name)]):new n(e)}}return j(a,r.Records)?new n(void 0):n.create(e)},nodeAlias(t,i){return A(i,r.Records)?i.alias?new n(void 0,[new T(w.UNEXPECTED_ALIAS,`Records cannot have an alias`,i.alias)]):new n(void 0):j(i,r.Records)?new n(void 0):n.create(e)},nodeSettings(t,i){return A(i,r.Records)?i.attributeList?ny(i.attributeList):new n({}):j(i,r.Records)?new n({}):n.create(e)}},iy=class{constructor(e,t){this.compiler=e,this.declarationNode=t}validate(){return[...this.validateContext(),...this.validateName(this.declarationNode.name),...this.validateAlias(this.declarationNode.alias),...this.validateSettingList(this.declarationNode.attributeList),...this.validateBody(this.declarationNode.body)]}validateContext(){return this.declarationNode.parent instanceof R?[]:[new T(w.INVALID_REF_CONTEXT,`A Ref must appear top-level`,this.declarationNode)]}validateName(e){return e?e instanceof Tm?[new T(w.INVALID_NAME,`Wildcard (*) is not allowed as a Ref name`,e)]:kf(e)?[]:[new T(w.INVALID_NAME,`A Ref's name is optional or must be an identifier or a quoted identifer`,e)]:[]}validateAlias(e){return e?[new T(w.UNEXPECTED_ALIAS,`A Ref shouldn't have an alias`,e)]:[]}validateSettingList(e){return e?[new T(w.UNEXPECTED_SETTINGS,`A Ref shouldn't have a setting list`,e)]:[]}validateBody(e){if(!e)return[];if(e instanceof H)return this.validateFields([e]);let[t,n]=_(e.body,e=>e instanceof H);return[...this.validateFields(t),...this.validateSubElements(n)]}validateFields(e){if(e.length===0)return[new T(w.EMPTY_REF,`A Ref must have at least one field`,this.declarationNode)];let t=[];return e.length>1&&t.push(...e.slice(1).map(e=>new T(w.REF_REDEFINED,`A Ref can only contain one binary relationship`,e))),e.forEach(e=>{if(e.callee&&!$f(e.callee)&&t.push(new T(w.INVALID_REF_FIELD,`A Ref field must be a binary relationship`,e.callee)),e.callee&&$f(e.callee)){let n=up(e.callee.leftExpression)??{variables:[],tupleElements:[]},r=n.variables.length+Math.min(n.tupleElements.length,1),i=up(e.callee.rightExpression)??{variables:[],tupleElements:[]},a=i.variables.length+Math.min(i.tupleElements.length,1);r<2&&t.push(new T(w.INVALID_REF_FIELD,`Invalid column reference`,e.callee.leftExpression||e.callee)),a<2&&t.push(new T(w.INVALID_REF_FIELD,`Invalid column reference`,e.callee.rightExpression||e.callee))}e.callee&&!ep(e.callee)&&t.push(new T(w.UNEQUAL_FIELDS_BINARY_REF,`Unequal fields in ref endpoints`,e.callee));let n=[...e.args];if(g(n)instanceof W){let e=this.validateFieldSettings(g(n));t.push(...e),n.pop()}else n[0]instanceof W&&(t.push(...this.validateFieldSettings(n[0])),n.shift());n.length>0&&t.push(...n.map(e=>new T(w.INVALID_REF_FIELD,`A Ref field should only have a single binary relationship`,e)))}),t}validateFieldSettings(e){return oy(e).getErrors()}validateSubElements(e){return e.flatMap(e=>e.type?this.compiler.validateNode(e).getErrors():[])}};function ay(e){if(!(O(e)&&e.expression.variable.kind!==v.QUOTED_STRING)&&!(e instanceof gm))return!1;let t;if(t=e instanceof gm?ap(e)||``:e.expression.variable.value,t)switch(t.toLowerCase()){case`cascade`:case`no action`:case`set null`:case`set default`:case`restrict`:return!0;default:return!1}return!1}function oy(e){let t=E(e),r=t.getErrors(),a=t.getValue(),o={};for(let[e,t]of Object.entries(a))switch(e){case i.Delete:case i.Update:t.length>1&&t.forEach(t=>r.push(new T(w.DUPLICATE_REF_SETTING,`'${e}' can only appear once`,t))),t.forEach(t=>{ay(t.value)||r.push(new T(w.INVALID_REF_SETTING_VALUE,`'${e}' can only have values "cascade", "no action", "set null", "set default" or "restrict"`,t))}),o[e]=t;break;case i.Color:t.length>1&&r.push(...t.map(e=>new T(w.DUPLICATE_REF_SETTING,`'color' can only appear once`,e))),t.forEach(e=>{Pf(e.value)||r.push(new T(w.INVALID_REF_SETTING_VALUE,`'color' must be a color literal`,e))}),o[e]=t;break;case i.Inactive:t.length>1&&r.push(...t.map(e=>new T(w.DUPLICATE_REF_SETTING,`'inactive' can only appear once`,e))),t.forEach(e=>{e.value&&r.push(new T(w.INVALID_REF_SETTING_VALUE,`'inactive' cannot have a value`,e))}),o[e]=t;break;default:r.push(...bv(e,t,{duplicate:w.DUPLICATE_REF_SETTING,invalidValue:w.INVALID_REF_SETTING_VALUE})),o[e]=t;break}return new n(o,r)}var sy={validateNode(t,i){return A(i,r.Ref)?n.create(void 0,new iy(t,i).validate()):n.create(e)},nodeFullname(t,i){return A(i,r.Ref)?i.name?kf(i.name)?new n(M(i.name)):new n(void 0,[new T(w.INVALID_NAME,`A Ref's name is optional or must be an identifier or a quoted identifer`,i.name)]):new n(void 0):j(i,r.Ref)?new n(void 0):n.create(e)},nodeAlias(t,i){return A(i,r.Ref)?i.alias?new n(void 0,[new T(w.UNEXPECTED_ALIAS,`A Ref shouldn't have an alias`,i.alias)]):new n(void 0):j(i,r.Ref)?new n(void 0):n.create(e)},nodeSettings(t,i){if(A(i,r.Ref))return i.attributeList?new n({},[new T(w.UNEXPECTED_SETTINGS,`A Ref shouldn't have a setting list`,i.attributeList)]):new n({});if(j(i,r.Ref)){let e=[...i.args],t;return g(e)instanceof W?t=g(e):e[0]instanceof W&&(t=e[0]),t?oy(t):new n({})}return n.create(e)}},cy=class{constructor(e,t){this.declarationNode=t,this.compiler=e}validate(){return[...this.validateContext(),...this.validateName(this.declarationNode.name),...this.validateAlias(this.declarationNode.alias),...this.validateSettingList(this.declarationNode.attributeList),...this.validateBody(this.declarationNode.body)]}validateContext(){return this.declarationNode.parent instanceof z?[new T(w.INVALID_TABLE_CONTEXT,`Table must appear top-level`,this.declarationNode)]:[]}validateName(e){return e?e instanceof km?[new T(w.INVALID_NAME,`Invalid array as Table name, maybe you forget to add a space between the name and the setting list?`,e)]:e instanceof Tm?[new T(w.INVALID_NAME,`Wildcard (*) is not allowed as a Table name`,e)]:Df(e)?[]:[new T(w.INVALID_NAME,`A Table name must be of the form <table> or <schema>.<table>`,e)]:[new T(w.NAME_NOT_FOUND,`A Table must have a name`,this.declarationNode)]}validateAlias(e){return e?Of(e)?[]:[new T(w.INVALID_ALIAS,`Table aliases can only contain alphanumeric and underscore unless surrounded by double quotes`,e)]:[]}validateSettingList(e){return ly(e).getErrors()}validateBody(e){if(!e)return[];if(e instanceof H)return[new T(w.UNEXPECTED_SIMPLE_BODY,`A Table's body must be a block`,e)];let[t,n]=_(e.body,e=>e instanceof H);return[...this.validateFields(t),...this.validateSubElements(n)]}validateFields(e){let t=e=>{let t=[];if(!e.callee)return[];e.args.length===0&&t.push(new T(w.INVALID_COLUMN,`A column must have a type`,e.callee)),O(e.callee)||t.push(new T(w.INVALID_COLUMN_NAME,`A column name must be an identifier or a quoted identifier`,e.callee)),e.args[0]&&!Vf(e.args[0])&&t.push(new T(w.INVALID_COLUMN_TYPE,`Invalid column type`,e.args[0]));let n=e.args.slice(1);return t.push(...this.validateFieldSetting(n)),t},n=e=>{let t=[];return e.callee?(jf(e.callee)||t.push(new T(w.INVALID_TABLE_PARTIAL_INJECTION,`A partial injection should be of the form ~<table-partial>`,e.callee)),e.args.length&&t.push(...e.args.map(e=>new T(w.INVALID_TABLE_PARTIAL_INJECTION,`A partial injection does not have any trailing attributes`,e))),t):[]};return e.flatMap(e=>e.callee instanceof V&&e.callee.op?.value===`~`?n(e):t(e))}validateFieldSetting(e){return uy(e).getErrors()}validateSubElements(e){let t=e.flatMap(e=>e.type?this.compiler.validateNode(e).getErrors():[]),n=e.filter(e=>e.isKind(r.Note));return n.length>1&&t.push(...n.map(e=>new T(w.NOTE_REDEFINED,`Duplicate notes are defined`,e))),t}};function ly(e){let t=E(e),r=t.getErrors(),a=t.getValue();return Sa(a,(e,t)=>{switch(t){case i.HeaderColor:e.length>1&&r.push(...e.map(e=>new T(w.DUPLICATE_TABLE_SETTING,`'headercolor' can only appear once`,e))),e.forEach(e=>{let t=O_[i.HeaderColor];t.isValidBuiltinFieldValue(e.value)||r.push(new T(w.INVALID_TABLE_SETTING_VALUE,t.message,e.value||e.name))});break;case i.Note:e.length>1&&r.push(...e.map(e=>new T(w.DUPLICATE_TABLE_SETTING,`'note' can only appear once`,e))),e.forEach(e=>{let t=O_[i.Note];t.isValidBuiltinFieldValue(e.value)||r.push(new T(w.INVALID_TABLE_SETTING_VALUE,t.message,e.value||e.name))});break;case i.Dep:e.forEach(e=>{zf(e.value)||r.push(new T(w.INVALID_TABLE_SETTING_VALUE,"'dep' must be `-> target` or `<- source`",e.value||e.name))});break;default:r.push(...bv(t,e,{duplicate:w.DUPLICATE_TABLE_SETTING,invalidValue:w.INVALID_TABLE_SETTING_VALUE}))}}),new n(a,r)}function uy(e){if(!e.slice(0,-1).every(Wf)||!e.slice(-1).every(e=>Wf(e)||e instanceof W))return new n({},e.map(e=>new T(w.INVALID_COLUMN,`These fields must be some inline settings optionally ended with a setting list`,e)));if(e.length===0)return new n({});let t;g(e)instanceof W&&(t=e.pop());let r=E(t),a=r.getErrors(),o=r.getValue();e.forEach(e=>{let t=(P(e)??``).toLowerCase();if(t!==i.PK&&t!==i.Unique){a.push(new T(w.INVALID_SETTINGS,"Inline column settings can only be `pk` or `unique`",e));return}o[t]===void 0?o[t]=[e]:o[t].push(e)});let s=o[i.PK]||[],c=o[i.PrimaryKey]||[];return s.length>=1&&c.length>=1&&a.push(...[...s,...c].map(e=>new T(w.DUPLICATE_COLUMN_SETTING,`Either one of 'primary key' and 'pk' can appear`,e))),Sa(o,(e,t)=>{switch(t){case i.Note:e.length>1&&a.push(...e.map(e=>new T(w.DUPLICATE_COLUMN_SETTING,`note can only appear once`,e))),e.forEach(e=>{let t=k_[i.Note];t.isValidBuiltinFieldValue(e.value)||a.push(new T(w.INVALID_COLUMN_SETTING_VALUE,t.message,e.value||e.name))});break;case i.Ref:e.forEach(e=>{Rf(e.value)||a.push(new T(w.INVALID_COLUMN_SETTING_VALUE,`'ref' must be a valid unary relationship`,e.value||e.name))});break;case i.PrimaryKey:e.length>1&&a.push(...e.map(e=>new T(w.DUPLICATE_COLUMN_SETTING,`primary key can only appear once`,e))),e.forEach(e=>{e.value!==void 0&&a.push(new T(w.INVALID_COLUMN_SETTING_VALUE,`'primary key' must not have a value`,e.value||e.name))});break;case i.PK:e.length>1&&a.push(...e.map(e=>new T(w.DUPLICATE_COLUMN_SETTING,`'pk' can only appear once`,e))),e.forEach(e=>{e instanceof B&&e.value!==void 0&&a.push(new T(w.INVALID_COLUMN_SETTING_VALUE,`'pk' must not have a value`,e.value||e.name))});break;case i.NotNull:{e.length>1&&a.push(...e.map(e=>new T(w.DUPLICATE_COLUMN_SETTING,`'not null' can only appear once`,e)));let t=o[i.Null]||[];e.length>=1&&t.length>=1&&a.push(...[...e,...t].map(e=>new T(w.CONFLICTING_SETTING,`'not null' and 'null' can not be set at the same time`,e))),e.forEach(e=>{e.value!==void 0&&a.push(new T(w.INVALID_COLUMN_SETTING_VALUE,`'not null' must not have a value`,e.value||e.name))});break}case i.Null:e.length>1&&a.push(...e.map(e=>new T(w.DUPLICATE_COLUMN_SETTING,`'null' can only appear once`,e))),e.forEach(e=>{e.value!==void 0&&a.push(new T(w.INVALID_COLUMN_SETTING_VALUE,`'null' must not have a value`,e.value||e.name))});break;case i.Unique:e.length>1&&a.push(...e.map(e=>new T(w.DUPLICATE_COLUMN_SETTING,`'unique' can only appear once`,e))),e.forEach(e=>{e instanceof B&&e.value!==void 0&&a.push(new T(w.INVALID_COLUMN_SETTING_VALUE,`'unique' must not have a value`,e.value||e.name))});break;case i.Increment:e.length>1&&a.push(...e.map(e=>new T(w.DUPLICATE_COLUMN_SETTING,`'increment' can only appear once`,e))),e.forEach(e=>{e instanceof B&&e.value!==void 0&&a.push(new T(w.INVALID_COLUMN_SETTING_VALUE,`'increment' must not have a value`,e.value||e.name))});break;case i.Default:e.length>1&&a.push(...e.map(e=>new T(w.DUPLICATE_COLUMN_SETTING,`'default' can only appear once`,e))),e.forEach(e=>{If(e.value)||a.push(new T(w.INVALID_COLUMN_SETTING_VALUE,`'default' must be an enum value, a string literal, number literal, function expression, true, false or null`,e.value||e.name))});break;case i.Check:e.forEach(e=>{e.value instanceof ym||a.push(new T(w.INVALID_COLUMN_SETTING_VALUE,`'check' must be a function expression`,e.value||e.name))});break;case i.Dep:e.forEach(e=>{zf(e.value)||a.push(new T(w.INVALID_COLUMN_SETTING_VALUE,`'dep' must be a valid unary relationship`,e.value||e.name))});break;default:a.push(...bv(t,e,{duplicate:w.DUPLICATE_COLUMN_SETTING,invalidValue:w.INVALID_COLUMN_SETTING_VALUE}))}}),new n(o,a)}var dy={validateNode(t,i){return A(i,r.Table)?n.create(void 0,new cy(t,i).validate()):n.create(e)},nodeFullname(t,i){if(A(i,r.Table)){if(!i.name)return new n(void 0,[new T(w.NAME_NOT_FOUND,`A Table must have a name`,i)]);if(i.name instanceof km)return new n(void 0,[new T(w.INVALID_NAME,`Invalid array as Table name, maybe you forget to add a space between the name and the setting list?`,i.name)]);let e=M(i.name);return e?.length===1&&e.unshift(y),new n(e)}if(j(i,r.Table)){if(jf(i.callee)){let e=P(i.callee.expression);return new n(e?[e]:void 0)}let e=P(i.callee);return new n(e?[e]:void 0)}return n.create(e)},nodeAlias(t,i){return A(i,r.Table)?i.alias?Of(i.alias)?new n(P(i.alias)):new n(void 0,[new T(w.INVALID_ALIAS,`Table aliases can only contain alphanumeric and underscore unless surrounded by double quotes`,i.alias)]):new n(void 0):j(i,r.Table)?new n(void 0):n.create(e)},nodeSettings(t,i){return A(i,r.Table)?i.attributeList?ly(i.attributeList):new n({}):j(i,r.Table)?uy(i.args.slice(1)):n.create(e)}},fy=class{constructor(e,t){this.declarationNode=t,this.compiler=e}validate(){return[...this.validateContext(),...this.validateName(this.declarationNode.name),...this.validateAlias(this.declarationNode.alias),...this.validateSettingList(this.declarationNode.attributeList),...this.validateBody(this.declarationNode.body)]}validateContext(){return this.declarationNode.parent instanceof z?[new T(w.INVALID_TABLEGROUP_CONTEXT,`TableGroup must appear top-level`,this.declarationNode)]:[]}validateName(e){return e?e instanceof Tm?[new T(w.INVALID_NAME,`Wildcard (*) is not allowed as a TableGroup name`,e)]:kf(e)?[]:[new T(w.INVALID_NAME,`A TableGroup name must be a single identifier`,e)]:[new T(w.NAME_NOT_FOUND,`A TableGroup must have a name`,this.declarationNode)]}validateAlias(e){return e?[new T(w.UNEXPECTED_ALIAS,`A TableGroup shouldn't have an alias`,e)]:[]}validateSettingList(e){return py(e).getErrors()}validateBody(e){if(!e)return[];if(e instanceof H)return[new T(w.UNEXPECTED_SIMPLE_BODY,`A TableGroup's body must be a block`,e)];let[t,n]=_(e.body,e=>e instanceof H);return[...this.validateFields(t),...this.validateSubElements(n)]}validateFields(e){return e.flatMap(e=>{let t=[];return e.callee&&M(e.callee)===void 0&&t.push(new T(w.INVALID_TABLEGROUP_FIELD,`A TableGroup field must be of the form <table> or <schema>.<table>`,e.callee)),e.args.length>0&&t.push(...e.args.map(e=>new T(w.INVALID_TABLEGROUP_FIELD,`A TableGroup field should only have a single Table name`,e))),t})}validateSubElements(e){let t=e.flatMap(e=>e.type?this.compiler.validateNode(e).getErrors():[]),n=e.filter(e=>e.isKind(r.Note));return n.length>1&&t.push(...n.map(e=>new T(w.NOTE_REDEFINED,`Duplicate notes are defined`,e))),t}};function py(e){let t=E(e),r=t.getErrors(),a=t.getValue(),o={};for(let[e,t]of Object.entries(a))switch(e){case i.Color:case i.Note:{let n=J_[e];t.length>1&&r.push(...t.map(t=>new T(w.DUPLICATE_TABLE_SETTING,`'${e}' can only appear once`,t))),t.forEach(e=>{n.isValidBuiltinFieldValue(e.value)||r.push(new T(w.INVALID_TABLE_SETTING_VALUE,n.message,e.value||e.name))}),o[e]=t;break}default:r.push(...bv(e,t,{duplicate:w.DUPLICATE_TABLE_SETTING,invalidValue:w.INVALID_TABLE_SETTING_VALUE})),o[e]=t;break}return new n(o,r)}var my={validateNode(t,i){return A(i,r.TableGroup)?n.create(void 0,new fy(t,i).validate()):n.create(e)},nodeFullname(t,i){if(A(i,r.TableGroup))return i.name?kf(i.name)?new n(M(i.name)):new n(void 0,[new T(w.INVALID_NAME,`A TableGroup name must be a single identifier`,i.name)]):new n(void 0,[new T(w.NAME_NOT_FOUND,`A TableGroup must have a name`,i)]);if(j(i,r.TableGroup)){let e=M(i.callee);return e?.length===1&&e.unshift(y),new n(e)}return n.create(e)},nodeAlias(t,i){return A(i,r.TableGroup)?i.alias?new n(void 0,[new T(w.UNEXPECTED_ALIAS,`A TableGroup shouldn't have an alias`,i.alias)]):new n(void 0):j(i,r.TableGroup)?new n(void 0):n.create(e)},nodeSettings(t,i){return A(i,r.TableGroup)?py(i.attributeList):j(i,r.TableGroup)?new n({}):n.create(e)}},hy=class{constructor(e,t){this.compiler=e,this.declarationNode=t}validate(){return[...this.validateContext(),...this.validateName(this.declarationNode.name),...this.validateAlias(this.declarationNode.alias),...this.validateSettingList(this.declarationNode.attributeList),...this.validateBody(this.declarationNode.body)]}validateContext(){return this.declarationNode.parent instanceof z?[new T(w.INVALID_TABLE_PARTIAL_CONTEXT,`TablePartial must appear top-level`,this.declarationNode)]:[]}validateName(e){return e?e instanceof Tm?[new T(w.INVALID_NAME,`Wildcard (*) is not allowed as a TablePartial name`,e)]:kf(e)?[]:[new T(w.INVALID_NAME,`A TablePartial name must be an identifier or a quoted identifer`,e)]:[new T(w.NAME_NOT_FOUND,`A TablePartial must have a name`,this.declarationNode)]}validateAlias(e){return e?[new T(w.UNEXPECTED_ALIAS,`A TablePartial shouldn't have an alias`,e)]:[]}validateSettingList(e){let t=E(e),n=t.getErrors();return Sa(t.getValue(),(e,t)=>{switch(t){case i.HeaderColor:e.length>1&&n.push(...e.map(e=>new T(w.DUPLICATE_TABLE_PARTIAL_SETTING,`'${t}' can only appear once`,e))),e.forEach(e=>{Pf(e.value)||n.push(new T(w.INVALID_TABLE_PARTIAL_SETTING_VALUE,`'${t}' must be a color literal`,e.value||e.name))});break;case i.Note:e.length>1&&n.push(...e.map(e=>new T(w.DUPLICATE_TABLE_PARTIAL_SETTING,`'${t}' can only appear once`,e))),e.forEach(e=>{D(e.value)||n.push(new T(w.INVALID_TABLE_PARTIAL_SETTING_VALUE,`'${t}' must be a string literal`,e.value||e.name))});break;default:n.push(...e.map(e=>new T(w.UNKNOWN_TABLE_PARTIAL_SETTING,`Unknown '${t}' setting`,e)))}}),n}validateBody(e){if(!e)return[];if(e instanceof H)return[new T(w.UNEXPECTED_SIMPLE_BODY,`A TablePartial's body must be a block`,e)];let[t,n]=_(e.body,e=>e instanceof H);return[...this.validateFields(t),...this.validateSubElements(n)]}validateFields(e){return e.flatMap(e=>{if(!e.callee)return[];let t=[];e.args.length===0&&t.push(new T(w.INVALID_COLUMN,`A column must have a type`,e.callee)),O(e.callee)||t.push(new T(w.INVALID_COLUMN_NAME,`A column name must be an identifier or a quoted identifier`,e.callee)),e.args[0]&&!Vf(e.args[0])&&t.push(new T(w.INVALID_COLUMN_TYPE,`Invalid column type`,e.args[0]));let n=e.args.slice(1);return t.push(...this.validateFieldSetting(n)),t})}validateFieldSetting(e){let t=g(e);if(!e.slice(0,-1).every(Wf)||!(Wf(t)||t instanceof W))return e.map(e=>new T(w.INVALID_COLUMN,`These fields must be some inline settings optionally ended with a setting list`,e));if(e.length===0)return[];let n;g(e)instanceof W&&(n=e.pop());let r=E(n),a=r.getErrors(),o=r.getValue();e.forEach(e=>{let t=(P(e)??``).toLowerCase();if(t!==i.PK&&t!==i.Unique){a.push(new T(w.INVALID_SETTINGS,"Inline column settings can only be `pk` or `unique`",e));return}o[t]===void 0?o[t]=[e]:o[t].push(e)});let s=o[i.PK]||[],c=o[i.PrimaryKey]||[];return s.length>=1&&c.length>=1&&a.push(...[...s,...c].map(e=>new T(w.DUPLICATE_COLUMN_SETTING,`Either one of 'primary key' and 'pk' can appear`,e))),Sa(o,(e,t)=>{switch(t){case i.Note:e.length>1&&a.push(...e.map(e=>new T(w.DUPLICATE_COLUMN_SETTING,`'${t}' can only appear once`,e))),e.forEach(e=>{D(e.value)||a.push(new T(w.INVALID_COLUMN_SETTING_VALUE,`'${t}' must be a quoted string`,e.value||e.name))});break;case i.Ref:e.forEach(e=>{Rf(e.value)||a.push(new T(w.INVALID_COLUMN_SETTING_VALUE,`'${t}' must be a valid unary relationship`,e.value||e.name))});break;case i.PrimaryKey:e.length>1&&a.push(...e.map(e=>new T(w.DUPLICATE_COLUMN_SETTING,`'${t}' can only appear once`,e))),e.forEach(e=>{e.value!==void 0&&a.push(new T(w.INVALID_COLUMN_SETTING_VALUE,`'${t}' must not have a value`,e.value||e.name))});break;case i.PK:e.length>1&&a.push(...e.map(e=>new T(w.DUPLICATE_COLUMN_SETTING,`'${t}' can only appear once`,e))),e.forEach(e=>{e instanceof B&&e.value!==void 0&&a.push(new T(w.INVALID_COLUMN_SETTING_VALUE,`'${t}' must not have a value`,e.value||e.name))});break;case i.NotNull:{e.length>1&&a.push(...e.map(e=>new T(w.DUPLICATE_COLUMN_SETTING,`'${t}' can only appear once`,e)));let n=o[i.Null]||[];e.length>=1&&n.length>=1&&a.push(...[...e,...n].map(e=>new T(w.CONFLICTING_SETTING,`'not null' and 'null' can not be set at the same time`,e))),e.forEach(e=>{e.value!==void 0&&a.push(new T(w.INVALID_COLUMN_SETTING_VALUE,`'${t}' must not have a value`,e.value||e.name))});break}case i.Null:e.length>1&&a.push(...e.map(e=>new T(w.DUPLICATE_COLUMN_SETTING,`'${t}' can only appear once`,e))),e.forEach(e=>{e.value!==void 0&&a.push(new T(w.INVALID_COLUMN_SETTING_VALUE,`'${t}' must not have a value`,e.value||e.name))});break;case i.Unique:e.length>1&&a.push(...e.map(e=>new T(w.DUPLICATE_COLUMN_SETTING,`'${t}' can only appear once`,e))),e.forEach(e=>{e instanceof B&&e.value!==void 0&&a.push(new T(w.INVALID_COLUMN_SETTING_VALUE,`'${t}' must not have a value`,e.value||e.name))});break;case i.Increment:e.length>1&&a.push(...e.map(e=>new T(w.DUPLICATE_COLUMN_SETTING,`'${t}' can only appear once`,e))),e.forEach(e=>{e instanceof B&&e.value!==void 0&&a.push(new T(w.INVALID_COLUMN_SETTING_VALUE,`'${t}' must not have a value`,e.value||e.name))});break;case i.Default:e.length>1&&a.push(...e.map(e=>new T(w.DUPLICATE_COLUMN_SETTING,`'${t}' can only appear once`,e))),e.forEach(e=>{If(e.value)||a.push(new T(w.INVALID_COLUMN_SETTING_VALUE,`'${t}' must be a string literal, number literal, function expression, true, false or null`,e.value||e.name))});break;case i.Check:e.forEach(e=>{e.value instanceof ym||a.push(new T(w.INVALID_COLUMN_SETTING_VALUE,`'check' must be a function expression`,e.value||e.name))});break;default:a.push(...bv(t,e,{duplicate:w.DUPLICATE_COLUMN_SETTING,invalidValue:w.INVALID_COLUMN_SETTING_VALUE}))}}),a}validateSubElements(e){let t=e.flatMap(e=>e.type?this.compiler.validateNode(e).getErrors():[]),n=e.filter(e=>e.isKind(r.Note));return n.length>1&&t.push(...n.map(e=>new T(w.NOTE_REDEFINED,`Duplicate notes are defined`,e))),t}};function gy(e){let t=E(e),r=t.getErrors(),a=t.getValue();return Sa(a,(e,t)=>{switch(t){case i.HeaderColor:e.length>1&&r.push(...e.map(e=>new T(w.DUPLICATE_TABLE_PARTIAL_SETTING,`'${t}' can only appear once`,e))),e.forEach(e=>{Pf(e.value)||r.push(new T(w.INVALID_TABLE_PARTIAL_SETTING_VALUE,`'${t}' must be a color literal`,e.value||e.name))});break;case i.Note:e.length>1&&r.push(...e.map(e=>new T(w.DUPLICATE_TABLE_PARTIAL_SETTING,`'${t}' can only appear once`,e))),e.forEach(e=>{D(e.value)||r.push(new T(w.INVALID_TABLE_PARTIAL_SETTING_VALUE,`'${t}' must be a string literal`,e.value||e.name))});break;default:r.push(...e.map(e=>new T(w.UNKNOWN_TABLE_PARTIAL_SETTING,`Unknown '${t}' setting`,e)))}}),new n(a,r)}function _y(e){let t=g(e);if(e.length>0&&(!e.slice(0,-1).every(Wf)||!(Wf(t)||t instanceof W)))return new n({},e.map(e=>new T(w.INVALID_COLUMN,`These fields must be some inline settings optionally ended with a setting list`,e)));if(e.length===0)return new n({});let r;g(e)instanceof W&&(r=e.pop());let a=E(r),o=a.getErrors(),s=a.getValue();e.forEach(e=>{let t=(P(e)??``).toLowerCase();if(t!==i.PK&&t!==i.Unique){o.push(new T(w.INVALID_SETTINGS,"Inline column settings can only be `pk` or `unique`",e));return}s[t]===void 0?s[t]=[e]:s[t].push(e)});let c=s[i.PK]||[],l=s[i.PrimaryKey]||[];return c.length>=1&&l.length>=1&&o.push(...[...c,...l].map(e=>new T(w.DUPLICATE_COLUMN_SETTING,`Either one of 'primary key' and 'pk' can appear`,e))),Sa(s,(e,t)=>{switch(t){case i.Note:e.length>1&&o.push(...e.map(e=>new T(w.DUPLICATE_COLUMN_SETTING,`'${t}' can only appear once`,e))),e.forEach(e=>{D(e.value)||o.push(new T(w.INVALID_COLUMN_SETTING_VALUE,`'${t}' must be a quoted string`,e.value||e.name))});break;case i.Ref:e.forEach(e=>{Rf(e.value)||o.push(new T(w.INVALID_COLUMN_SETTING_VALUE,`'${t}' must be a valid unary relationship`,e.value||e.name))});break;case i.PrimaryKey:e.length>1&&o.push(...e.map(e=>new T(w.DUPLICATE_COLUMN_SETTING,`'${t}' can only appear once`,e))),e.forEach(e=>{e.value!==void 0&&o.push(new T(w.INVALID_COLUMN_SETTING_VALUE,`'${t}' must not have a value`,e.value||e.name))});break;case i.PK:e.length>1&&o.push(...e.map(e=>new T(w.DUPLICATE_COLUMN_SETTING,`'${t}' can only appear once`,e))),e.forEach(e=>{e instanceof B&&e.value!==void 0&&o.push(new T(w.INVALID_COLUMN_SETTING_VALUE,`'${t}' must not have a value`,e.value||e.name))});break;case i.NotNull:{e.length>1&&o.push(...e.map(e=>new T(w.DUPLICATE_COLUMN_SETTING,`'${t}' can only appear once`,e)));let n=s[i.Null]||[];e.length>=1&&n.length>=1&&o.push(...[...e,...n].map(e=>new T(w.CONFLICTING_SETTING,`'not null' and 'null' can not be set at the same time`,e))),e.forEach(e=>{e.value!==void 0&&o.push(new T(w.INVALID_COLUMN_SETTING_VALUE,`'${t}' must not have a value`,e.value||e.name))});break}case i.Null:e.length>1&&o.push(...e.map(e=>new T(w.DUPLICATE_COLUMN_SETTING,`'${t}' can only appear once`,e))),e.forEach(e=>{e.value!==void 0&&o.push(new T(w.INVALID_COLUMN_SETTING_VALUE,`'${t}' must not have a value`,e.value||e.name))});break;case i.Unique:e.length>1&&o.push(...e.map(e=>new T(w.DUPLICATE_COLUMN_SETTING,`'${t}' can only appear once`,e))),e.forEach(e=>{e instanceof B&&e.value!==void 0&&o.push(new T(w.INVALID_COLUMN_SETTING_VALUE,`'${t}' must not have a value`,e.value||e.name))});break;case i.Increment:e.length>1&&o.push(...e.map(e=>new T(w.DUPLICATE_COLUMN_SETTING,`'${t}' can only appear once`,e))),e.forEach(e=>{e instanceof B&&e.value!==void 0&&o.push(new T(w.INVALID_COLUMN_SETTING_VALUE,`'${t}' must not have a value`,e.value||e.name))});break;case i.Default:e.length>1&&o.push(...e.map(e=>new T(w.DUPLICATE_COLUMN_SETTING,`'${t}' can only appear once`,e))),e.forEach(e=>{If(e.value)||o.push(new T(w.INVALID_COLUMN_SETTING_VALUE,`'${t}' must be a string literal, number literal, function expression, true, false or null`,e.value||e.name))});break;case i.Check:e.forEach(e=>{e.value instanceof ym||o.push(new T(w.INVALID_COLUMN_SETTING_VALUE,`'check' must be a function expression`,e.value||e.name))});break;default:o.push(...bv(t,e,{duplicate:w.DUPLICATE_COLUMN_SETTING,invalidValue:w.INVALID_COLUMN_SETTING_VALUE}))}}),new n(s,o)}var vy={validateNode(t,i){return A(i,r.TablePartial)?n.create(void 0,new hy(t,i).validate()):n.create(e)},nodeFullname(t,i){if(A(i,r.TablePartial))return i.name?kf(i.name)?new n(M(i.name)):new n(void 0,[new T(w.INVALID_NAME,`A TablePartial name must be an identifier or a quoted identifer`,i.name)]):new n(void 0,[new T(w.NAME_NOT_FOUND,`A TablePartial must have a name`,i)]);if(j(i,r.TablePartial)){let e=P(i.callee);return new n(e?[e]:void 0)}return n.create(e)},nodeAlias(t,i){return A(i,r.TablePartial)?i.alias?new n(void 0,[new T(w.UNEXPECTED_ALIAS,`A TablePartial shouldn't have an alias`,i.alias)]):new n(P(i.alias)):j(i,r.TablePartial)?new n(void 0):n.create(e)},nodeSettings(t,i){return A(i,r.TablePartial)?gy(i.attributeList):j(i,r.TablePartial)?_y(i.args.slice(1)):n.create(e)}},yy=class{constructor(e,t){this.compiler=e,this.declarationNode=t}validate(){return[...this.validateContext(),...this.validateBody()]}validateContext(){return this.declarationNode.parent instanceof R?[]:[new T(w.INVALID_USE_CONTEXT,`'use' must appear at the top level`,this.declarationNode)]}validateBody(){let e=[];if(this.declarationNode.specifiers instanceof Tm)return[];for(let t of this.declarationNode.specifiers?.specifiers||[])e.push(...this.validateSpecifier(t));return e}validateSpecifier(e){let t=[];return e.importKind===void 0?t.push(new T(w.INVALID_USE_SPECIFIER_KIND,`A use specifier must have a type (e.g. table, enum)`,e)):e.isKind(...Object.values(Bp))||t.push(new T(w.INVALID_USE_SPECIFIER_KIND,`'${e.importKind.value}' is not a valid specifier type`,e.importKind)),[...t,...this.compiler.nodeFullname(e).getErrors(),...this.compiler.nodeAlias(e).getErrors()]}},by=[dy,Uv,ry,Gv,Nv,sy,zv,ey,my,vy,qv,Xv,Vv,Qv,{validateNode(t,r){return r instanceof pm?n.create(void 0,new yy(t,r).validate()):n.create(e)},nodeFullname(t,r){if(!qf(r))return n.create(e);let i=M(r.name);return!i||i.length===0?n.create(void 0,[new T(w.INVALID_NAME,`Use specifiers must have a valid name (simple name or schema-qualified name)`,r)]):r.isKind(Bp.TableGroup)&&i.length>1?n.create(i,[new T(w.INVALID_USE_SPECIFIER_NAME,`A TableGroup name must be a simple name`,r)]):r.isKind(Bp.TablePartial)&&i.length>1?n.create(i,[new T(w.INVALID_USE_SPECIFIER_NAME,`A TablePartial name must be a simple name`,r)]):r.isKind(Bp.Note)&&i.length>1?n.create(i,[new T(w.INVALID_USE_SPECIFIER_NAME,`A Sticky note name must be a simple name`,r)]):(i.length===1&&!r.isKind(Bp.Schema)&&i.unshift(y),new n(i))},nodeAlias(t,r){return qf(r)?r.alias?Of(r.alias)?new n(P(r.alias)):new n(void 0,[new T(w.INVALID_ALIAS,`Use aliases can only contain alphanumeric and underscore unless surrounded by double quotes`,r.alias)]):new n(void 0):n.create(e)},nodeSettings(t,r){return r instanceof pm?n.create({}):n.create(e)}},Iv];function xy(t,...r){for(let n of by){let i=n[t];if(i){let t=i(...r);if(!t.hasValue(e))return t}}return n.create(e)}function Sy(r){let i=xy(`validateNode`,this,r);return i.hasValue(e)?n.create(t):i}function Cy(r){let i=xy(`nodeSettings`,this,r);return i.hasValue(e)?n.create(t):i}function wy(r){let i=xy(`nodeFullname`,this,r);return i.hasValue(e)?n.create(t):i}function Ty(r){let i=xy(`nodeAlias`,this,r);return i.hasValue(e)?n.create(t):i}var Ey=typeof document<`u`&&document.location&&document.location.hash.indexOf(`pseudo=true`)>=0;function Dy(e,t){let n;return n=t.length===0?e:e.replace(/\{(\d+)\}/g,(e,n)=>{let r=t[n[0]],i=e;return typeof r==`string`?i=r:(typeof r==`number`||typeof r==`boolean`||r==null)&&(i=String(r)),i}),Ey&&(n=`[`+n.replace(/[aouei]/g,`$&$&`)+`]`),n}function Oy(e,t,...n){return Dy(t,n)}var ky=!1,Ay=!1,jy=!1,My=!1,Ny=void 0,Py=typeof self==`object`?self:typeof global==`object`?global:{},Fy=void 0;Py.vscode!==void 0&&Py.vscode.process!==void 0?Fy=Py.vscode.process:typeof process<`u`&&(Fy=process);var Iy=typeof Fy?.versions?.electron==`string`&&Fy?.type===`renderer`;if(typeof navigator==`object`&&!Iy)Ny=navigator.userAgent,ky=Ny.indexOf(`Windows`)>=0,Ay=Ny.indexOf(`Macintosh`)>=0,(Ny.indexOf(`Macintosh`)>=0||Ny.indexOf(`iPad`)>=0||Ny.indexOf(`iPhone`)>=0)&&navigator.maxTouchPoints&&navigator.maxTouchPoints,jy=Ny.indexOf(`Linux`)>=0,Ny?.indexOf(`Mobi`),My=!0,Oy({key:`ensureLoaderPluginIsLoaded`,comment:[`{Locked}`]},`_`),navigator.language;else if(typeof Fy==`object`){ky=Fy.platform===`win32`,Ay=Fy.platform===`darwin`,jy=Fy.platform===`linux`,jy&&Fy.env.SNAP&&Fy.env.SNAP_REVISION,Fy.env.CI||Fy.env.BUILD_ARTIFACTSTAGINGDIRECTORY;let e=Fy.env.VSCODE_NLS_CONFIG;if(e)try{let t=JSON.parse(e);t.availableLanguages[`*`],t.locale,t.osLocale,t._translationsConfigFile}catch{}}else console.error(`Unable to resolve platform.`);var Ly=ky,Ry=Ay;My&&Py.importScripts;var zy=Ny,By=typeof Py.postMessage==`function`&&!Py.importScripts;(()=>{if(By){let e=[];Py.addEventListener(`message`,t=>{if(t.data&&t.data.vscodeScheduleAsyncWork)for(let n=0,r=e.length;n<r;n++){let r=e[n];if(r.id===t.data.vscodeScheduleAsyncWork){e.splice(n,1),r.callback();return}}});let t=0;return n=>{let r=++t;e.push({id:r,callback:n}),Py.postMessage({vscodeScheduleAsyncWork:r},`*`)}}return e=>setTimeout(e)})();var Vy=!!(zy&&zy.indexOf(`Chrome`)>=0);zy&&zy.indexOf(`Firefox`),!Vy&&zy&&zy.indexOf(`Safari`),zy&&zy.indexOf(`Edg/`),zy&&zy.indexOf(`Android`);var Hy;if(Py.vscode!==void 0&&Py.vscode.process!==void 0){let e=Py.vscode.process;Hy={get platform(){return e.platform},get arch(){return e.arch},get env(){return e.env},cwd(){return e.cwd()}}}else Hy=typeof process<`u`?{get platform(){return process.platform},get arch(){return process.arch},get env(){return process.env},cwd(){return process.env.VSCODE_CWD||process.cwd()}}:{get platform(){return Ly?`win32`:Ry?`darwin`:`linux`},get arch(){},get env(){return{}},cwd(){return`/`}};var Uy=Hy.cwd,Wy=Hy.env,Gy=Hy.platform,Ky=65,qy=97,Jy=90,Yy=122,Xy=46,Zy=47,Qy=92,$y=58,eb=63,tb=class extends Error{constructor(e,t,n){let r;typeof t==`string`&&t.indexOf(`not `)===0?(r=`must not be`,t=t.replace(/^not /,``)):r=`must be`;let i=`The "${e}" ${e.indexOf(`.`)===-1?`argument`:`property`} ${r} of type ${t}`;i+=`. Received type ${typeof n}`,super(i),this.code=`ERR_INVALID_ARG_TYPE`}};function nb(e,t){if(typeof e!=`object`||!e)throw new tb(t,`Object`,e)}function rb(e,t){if(typeof e!=`string`)throw new tb(t,`string`,e)}var ib=Gy===`win32`;function K(e){return e===Zy||e===Qy}function ab(e){return e===Zy}function ob(e){return e>=Ky&&e<=Jy||e>=qy&&e<=Yy}function sb(e,t,n,r){let i=``,a=0,o=-1,s=0,c=0;for(let l=0;l<=e.length;++l){if(l<e.length)c=e.charCodeAt(l);else if(r(c))break;else c=Zy;if(r(c)){if(!(o===l-1||s===1))if(s===2){if(i.length<2||a!==2||i.charCodeAt(i.length-1)!==Xy||i.charCodeAt(i.length-2)!==Xy){if(i.length>2){let e=i.lastIndexOf(n);e===-1?(i=``,a=0):(i=i.slice(0,e),a=i.length-1-i.lastIndexOf(n)),o=l,s=0;continue}else if(i.length!==0){i=``,a=0,o=l,s=0;continue}}t&&(i+=i.length>0?`${n}..`:`..`,a=2)}else i.length>0?i+=`${n}${e.slice(o+1,l)}`:i=e.slice(o+1,l),a=l-o-1;o=l,s=0}else c===Xy&&s!==-1?++s:s=-1}return i}function cb(e,t){nb(t,`pathObject`);let n=t.dir||t.root,r=t.base||`${t.name||``}${t.ext||``}`;return n?n===t.root?`${n}${r}`:`${n}${e}${r}`:r}var lb={resolve(...e){let t=``,n=``,r=!1;for(let i=e.length-1;i>=-1;i--){let a;if(i>=0){if(a=e[i],rb(a,`path`),a.length===0)continue}else t.length===0?a=Uy():(a=Wy[`=${t}`]||Uy(),(a===void 0||a.slice(0,2).toLowerCase()!==t.toLowerCase()&&a.charCodeAt(2)===Qy)&&(a=`${t}\\`));let o=a.length,s=0,c=``,l=!1,u=a.charCodeAt(0);if(o===1)K(u)&&(s=1,l=!0);else if(K(u))if(l=!0,K(a.charCodeAt(1))){let e=2,t=e;for(;e<o&&!K(a.charCodeAt(e));)e++;if(e<o&&e!==t){let n=a.slice(t,e);for(t=e;e<o&&K(a.charCodeAt(e));)e++;if(e<o&&e!==t){for(t=e;e<o&&!K(a.charCodeAt(e));)e++;(e===o||e!==t)&&(c=`\\\\${n}\\${a.slice(t,e)}`,s=e)}}}else s=1;else ob(u)&&a.charCodeAt(1)===$y&&(c=a.slice(0,2),s=2,o>2&&K(a.charCodeAt(2))&&(l=!0,s=3));if(c.length>0)if(t.length>0){if(c.toLowerCase()!==t.toLowerCase())continue}else t=c;if(r){if(t.length>0)break}else if(n=`${a.slice(s)}\\${n}`,r=l,l&&t.length>0)break}return n=sb(n,!r,`\\`,K),r?`${t}\\${n}`:`${t}${n}`||`.`},normalize(e){rb(e,`path`);let t=e.length;if(t===0)return`.`;let n=0,r,i=!1,a=e.charCodeAt(0);if(t===1)return ab(a)?`\\`:e;if(K(a))if(i=!0,K(e.charCodeAt(1))){let i=2,a=i;for(;i<t&&!K(e.charCodeAt(i));)i++;if(i<t&&i!==a){let o=e.slice(a,i);for(a=i;i<t&&K(e.charCodeAt(i));)i++;if(i<t&&i!==a){for(a=i;i<t&&!K(e.charCodeAt(i));)i++;if(i===t)return`\\\\${o}\\${e.slice(a)}\\`;i!==a&&(r=`\\\\${o}\\${e.slice(a,i)}`,n=i)}}}else n=1;else ob(a)&&e.charCodeAt(1)===$y&&(r=e.slice(0,2),n=2,t>2&&K(e.charCodeAt(2))&&(i=!0,n=3));let o=n<t?sb(e.slice(n),!i,`\\`,K):``;return o.length===0&&!i&&(o=`.`),o.length>0&&K(e.charCodeAt(t-1))&&(o+=`\\`),r===void 0?i?`\\${o}`:o:i?`${r}\\${o}`:`${r}${o}`},isAbsolute(e){rb(e,`path`);let t=e.length;if(t===0)return!1;let n=e.charCodeAt(0);return K(n)||t>2&&ob(n)&&e.charCodeAt(1)===$y&&K(e.charCodeAt(2))},join(...e){if(e.length===0)return`.`;let t,n;for(let r=0;r<e.length;++r){let i=e[r];rb(i,`path`),i.length>0&&(t===void 0?t=n=i:t+=`\\${i}`)}if(t===void 0)return`.`;let r=!0,i=0;if(typeof n==`string`&&K(n.charCodeAt(0))){++i;let e=n.length;e>1&&K(n.charCodeAt(1))&&(++i,e>2&&(K(n.charCodeAt(2))?++i:r=!1))}if(r){for(;i<t.length&&K(t.charCodeAt(i));)i++;i>=2&&(t=`\\${t.slice(i)}`)}return lb.normalize(t)},relative(e,t){if(rb(e,`from`),rb(t,`to`),e===t)return``;let n=lb.resolve(e),r=lb.resolve(t);if(n===r||(e=n.toLowerCase(),t=r.toLowerCase(),e===t))return``;let i=0;for(;i<e.length&&e.charCodeAt(i)===Qy;)i++;let a=e.length;for(;a-1>i&&e.charCodeAt(a-1)===Qy;)a--;let o=a-i,s=0;for(;s<t.length&&t.charCodeAt(s)===Qy;)s++;let c=t.length;for(;c-1>s&&t.charCodeAt(c-1)===Qy;)c--;let l=c-s,u=o<l?o:l,d=-1,f=0;for(;f<u;f++){let n=e.charCodeAt(i+f);if(n!==t.charCodeAt(s+f))break;n===Qy&&(d=f)}if(f!==u){if(d===-1)return r}else{if(l>u){if(t.charCodeAt(s+f)===Qy)return r.slice(s+f+1);if(f===2)return r.slice(s+f)}o>u&&(e.charCodeAt(i+f)===Qy?d=f:f===2&&(d=3)),d===-1&&(d=0)}let p=``;for(f=i+d+1;f<=a;++f)(f===a||e.charCodeAt(f)===Qy)&&(p+=p.length===0?`..`:`\\..`);return s+=d,p.length>0?`${p}${r.slice(s,c)}`:(r.charCodeAt(s)===Qy&&++s,r.slice(s,c))},toNamespacedPath(e){if(typeof e!=`string`||e.length===0)return e;let t=lb.resolve(e);if(t.length<=2)return e;if(t.charCodeAt(0)===Qy){if(t.charCodeAt(1)===Qy){let e=t.charCodeAt(2);if(e!==eb&&e!==Xy)return`\\\\?\\UNC\\${t.slice(2)}`}}else if(ob(t.charCodeAt(0))&&t.charCodeAt(1)===$y&&t.charCodeAt(2)===Qy)return`\\\\?\\${t}`;return e},dirname(e){rb(e,`path`);let t=e.length;if(t===0)return`.`;let n=-1,r=0,i=e.charCodeAt(0);if(t===1)return K(i)?e:`.`;if(K(i)){if(n=r=1,K(e.charCodeAt(1))){let i=2,a=i;for(;i<t&&!K(e.charCodeAt(i));)i++;if(i<t&&i!==a){for(a=i;i<t&&K(e.charCodeAt(i));)i++;if(i<t&&i!==a){for(a=i;i<t&&!K(e.charCodeAt(i));)i++;if(i===t)return e;i!==a&&(n=r=i+1)}}}}else ob(i)&&e.charCodeAt(1)===$y&&(n=t>2&&K(e.charCodeAt(2))?3:2,r=n);let a=-1,o=!0;for(let n=t-1;n>=r;--n)if(K(e.charCodeAt(n))){if(!o){a=n;break}}else o=!1;if(a===-1){if(n===-1)return`.`;a=n}return e.slice(0,a)},basename(e,t){t!==void 0&&rb(t,`ext`),rb(e,`path`);let n=0,r=-1,i=!0,a;if(e.length>=2&&ob(e.charCodeAt(0))&&e.charCodeAt(1)===$y&&(n=2),t!==void 0&&t.length>0&&t.length<=e.length){if(t===e)return``;let o=t.length-1,s=-1;for(a=e.length-1;a>=n;--a){let c=e.charCodeAt(a);if(K(c)){if(!i){n=a+1;break}}else s===-1&&(i=!1,s=a+1),o>=0&&(c===t.charCodeAt(o)?--o===-1&&(r=a):(o=-1,r=s))}return n===r?r=s:r===-1&&(r=e.length),e.slice(n,r)}for(a=e.length-1;a>=n;--a)if(K(e.charCodeAt(a))){if(!i){n=a+1;break}}else r===-1&&(i=!1,r=a+1);return r===-1?``:e.slice(n,r)},extname(e){rb(e,`path`);let t=0,n=-1,r=0,i=-1,a=!0,o=0;e.length>=2&&e.charCodeAt(1)===$y&&ob(e.charCodeAt(0))&&(t=r=2);for(let s=e.length-1;s>=t;--s){let t=e.charCodeAt(s);if(K(t)){if(!a){r=s+1;break}continue}i===-1&&(a=!1,i=s+1),t===Xy?n===-1?n=s:o!==1&&(o=1):n!==-1&&(o=-1)}return n===-1||i===-1||o===0||o===1&&n===i-1&&n===r+1?``:e.slice(n,i)},format:cb.bind(null,`\\`),parse(e){rb(e,`path`);let t={root:``,dir:``,base:``,ext:``,name:``};if(e.length===0)return t;let n=e.length,r=0,i=e.charCodeAt(0);if(n===1)return K(i)?(t.root=t.dir=e,t):(t.base=t.name=e,t);if(K(i)){if(r=1,K(e.charCodeAt(1))){let t=2,i=t;for(;t<n&&!K(e.charCodeAt(t));)t++;if(t<n&&t!==i){for(i=t;t<n&&K(e.charCodeAt(t));)t++;if(t<n&&t!==i){for(i=t;t<n&&!K(e.charCodeAt(t));)t++;t===n?r=t:t!==i&&(r=t+1)}}}}else if(ob(i)&&e.charCodeAt(1)===$y){if(n<=2)return t.root=t.dir=e,t;if(r=2,K(e.charCodeAt(2))){if(n===3)return t.root=t.dir=e,t;r=3}}r>0&&(t.root=e.slice(0,r));let a=-1,o=r,s=-1,c=!0,l=e.length-1,u=0;for(;l>=r;--l){if(i=e.charCodeAt(l),K(i)){if(!c){o=l+1;break}continue}s===-1&&(c=!1,s=l+1),i===Xy?a===-1?a=l:u!==1&&(u=1):a!==-1&&(u=-1)}return s!==-1&&(a===-1||u===0||u===1&&a===s-1&&a===o+1?t.base=t.name=e.slice(o,s):(t.name=e.slice(o,a),t.base=e.slice(o,s),t.ext=e.slice(a,s))),o>0&&o!==r?t.dir=e.slice(0,o-1):t.dir=t.root,t},sep:`\\`,delimiter:`;`,win32:null,posix:null},ub=(()=>{if(ib){let e=/\\/g;return()=>{let t=Uy().replace(e,`/`);return t.slice(t.indexOf(`/`))}}return()=>Uy()})(),db={resolve(...e){let t=``,n=!1;for(let r=e.length-1;r>=-1&&!n;r--){let i=r>=0?e[r]:ub();rb(i,`path`),i.length!==0&&(t=`${i}/${t}`,n=i.charCodeAt(0)===Zy)}return t=sb(t,!n,`/`,ab),n?`/${t}`:t.length>0?t:`.`},normalize(e){if(rb(e,`path`),e.length===0)return`.`;let t=e.charCodeAt(0)===Zy,n=e.charCodeAt(e.length-1)===Zy;return e=sb(e,!t,`/`,ab),e.length===0?t?`/`:n?`./`:`.`:(n&&(e+=`/`),t?`/${e}`:e)},isAbsolute(e){return rb(e,`path`),e.length>0&&e.charCodeAt(0)===Zy},join(...e){if(e.length===0)return`.`;let t;for(let n=0;n<e.length;++n){let r=e[n];rb(r,`path`),r.length>0&&(t===void 0?t=r:t+=`/${r}`)}return t===void 0?`.`:db.normalize(t)},relative(e,t){if(rb(e,`from`),rb(t,`to`),e===t||(e=db.resolve(e),t=db.resolve(t),e===t))return``;let n=e.length,r=n-1,i=t.length-1,a=r<i?r:i,o=-1,s=0;for(;s<a;s++){let n=e.charCodeAt(1+s);if(n!==t.charCodeAt(1+s))break;n===Zy&&(o=s)}if(s===a)if(i>a){if(t.charCodeAt(1+s)===Zy)return t.slice(1+s+1);if(s===0)return t.slice(1+s)}else r>a&&(e.charCodeAt(1+s)===Zy?o=s:s===0&&(o=0));let c=``;for(s=1+o+1;s<=n;++s)(s===n||e.charCodeAt(s)===Zy)&&(c+=c.length===0?`..`:`/..`);return`${c}${t.slice(1+o)}`},toNamespacedPath(e){return e},dirname(e){if(rb(e,`path`),e.length===0)return`.`;let t=e.charCodeAt(0)===Zy,n=-1,r=!0;for(let t=e.length-1;t>=1;--t)if(e.charCodeAt(t)===Zy){if(!r){n=t;break}}else r=!1;return n===-1?t?`/`:`.`:t&&n===1?`//`:e.slice(0,n)},basename(e,t){t!==void 0&&rb(t,`ext`),rb(e,`path`);let n=0,r=-1,i=!0,a;if(t!==void 0&&t.length>0&&t.length<=e.length){if(t===e)return``;let o=t.length-1,s=-1;for(a=e.length-1;a>=0;--a){let c=e.charCodeAt(a);if(c===Zy){if(!i){n=a+1;break}}else s===-1&&(i=!1,s=a+1),o>=0&&(c===t.charCodeAt(o)?--o===-1&&(r=a):(o=-1,r=s))}return n===r?r=s:r===-1&&(r=e.length),e.slice(n,r)}for(a=e.length-1;a>=0;--a)if(e.charCodeAt(a)===Zy){if(!i){n=a+1;break}}else r===-1&&(i=!1,r=a+1);return r===-1?``:e.slice(n,r)},extname(e){rb(e,`path`);let t=-1,n=0,r=-1,i=!0,a=0;for(let o=e.length-1;o>=0;--o){let s=e.charCodeAt(o);if(s===Zy){if(!i){n=o+1;break}continue}r===-1&&(i=!1,r=o+1),s===Xy?t===-1?t=o:a!==1&&(a=1):t!==-1&&(a=-1)}return t===-1||r===-1||a===0||a===1&&t===r-1&&t===n+1?``:e.slice(t,r)},format:cb.bind(null,`/`),parse(e){rb(e,`path`);let t={root:``,dir:``,base:``,ext:``,name:``};if(e.length===0)return t;let n=e.charCodeAt(0)===Zy,r;n?(t.root=`/`,r=1):r=0;let i=-1,a=0,o=-1,s=!0,c=e.length-1,l=0;for(;c>=r;--c){let t=e.charCodeAt(c);if(t===Zy){if(!s){a=c+1;break}continue}o===-1&&(s=!1,o=c+1),t===Xy?i===-1?i=c:l!==1&&(l=1):i!==-1&&(l=-1)}if(o!==-1){let r=a===0&&n?1:a;i===-1||l===0||l===1&&i===o-1&&i===a+1?t.base=t.name=e.slice(r,o):(t.name=e.slice(r,i),t.base=e.slice(r,o),t.ext=e.slice(i,o))}return a>0?t.dir=e.slice(0,a-1):n&&(t.dir=`/`),t},sep:`/`,delimiter:`:`,win32:null,posix:null};db.win32=lb.win32=lb,db.posix=lb.posix=db,ib?lb.normalize:db.normalize,ib?lb.resolve:db.resolve,ib?lb.relative:db.relative,ib?lb.dirname:db.dirname,ib?lb.basename:db.basename,ib?lb.extname:db.extname,ib?lb.sep:db.sep;var fb=/^\w[\w\d+.-]*$/,pb=/^\//,mb=/^\/\//;function hb(e,t){if(!e.scheme&&t)throw Error(`[UriError]: Scheme is missing: {scheme: "", authority: "${e.authority}", path: "${e.path}", query: "${e.query}", fragment: "${e.fragment}"}`);if(e.scheme&&!fb.test(e.scheme))throw Error(`[UriError]: Scheme contains illegal characters.`);if(e.path){if(e.authority){if(!pb.test(e.path))throw Error(`[UriError]: If a URI contains an authority component, then the path component must either be empty or begin with a slash ("/") character`)}else if(mb.test(e.path))throw Error(`[UriError]: If a URI does not contain an authority component, then the path cannot begin with two slash characters ("//")`)}}function gb(e,t){return!e&&!t?`file`:e}function _b(e,t){switch(e){case`https`:case`http`:case`file`:t?t[0]!==vb&&(t=vb+t):t=vb;break}return t}var q=``,vb=`/`,yb=/^(([^:/?#]+?):)?(\/\/([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/,bb=class e{static isUri(t){return t instanceof e?!0:t?typeof t.authority==`string`&&typeof t.fragment==`string`&&typeof t.path==`string`&&typeof t.query==`string`&&typeof t.scheme==`string`&&typeof t.fsPath==`string`&&typeof t.with==`function`&&typeof t.toString==`function`:!1}constructor(e,t,n,r,i,a=!1){typeof e==`object`?(this.scheme=e.scheme||q,this.authority=e.authority||q,this.path=e.path||q,this.query=e.query||q,this.fragment=e.fragment||q):(this.scheme=gb(e,a),this.authority=t||q,this.path=_b(this.scheme,n||q),this.query=r||q,this.fragment=i||q,hb(this,a))}get fsPath(){return Eb(this,!1)}with(e){if(!e)return this;let{scheme:t,authority:n,path:r,query:i,fragment:a}=e;return t===void 0?t=this.scheme:t===null&&(t=q),n===void 0?n=this.authority:n===null&&(n=q),r===void 0?r=this.path:r===null&&(r=q),i===void 0?i=this.query:i===null&&(i=q),a===void 0?a=this.fragment:a===null&&(a=q),t===this.scheme&&n===this.authority&&r===this.path&&i===this.query&&a===this.fragment?this:new Sb(t,n,r,i,a)}static parse(e,t=!1){let n=yb.exec(e);return n?new Sb(n[2]||q,Ab(n[4]||q),Ab(n[5]||q),Ab(n[7]||q),Ab(n[9]||q),t):new Sb(q,q,q,q,q)}static file(e){let t=q;if(Ly&&(e=e.replace(/\\/g,vb)),e[0]===vb&&e[1]===vb){let n=e.indexOf(vb,2);n===-1?(t=e.substring(2),e=vb):(t=e.substring(2,n),e=e.substring(n)||vb)}return new Sb(`file`,t,e,q,q)}static from(e,t){return new Sb(e.scheme,e.authority,e.path,e.query,e.fragment,t)}static joinPath(t,...n){if(!t.path)throw Error(`[UriError]: cannot call joinPath on URI without path`);let r;return r=Ly&&t.scheme===`file`?e.file(lb.join(Eb(t,!0),...n)).path:db.join(t.path,...n),t.with({path:r})}toString(e=!1){return Db(this,e)}toJSON(){return this}static revive(t){if(!t||t instanceof e)return t;{let e=new Sb(t);return e._formatted=t.external??null,e._fsPath=t._sep===xb?t.fsPath??null:null,e}}},xb=Ly?1:void 0,Sb=class extends bb{constructor(){super(...arguments),this._formatted=null,this._fsPath=null}get fsPath(){return this._fsPath||=Eb(this,!1),this._fsPath}toString(e=!1){return e?Db(this,!0):(this._formatted||=Db(this,!1),this._formatted)}toJSON(){let e={$mid:1};return this._fsPath&&(e.fsPath=this._fsPath,e._sep=xb),this._formatted&&(e.external=this._formatted),this.path&&(e.path=this.path),this.scheme&&(e.scheme=this.scheme),this.authority&&(e.authority=this.authority),this.query&&(e.query=this.query),this.fragment&&(e.fragment=this.fragment),e}},Cb={58:`%3A`,47:`%2F`,63:`%3F`,35:`%23`,91:`%5B`,93:`%5D`,64:`%40`,33:`%21`,36:`%24`,38:`%26`,39:`%27`,40:`%28`,41:`%29`,42:`%2A`,43:`%2B`,44:`%2C`,59:`%3B`,61:`%3D`,32:`%20`};function wb(e,t,n){let r,i=-1;for(let a=0;a<e.length;a++){let o=e.charCodeAt(a);if(o>=97&&o<=122||o>=65&&o<=90||o>=48&&o<=57||o===45||o===46||o===95||o===126||t&&o===47||n&&o===91||n&&o===93||n&&o===58)i!==-1&&(r+=encodeURIComponent(e.substring(i,a)),i=-1),r!==void 0&&(r+=e.charAt(a));else{r===void 0&&(r=e.substr(0,a));let t=Cb[o];t===void 0?i===-1&&(i=a):(i!==-1&&(r+=encodeURIComponent(e.substring(i,a)),i=-1),r+=t)}}return i!==-1&&(r+=encodeURIComponent(e.substring(i))),r===void 0?e:r}function Tb(e){let t;for(let n=0;n<e.length;n++){let r=e.charCodeAt(n);r===35||r===63?(t===void 0&&(t=e.substr(0,n)),t+=Cb[r]):t!==void 0&&(t+=e[n])}return t===void 0?e:t}function Eb(e,t){let n;return n=e.authority&&e.path.length>1&&e.scheme===`file`?`//${e.authority}${e.path}`:e.path.charCodeAt(0)===47&&(e.path.charCodeAt(1)>=65&&e.path.charCodeAt(1)<=90||e.path.charCodeAt(1)>=97&&e.path.charCodeAt(1)<=122)&&e.path.charCodeAt(2)===58?t?e.path.substr(1):e.path[1].toLowerCase()+e.path.substr(2):e.path,Ly&&(n=n.replace(/\//g,`\\`)),n}function Db(e,t){let n=t?Tb:wb,r=``,{scheme:i,authority:a,path:o,query:s,fragment:c}=e;if(i&&(r+=i,r+=`:`),(a||i===`file`)&&(r+=vb,r+=vb),a){let e=a.indexOf(`@`);if(e!==-1){let t=a.substr(0,e);a=a.substr(e+1),e=t.lastIndexOf(`:`),e===-1?r+=n(t,!1,!1):(r+=n(t.substr(0,e),!1,!1),r+=`:`,r+=n(t.substr(e+1),!1,!0)),r+=`@`}a=a.toLowerCase(),e=a.lastIndexOf(`:`),e===-1?r+=n(a,!1,!0):(r+=n(a.substr(0,e),!1,!0),r+=a.substr(e))}if(o){if(o.length>=3&&o.charCodeAt(0)===47&&o.charCodeAt(2)===58){let e=o.charCodeAt(1);e>=65&&e<=90&&(o=`/${String.fromCharCode(e+32)}:${o.substr(3)}`)}else if(o.length>=2&&o.charCodeAt(1)===58){let e=o.charCodeAt(0);e>=65&&e<=90&&(o=`${String.fromCharCode(e+32)}:${o.substr(2)}`)}r+=n(o,!0,!1)}return s&&(r+=`?`,r+=n(s,!1,!1)),c&&(r+=`#`,r+=t?c:wb(c,!1,!1)),r}function Ob(e){try{return decodeURIComponent(e)}catch{return e.length>3?e.substr(0,3)+Ob(e.substr(3)):e}}var kb=/(%[0-9A-Za-z][0-9A-Za-z])+/g;function Ab(e){return e.match(kb)?e.replace(kb,e=>Ob(e)):e}var J=function(e){return e[e.Function=1]=`Function`,e[e.Constructor=2]=`Constructor`,e[e.Field=3]=`Field`,e[e.Variable=4]=`Variable`,e[e.Class=5]=`Class`,e[e.Struct=6]=`Struct`,e[e.Interface=7]=`Interface`,e[e.Module=8]=`Module`,e[e.Property=9]=`Property`,e[e.Event=10]=`Event`,e[e.Operator=11]=`Operator`,e[e.Unit=12]=`Unit`,e[e.Value=13]=`Value`,e[e.Constant=14]=`Constant`,e[e.Enum=15]=`Enum`,e[e.EnumMember=16]=`EnumMember`,e[e.Keyword=17]=`Keyword`,e[e.Text=18]=`Text`,e[e.Color=19]=`Color`,e[e.File=20]=`File`,e[e.Reference=21]=`Reference`,e[e.Customcolor=22]=`Customcolor`,e[e.Folder=23]=`Folder`,e[e.TypeParameter=24]=`TypeParameter`,e[e.User=25]=`User`,e[e.Issue=26]=`Issue`,e[e.Snippet=27]=`Snippet`,e}({}),Y=function(e){return e[e.None=0]=`None`,e[e.KeepWhitespace=1]=`KeepWhitespace`,e[e.InsertAsSnippet=4]=`InsertAsSnippet`,e}({}),jb=function(e){return e[e.Hint=1]=`Hint`,e[e.Info=2]=`Info`,e[e.Warning=4]=`Warning`,e[e.Error=8]=`Error`,e}({});function Mb(e,t){return e.getOffsetAt(t)}function Nb(e,t){let{startPos:n,endPos:r}=t;return{startLineNumber:n.line+1,startColumn:n.column+1,endLineNumber:r.line+1,endColumn:r.column+1}}var Pb=class{constructor(e){this.compiler=e}provideCodeActions(e,t,n,r){if(!n.markers.length)return;let i=bo.fromUri(String(e.uri)),a=this.compiler.interpretProject().getInfos().filter(e=>e.quickFixes?.length&&e.filepath.equals(i));if(!a.length)return;let o=[];for(let t of n.markers)for(let n of a){let r=Nb(e,n.nodeOrToken);if(!(r.startLineNumber!==t.startLineNumber||r.startColumn!==t.startColumn||r.endLineNumber!==t.endLineNumber||r.endColumn!==t.endColumn))for(let r of n.quickFixes??[]){let n=this.quickFixToCodeAction(r,e,t);n&&o.push(n)}}let s=Pa(o,e=>{let t=e.diagnostics?.[0];return`${e.title}@${t?.startLineNumber}:${t?.startColumn}`});if(s.length)return{actions:s,dispose(){}}}quickFixToCodeAction(e,t,n){let r=bo.fromUri(String(t.uri));if(!e.filepath.equals(r))return;let i=bb.parse(e.filepath.toUri({protocol:t.uri.scheme})),a={edits:e.edits.map(e=>{let n=t.getPositionAt(e.start),r=t.getPositionAt(e.end);return{resource:i,textEdit:{range:{startLineNumber:n.lineNumber,startColumn:n.column,endLineNumber:r.lineNumber,endColumn:r.column},text:e.newText},versionId:t.getVersionId()}})};return{title:e.title,kind:`quickfix`,edit:a,diagnostics:[n]}}},Fb=class{constructor(e){this.compiler=e}provideDefinition(e,n){let{uri:r}=e,i=bo.fromUri(String(e.uri)),a=Mb(e,n),o=[...this.compiler.container.stack(i,a)];for(;o.length!==0;){let e=o.pop();if(!e)continue;let n=this.compiler.nodeReferee(e).getFiltered(t);if(!n)continue;let i;if(n.declaration&&[fm.PRIMARY_EXPRESSION,fm.VARIABLE].includes(e.kind)&&({declaration:i}=n),i){let{startPos:e,endPos:t}=i,n=r;return r&&i.filepath&&(n=bb.parse(i.filepath.toUri({protocol:r.scheme}))),[{range:{startColumn:e.column+1,startLineNumber:e.line+1,endColumn:t.column+1,endLineNumber:t.line+1},uri:n}]}}return[]}},Ib=class{constructor(e){this.compiler=e}provideDiagnostics(e){return[...this.provideErrors(e),...this.provideWarnings(e),...this.provideInfos(e)]}provideErrors(e){return e?this.compiler.parse.errors(e).map(e=>this.createDiagnostic(e,`error`)):this.compiler.interpretProject().getErrors().map(e=>this.createDiagnostic(e,`error`))}provideWarnings(e){return e?this.compiler.parse.warnings(e).map(e=>this.createDiagnostic(e,`warning`)):this.compiler.interpretProject().getWarnings().map(e=>this.createDiagnostic(e,`warning`))}provideInfos(e){return e?this.compiler.interpretFile(e).getInfos().map(e=>this.createDiagnostic(e,`info`)):this.compiler.interpretProject().getInfos().map(e=>this.createDiagnostic(e,`info`))}provideMarkers(e){return this.provideDiagnostics(e).filter(t=>t.filepath.equals(e)).map(e=>({severity:this.getSeverityValue(e.type),message:e.text,startLineNumber:e.startRow,startColumn:e.startColumn,endLineNumber:e.endRow,endColumn:e.endColumn,code:e.code?String(e.code):void 0}))}createDiagnostic(e,t){let n=e.nodeOrToken,r=n.startPos,i=n.endPos,a=e.details,o=a?.quickFixes?.filter(e=>e.edits.length>0).map(e=>({title:e.title,shortTitle:e.shortTitle,edits:e.edits.map(e=>({start:e.start,end:e.end,newText:e.newText})),isPreferred:e.isPreferred}));return{type:t,text:e.diagnostic,startRow:r.line+1,startColumn:r.column+1,endRow:i.line+1,endColumn:i.column+1,code:e.code,category:a?.category,explanation:a?.explanation,filepath:e.filepath,quickFixes:o}}getSeverityValue(e){return e===`error`?jb.Error:e===`warning`?jb.Warning:jb.Info}},Lb=class{constructor(e){this.compiler=e}provideReferences(e,n){let{uri:r}=e,i=bo.fromUri(String(r)),a=Mb(e,n);this.compiler.bindProject();let o=[...this.compiler.container.stack(i,a)];for(;o.length!==0;){let e=o.pop();if(e&&[fm.ELEMENT_DECLARATION,fm.FUNCTION_APPLICATION,fm.PRIMARY_EXPRESSION].includes(e?.kind)){let n=this.compiler.nodeSymbol(e).getFiltered(t)??this.compiler.nodeReferee(e).getFiltered(t),i=n?this.compiler.symbolReferences(n):void 0;if(i&&i.length>0)return i.map(e=>{let t=r;return r&&e.filepath&&(t=bb.parse(e.filepath.toUri({protocol:r.scheme}))),{range:{startColumn:e.startPos.column+1,startLineNumber:e.startPos.line+1,endColumn:e.endPos.column+1,endLineNumber:e.endPos.line+1},uri:t}})}}return[]}};function Rb(e,t){let n=[],r=e.parseFile(t).getValue()?.ast;if(!r)return n;for(let e of r.uses){let t=e.importPath?.value??``,r=[];if(e.specifiers instanceof hm)for(let t of e.specifiers.specifiers){let e,n;if(t.name?(e=t.importKind?.value,n=P(t.name)):t.importKind&&(n=t.importKind.value??void 0),n){let i=t.alias?P(t.alias):void 0;r.push({kind:e,name:n,alias:i})}}else e.specifiers instanceof Tm&&r.push({name:`*`});r.length===0&&!(e.specifiers instanceof Tm)||n.push({startOffset:e.fullStart,endOffset:e.fullEnd,sourceFile:t,specifiers:r,node:e})}return n}function zb(e,t,n,r,i,a){let o=Rb(e,i),s=r.relativeTo(i.dirname).replace(/\.dbml$/,``),c=o.findIndex(e=>e.sourceFile===s),l=`${n} ${t}`;if(c!==-1){let e=o[c];return e.specifiers.some(e=>e.name===t)?{topInsert:``}:{topInsert:Bb(Na([...e.specifiers.filter(e=>e.name!==`*`).map(e=>`${e.kind??n} ${e.name}${e.alias?` as ${e.alias}`:``}`),l]),s,`
@@ -9502,14 +9502,27 @@ function a_(e, n, r, i, a) {
9502
9502
  }
9503
9503
  //#endregion
9504
9504
  //#region src/core/global_modules/ref/interpret.ts
9505
- var o_ = { [i.Color]: {
9506
- isValidBuiltinFieldValue: Pf,
9507
- message: "'color' must be a color literal",
9508
- assignBuiltinField(e, t) {
9509
- e.color = t;
9505
+ var o_ = {
9506
+ [i.Color]: {
9507
+ isValidBuiltinFieldValue: Pf,
9508
+ message: "'color' must be a color literal",
9509
+ assignBuiltinField(e, t) {
9510
+ e.color = t;
9511
+ }
9512
+ },
9513
+ [i.Note]: {
9514
+ isValidBuiltinFieldValue: D,
9515
+ message: "'note' must be a string literal",
9516
+ assignBuiltinField(e, t, n) {
9517
+ e.note = {
9518
+ value: t,
9519
+ token: n
9520
+ };
9521
+ }
9510
9522
  }
9511
- } }, s_ = [
9523
+ }, s_ = [
9512
9524
  i.Color,
9525
+ i.Note,
9513
9526
  i.Delete,
9514
9527
  i.Update,
9515
9528
  i.Inactive
@@ -9530,40 +9543,45 @@ var o_ = { [i.Color]: {
9530
9543
  return this.ref.name = t.pop() || null, t.length > 1 && e.push(new T(w.UNSUPPORTED, "Nested schema is not supported", this.declarationNode.name)), this.ref.schemaName = t.join(".") || null, e;
9531
9544
  }
9532
9545
  interpretBody() {
9533
- let e = this.metadata.op(this.compiler), t = this.metadata.leftColumns(this.compiler), n = this.metadata.leftTable(this.compiler), r = this.metadata.rightColumns(this.compiler), i = this.metadata.rightTable(this.compiler);
9546
+ let e = this.metadata.op(this.compiler), t = this.metadata.leftColumns(this.compiler), n = this.metadata.leftTable(this.compiler), r = this.metadata.rightColumns(this.compiler), a = this.metadata.rightTable(this.compiler);
9534
9547
  if (La(t, r).every(([e, t]) => e?.originalSymbol === t?.originalSymbol)) return [new T(w.SAME_ENDPOINT, "Two endpoints are the same", this.declarationNode)];
9535
- let a = uf(e), o = n?.interpretedName(this.compiler, this.filepath), s = i?.interpretedName(this.compiler, this.filepath), c = F(this.metadata.leftToken()), l = F(this.metadata.rightToken());
9548
+ let o = uf(e), s = n?.interpretedName(this.compiler, this.filepath), c = a?.interpretedName(this.compiler, this.filepath), l = F(this.metadata.leftToken()), u = F(this.metadata.rightToken());
9536
9549
  if (this.ref.endpoints = this.declarationNode instanceof z ? [{
9537
- schemaName: o?.schema ?? null,
9538
- tableName: o?.name ?? "",
9539
- fieldNames: t.map((e) => e.name ?? ""),
9540
- relation: a[0],
9541
- token: c
9542
- }, {
9543
9550
  schemaName: s?.schema ?? null,
9544
9551
  tableName: s?.name ?? "",
9545
- fieldNames: r.map((e) => e.name ?? ""),
9546
- relation: a[1],
9552
+ fieldNames: t.map((e) => e.name ?? ""),
9553
+ relation: o[0],
9547
9554
  token: l
9555
+ }, {
9556
+ schemaName: c?.schema ?? null,
9557
+ tableName: c?.name ?? "",
9558
+ fieldNames: r.map((e) => e.name ?? ""),
9559
+ relation: o[1],
9560
+ token: u
9548
9561
  }] : [{
9549
- schemaName: s?.schema ?? null,
9550
- tableName: s?.name ?? "",
9562
+ schemaName: c?.schema ?? null,
9563
+ tableName: c?.name ?? "",
9551
9564
  fieldNames: r.map((e) => e.name ?? ""),
9552
- relation: a[1],
9553
- token: l
9565
+ relation: o[1],
9566
+ token: u
9554
9567
  }, {
9555
- schemaName: o?.schema ?? null,
9556
- tableName: o?.name ?? "",
9568
+ schemaName: s?.schema ?? null,
9569
+ tableName: s?.name ?? "",
9557
9570
  fieldNames: t.map((e) => e.name ?? ""),
9558
- relation: a[0],
9559
- token: c
9571
+ relation: o[0],
9572
+ token: l
9560
9573
  }], !(this.declarationNode instanceof z)) return [];
9561
- let u = cp(this.declarationNode)[0];
9562
- if (u.args[0]) {
9563
- let e = E(u.args[0]).getValue(), t = e.delete?.at(0)?.value;
9574
+ let d = cp(this.declarationNode)[0];
9575
+ if (d.args[0]) {
9576
+ let e = E(d.args[0]).getValue(), t = e.delete?.at(0)?.value;
9564
9577
  this.ref.onDelete = t instanceof gm ? ap(t) : P(t);
9565
9578
  let n = e.update?.at(0)?.value;
9566
- this.ref.onUpdate = n instanceof gm ? ap(n) : P(n), this.ref.color = e.color?.length ? Fp(e.color?.at(0)?.value) : void 0, this.ref.inactive = e.inactive?.length ? !0 : void 0, this.ref.metadata = Rp(e, s_);
9579
+ this.ref.onUpdate = n instanceof gm ? ap(n) : P(n), this.ref.color = e.color?.length ? Fp(e.color?.at(0)?.value) : void 0;
9580
+ let [r] = e[i.Note] || [];
9581
+ this.ref.note = r && {
9582
+ value: Np(N(r?.value)),
9583
+ token: F(r)
9584
+ }, this.ref.inactive = e.inactive?.length ? !0 : void 0, this.ref.metadata = Rp(e, s_);
9567
9585
  }
9568
9586
  return [];
9569
9587
  }
@@ -3,7 +3,7 @@ import { RefMetadata, SettingName, Filepath } from '../../types';
3
3
  import { default as Compiler } from '../../../compiler';
4
4
  import { default as Report } from '../../types/report';
5
5
  import { MetadataFieldRegistry } from '../metadata/utils';
6
- export declare const REF_METADATA_FIELDS: MetadataFieldRegistry<Ref, SettingName.Color>;
6
+ export declare const REF_METADATA_FIELDS: MetadataFieldRegistry<Ref, SettingName.Color | SettingName.Note>;
7
7
  export declare class RefInterpreter {
8
8
  private compiler;
9
9
  private metadata;
@@ -164,6 +164,10 @@ export interface Ref {
164
164
  name: string | null;
165
165
  endpoints: RefEndpointPair;
166
166
  color?: Color;
167
+ note?: {
168
+ value: string;
169
+ token: TokenPosition;
170
+ };
167
171
  onDelete?: string;
168
172
  onUpdate?: string;
169
173
  inactive?: boolean;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@dbml/parse",
4
- "version": "10.4.0-alpha.0",
4
+ "version": "10.4.0-alpha.1",
5
5
  "description": "DBML parser v2",
6
6
  "author": "Holistics <dev@holistics.io>",
7
7
  "license": "Apache-2.0",
@@ -51,5 +51,5 @@
51
51
  "engines": {
52
52
  "node": ">=18"
53
53
  },
54
- "gitHead": "ed4c390c2ce94cb9e73e3e84610917e1f15527ff"
54
+ "gitHead": "2abacb40597047ca589b2c18117f57f8fe3978e6"
55
55
  }