/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/odoo-await@3.4.1/lib/index.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
const xmlrpc=require("xmlrpc");class OdooAwait{constructor(e){let r={baseUrl:"http://localhost",port:void 0,db:"odoo_db",username:"admin",password:"admin"};const t=e?Object.assign(r,e):r,a=new URL(t.baseUrl),i=""!==a.username?{user:a.username,pass:a.password}:null;this.host=a.hostname,this.secure="https:"===a.protocol,this.port=t.port,this.basicAuth=i,this.db=t.db,this.username=t.username,this.password=t.password,this.uid=0,this.port||(this.port=a.port),this.port||(this.secure?this.port=443:this.port=80)}createClientOptions(e){return{host:this.host,port:this.port,basic_auth:this.basicAuth,path:e}}connect(){let e,r=this;const t=this.createClientOptions("/xmlrpc/2/common");e=this.secure?xmlrpc.createSecureClient(t):xmlrpc.createClient(t);let a=[];return a.push(this.db,this.username,this.password),a.push({}),new Promise(((t,i)=>{e.methodCall("authenticate",a,(function(e,a){e?i(e):a?(r.uid=a,t(a)):i("Error connecting to database. This is probably due to invalid credentials.")}))}))}execute_kw(e,r,t){const a=this.createClientOptions("/xmlrpc/2/object");let i=this.secure?xmlrpc.createSecureClient(a):xmlrpc.createClient(a);return t.unshift(this.db,this.uid,this.password,e,r),new Promise(((e,r)=>{i.methodCall("execute_kw",t,(function(t,a){t?r(t):e(a)}))}))}parseManyFields(e){for(const[r,t]of Object.entries(e))if(t&&t.action)switch(t.action){case"create":if(!t.value)throw new Error("'create' action requires a value object or an array of value objects");e[r]=Array.isArray(t.value)?t.value.map((e=>[0,0,e])):[[0,0,t.value]];break;case"update":if(!t.id||!t.value)throw new Error("'update' action requires both an ID number and a value object");e[r]=[[1,t.id,t.value]];break;case"add":if(!t.id)throw new Error("'add' action requires an ID or array of ID's to add to add to the set");e[r]=Array.isArray(t.id)?t.id.map((e=>[4,e,0])):[[4,t.id,0]];break;case"remove":if(!t.id)throw new Error("'remove' action requires an ID or array of ID's to remove from the set");e[r]=Array.isArray(t.id)?t.id.map((e=>[3,e,0])):[[3,t.id,0]];break;case"delete":if(!t.id)throw new Error("'delete' action requires an ID or array of ID's to delete");e[r]=Array.isArray(t.id)?t.id.map((e=>[2,e,0])):[[2,t.id,0]];break;case"clear":e[r]=[[5,0,0]];break;case"replace":if(!t.id)throw new Error("'replace' action requires an ID or array of ID's to add to the set");Array.isArray(t.id)||(t.id=[t.id]),e[r]=[[6,0,t.id]]}return e}async create(e,r={},t,a){r=this.parseManyFields(r);const i=await this.execute_kw(e,"create",[[r]]);return t&&await this.createExternalId(e,i,t,a||"__api__"),i}async read(e,r,t=[]){let a;return a=Array.isArray(r)?[[r]]:[[[r]]],t.length&&a.push({fields:t}),await this.execute_kw(e,"read",a)}async update(e,r,t={}){return t=this.parseManyFields(t),await this.execute_kw(e,"write",[[r,t]])}async delete(e,r){return await this.execute_kw(e,"unlink",[[r]]),!0}async searchRead(e,r,t=[],a={offset:0,limit:0,order:"",context:{}}){let i=[];if(r)if(Array.isArray(r)&&"string"==typeof r[0]&&!["&","|","!"].includes(r[0]))i=[r];else if(Array.isArray(r)&&(Array.isArray(r[0])||["&","|","!"].includes(r[0])))i=r;else for(let[e,t]of Object.entries(r))i.push([e,"=",t]);let s=[];return s.push([i]),s.push({fields:t,offset:a.offset,limit:a.limit,order:a.order,context:a.context}),await this.execute_kw(e,"search_read",s)}async search(e,r){let t=[];if(r)if(Array.isArray(r)&&"string"==typeof r[0]&&!["&","|","!"].includes(r[0]))t=[r];else if(Array.isArray(r)&&(Array.isArray(r[0])||["&","|","!"].includes(r[0])))t=r;else for(const[e,a]of Object.entries(r))t.push([e,"=",a]);let a=[];return a.push([t]),await this.execute_kw(e,"search",a)}async getFields(e,r=[]){return await this.execute_kw(e,"fields_get",[[],{attributes:r}])}async action(e,r,t){return await this.execute_kw(e,r,[[t]])}async createExternalId(e,r,t,a){return await this.execute_kw("ir.model.data","create",[[{model:e,name:`${t}`,res_id:r,module:a||"__api__"}]])}async searchByExternalId(e){const r=await this.searchRead("ir.model.data",{name:e},["res_id"]);if(!r.length)throw new Error(`No matching record found for external identifier ${e}`);return r[0].res_id}async readByExternalId(e,r=[]){const t=await this.searchRead("ir.model.data",{name:e},["res_id","model"]);if(!t.length)throw new Error(`No matching record found for external identifier ${e}`);return(await this.read(t[0].model,[t[0].res_id],r))[0]}async updateByExternalId(e,r={}){const t=await this.searchRead("ir.model.data",{name:e},["res_id","model"]);if(!t.length)throw new Error(`No matching record found for external identifier ${e}`);return await this.update(t[0].model,t[0].res_id,r)}async deleteByExternalId(e){const r=await this.searchRead("ir.model.data",{name:e},["res_id","model"]);if(!r.length)throw new Error(`No matching record found for external ID ${e}`);return await this.delete(r[0].model,r[0].res_id)}}module.exports=OdooAwait;
//# sourceMappingURL=/sm/34cd737223bf1c3f1f9c2a658dcb0fc0cce3d437f2b51598a5c66f019504aef5.map