/*! Built with http://stenciljs.com */
const{h:t}=window.webcomponents;import{a as e,b as s,c as i,d as n}from"./chunk-d76c3657.js";var a=function(t,e,s,i){return new(s||(s=Promise))(function(n,a){function o(t){try{h(i.next(t))}catch(t){a(t)}}function r(t){try{h(i.throw(t))}catch(t){a(t)}}function h(t){t.done?n(t.value):new s(function(e){e(t.value)}).then(o,r)}h((i=i.apply(t,e||[])).next())})};class o{constructor(){this.authenticated=!1,this.dev=!1,this.errorMessage="",this.api=new e}init(t){return a(this,void 0,void 0,function*(){let e=this.config=this.applications[t];this.id=t,this.api.init({host:e.suitecrm.PROD_URL+e.suitecrm.API_URL,headers:e.api_headers,routes:e.api_routes}),this.api.requestCredentials=e.requestCredentials,this.api.corsMode=e.corsMode,this.dev&&(this.api.baseUrl=e.suitecrm.DEV_URL+e.suitecrm.API_URL),yield this.api.get(this.api.route("dwp/currentUser")).then(t=>t.statusText&&"Unauthorized"===t.statusText?(this.authenticated=!1,t):(this.authenticated=!0,"undefined"!=typeof window&&window.localStorage&&window.localStorage.setItem("preferred_language",t.data.preferred_language),t))})}login(t){return a(this,void 0,void 0,function*(){let e=t.detail;return yield this.api.post(this.api.route("dwp/login"),e).then(t=>{t.status>=400||t.message&&"credentials do not meet the requirements"===t.message?this.onLoginFailure(t):this.onLoginSucces(t)})})}logout(){return a(this,void 0,void 0,function*(){return yield this.api.post("logout",{}).then(()=>{this.authenticated=!1})})}onLoginSucces(t){this.errorMessage="",this.authenticated=!0}onLoginFailure(t){t.message&&"credentials do not meet the requirements"===t.message&&(this.errorMessage=t.message),t.flashMessages&&t.flashMessages.length&&(this.errorMessage=t.flashMessages[0].text),t.statusText&&(this.errorMessage=t.statusText),console.error("loginFailure: ",this.errorMessage),this.authenticated=!1}get applications(){return{"e-be":{suitecrm:{DEV_URL:"http://localhost:9005",PROD_URL:"http://localhost:9005",API_URL:"/Api/V8_Custom/",IDP_URL:"/Api/V8_Custom/idp.php"},requestCredentials:"same-origin",corsMode:"cors",swaggerUrl:"",api_headers:[{"Content-Type":"application/json;charset=utf-8"}],api_routes:[{"dwp/login":"login"},{"dwp/currentUser":"user/current"},{"dwp/Menu/start":"Menu/start"}],api_routes_dev:[{case:"/v1/case"}]},"e-nl":{suitecrm:{DEV_URL:"http://localhost:8585",PROD_URL:"http://localhost:8585",API_URL:"/essent-crm-osgi/rest/"},requestCredentials:"same-origin",corsMode:"no-cors",swaggerUrl:"api-docs/uaasApi",api_headers:[{"Content-Type":"application/json;charset=utf-8"}],api_routes:[{"dwp/login":"dwp/login"},{"dwp/currentUser":"dwp/currentUser"},{"dwp/Menu/start":"dwp/Menu/start"}],api_routes_dev:[{case:"/v1/case"}]}}}}const r=Symbol(),h=Symbol();class c extends o{constructor(t){if(super(),t!==h)throw new Error("Cannot construct singleton");this._type="UaasService"}static get instance(){return this[r]||(this[r]=new c(h)),this[r]}get type(){return this._type}set type(t){this._type=t}}var u=function(t,e,s,i){return new(s||(s=Promise))(function(n,a){function o(t){try{h(i.next(t))}catch(t){a(t)}}function r(t){try{h(i.throw(t))}catch(t){a(t)}}function h(t){t.done?n(t.value):new s(function(e){e(t.value)}).then(o,r)}h((i=i.apply(t,e||[])).next())})};const d=Symbol(),l=Symbol();class p{constructor(t){if(this.baseUrl="",this.loading=!0,this.authenticated=!1,this.errorMessage="",t!==l)throw new Error("Cannot construct singleton");this._type="AppService",this.uaas=c.instance,this.config=s.instance,this.config.setDefaults(i)}init(t){return u(this,void 0,void 0,function*(){return yield this.config.fetch(this.baseUrl+"/assets/config.json"),this.baseUrl=t,this.data={},this.data.manifest=yield this.fetchJson(this.baseUrl+"/assets/collection-manifest.json"),this.data.mockData=yield this.fetchJson(this.baseUrl+"/assets/data/mock-data.json"),this.data.dwpData=yield this.fetchJson(this.baseUrl+"/assets/data/mock-dwp.json"),this.data.swaggerData=yield this.fetchJson(this.baseUrl+"/assets/data/uaasApiSwagger.json"),this.collectionManifest=this.data.manifest,this.parseComponents(),this.initHistory(),document.location.hostname.match("localhost")&&this.config.set("dev",!0),this.data})}initHistory(){"undefined"!=typeof window&&window.history}parseComponents(){let t=[];n.copy(this.data.manifest.components).map(e=>{let s=e.tag,i=!1;this.config.get("componentsIgnorePatterns").forEach(t=>{s.match(t)&&(i=!0)}),i||t.push(e)}),this.components=t}fetchJson(t){return u(this,void 0,void 0,function*(){return yield(yield fetch(t)).json()})}showFlashMessage(t,e,s=2e3){document.querySelector("ema-flash-message").show(t,e,s)}static get instance(){return this[d]||(this[d]=new p(l)),this[d]}get type(){return this._type}set type(t){this._type=t}}export{p as a};