import e from"@openremote/core";import{project as t,modal as o}from"../components/flow-editor";export class Exporter{flowToJson(e){return JSON.stringify(e)}jsonToFlow(e){return JSON.parse(e)}collectionToRuleset(e,t){return{lang:"FLOW",name:e.name,type:t,rules:this.flowToJson(e)}}rulesetToCollection(e){return this.jsonToFlow(e.rules)}exportAsNew(n){let s=this.flowToJson(n),l=e.rest.api.RulesResource,i={lang:"FLOW",name:n.name,type:"global",rules:s};l.createGlobalRuleset(i).then(e=>{200===e.status?(t.setCurrentProject(e.data,n.name,n.description),console.log("Successfully saved new ruleset")):(console.log("Something went wrong while saving NEW ruleset\nHTTP status "+e.status),o.notification("Failure",`Something went wrong while saving ${i.name}`))}).catch(e=>{console.log("Something went wrong while saving NEW ruleset\nHTTP status "+e),o.notification("Failure",`Something went wrong while saving ${i.name}`)})}exportAsExisting(n,s){let l=this.flowToJson(s),i=e.rest.api.RulesResource;i.getGlobalRuleset(t.existingFlowRuleId).then(e=>{let n=e.data;n.rules=l,i.updateGlobalRuleset(n.id,n).then(e=>{204===e.status?(t.unsavedState=!1,console.log("Successfully saved ruleset")):(console.log("Something went wrong while saving EXISTING ruleset\n"+e.status),o.notification("Failure",`Something went wrong while saving ${s.name}`))})}).catch(e=>{console.log("Something went wrong while saving EXISTING ruleset\n"+e),o.notification("Failure",`Something went wrong while saving ${s.name}`)})}}