var j=Object.defineProperty;var g=(s,e,t)=>e in s?j(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t;var i=(s,e,t)=>(g(s,typeof e!="symbol"?e+"":e,t),t);import{C as c}from"./gateway.a36b7b3a.js";import{A as m}from"./activeRecord.d87d7ef2.js";import"./outputWidgets.f120b477.js";import{n as l}from"./string.c4e71dd1.js";(function(){try{var s=typeof window<"u"?window:typeof global<"u"?global:typeof self<"u"?self:{},e=new Error().stack;e&&(s._sentryDebugIds=s._sentryDebugIds||{},s._sentryDebugIds[e]="d8a9dccf-795d-44b9-b3f1-52404d2f5607",s._sentryDebugIdIdentifier="sentry-dbid-d8a9dccf-795d-44b9-b3f1-52404d2f5607")}catch{}})();class y{async create(e){return c.post(`projects/${e.projectId}/tables/${e.tableId}/columns`,e)}async delete(e){return c.delete(`projects/${e.projectId}/tables/${e.tableId}/columns/${e.id}`)}async update(e,t){return c.patch(`projects/${e.projectId}/tables/${e.tableId}/columns/${e.id}`,t)}}const u=new y,I=class{constructor(e){i(this,"record");this.record=m.create(u,e,"id")}static async create(e,t,r,o,p,d){const b=await u.create({name:e,type:t,nullable:r,unique:o,tableId:p,projectId:d});return"error"in b?b:new I(b)}async update(e){if(Object.keys(e).length===0||!this.id)return{success:!0,error:""};const t=await u.update({id:this.id,tableId:this.tableId,projectId:this.projectId},e);return t.error?{success:!1,error:t.error}:{success:!0,error:""}}toDTO(){return this.record.state}get id(){return this.record.get("id")}get tableId(){return this.record.get("tableId")}get projectId(){return this.record.get("projectId")}get protected(){return this.record.get("protected")}async delete(){this.id&&u.delete({id:this.id,tableId:this.tableId,projectId:this.projectId})}};let a=I;i(a,"fromDTO",e=>new I(e));class f{async list(e){return c.get(`projects/${e}/tables`)}async create(e,t){return await c.post(`projects/${e.projectId}/tables`,t)}async get(e){return c.get(`projects/${e.projectId}/tables/${e.tableId}`)}async delete(e){return c.delete(`projects/${e.projectId}/tables/${e.tableId}`)}async selectRow(e){return c.get(`projects/${e.projectId}/tables/${e.tableId}/rows`)}async update(e,t){return c.patch(`projects/${e.projectId}/tables/${e.tableId}`,t)}async insertRow(e,t){return c.post(`projects/${e.projectId}/tables/${e.tableId}/rows`,t)}async updateRow(e,t){return c.patch(`projects/${e.projectId}/tables/${e.tableId}/rows/${e.rowId}`,t)}async deleteRow(e){return c.delete(`projects/${e.projectId}/tables/${e.tableId}/rows/${e.rowId}`)}}const n=new f;class h{constructor(e,t=null){i(this,"record");i(this,"columns");this.record=m.create(n,e,"id"),this.columns=t}static async list(e){return(await n.list(e)).map(r=>new h(r))}static async create(e,t){const r=l(t,!1),o=await n.create({projectId:e},{name:r});return new h(o.table,o.columns.map(d=>a.fromDTO(d)))}static async get(e,t){const r=await n.get({projectId:e,tableId:t}),o=r.table,p=r.columns.map(d=>a.fromDTO({...d,projectId:o.projectId}));return new h(o,p)}async delete(e,t){return n.delete({projectId:e,tableId:t})}fixTraillingName(){this.name=l(this.name,!1)}async save(){if(Object.keys(this.record.changes).length!==0){this.record.changes.name&&this.fixTraillingName();try{await n.update({id:this.id,tableId:this.id,projectId:this.projectId},this.record.changes)}finally{this.record.resetChanges()}}}resetChanges(){this.record.resetChanges()}onUpdate(e){this.record.pubsub.subscribe("update",e)}hasChanges(){return this.record.hasChanges()}hasChangesDeep(e){return this.record.hasChangesDeep(e)&&l(this.name,!1)!==this.record.initialState.name}getColumns(){var e,t;return(t=(e=this.columns)==null?void 0:e.map(r=>r.toDTO()))!=null?t:[]}getUnprotectedColumns(){var e,t;return(t=(e=this.columns)==null?void 0:e.filter(r=>!r.protected).map(r=>r.toDTO()))!=null?t:[]}get id(){return this.record.get("id")}get name(){return this.record.get("name")}set name(e){const t=l(e,!0);this.record.set("name",t)}get projectId(){return this.record.get("projectId")}async addColumn(e){const t=await a.create(e.name,e.type,e.nullable,e.unique,this.id,this.projectId);return"error"in t?{success:!1,error:t.error}:this.columns?(this.columns.push(t),{success:!0,error:""}):(this.columns=[t],{success:!0,error:""})}getColumn(e){var t;return(t=this.columns)==null?void 0:t.find(r=>r.id&&r.id===e)}async select(e={}){const t=Object.keys(e).map(r=>`${r} = :${r}`).join(" AND ");return n.selectRow({name:this.name,where:t,params:e,tableId:this.id,projectId:this.projectId})}async insertRow(e){return n.insertRow({tableId:this.id,projectId:this.projectId},e)}async updateRow(e,t){return n.updateRow({tableId:this.id,projectId:this.projectId,rowId:e},t)}async deleteRow(e){return n.deleteRow({tableId:this.id,projectId:this.projectId,rowId:e})}}export{h as T};
//# sourceMappingURL=tables.bf4afdea.js.map
