"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("../node_modules/tslib/tslib.es6.js"),t=require("react"),r=require("../services/user.js"),n=require("../lib/avatars/avatars.js");require("../node_modules/react-redux/es/index.js");var s=require("../services/astproxy.js"),a=require("../lib/user/extensions.js"),o=require("../utils/genericFunctions/eventDispatch.js");require("../store/index.js");var u=require("../node_modules/react-redux/es/hooks/useSelector.js");require("../node_modules/react-redux/es/components/Context.js"),require("../node_modules/hoist-non-react-statics/dist/hoist-non-react-statics.cjs.js"),require("../node_modules/react-redux/node_modules/react-is/index.js");var c=require("../node_modules/react-redux/es/hooks/useDispatch.js");function i(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var l=i(t),d="phone_island_api_mode",f=function(e){try{var t=localStorage.getItem("".concat(d,"_").concat(e));if("new"===t||"legacy"===t)return t}catch(e){console.warn("Failed to read API mode from localStorage:",e)}return"unknown"},h=function(e,t){try{localStorage.setItem("".concat(d,"_").concat(e),t)}catch(e){console.warn("Failed to save API mode to localStorage:",e)}};exports.RestAPI=function(i){var d=i.hostName,p=i.username,_=i.authToken,v=i.children,g=c.useDispatch(),m=u.useSelector((function(e){return e.fetchDefaults})).fetchReady;return t.useEffect((function(){if(_&&d&&p){e.__awaiter(void 0,void 0,void 0,(function(){var t,r;return e.__generator(this,(function(e){switch(e.label){case 0:if("unknown"!==(t=f(p)))return[3,4];e.label=1;case 1:return e.trys.push([1,3,,4]),[4,fetch("https://".concat(d,"/api/user/me"),{headers:{Authorization:"Bearer ".concat(_)}})];case 2:if((r=e.sent()).ok)t="new",h(p,"new");else{if(404!==r.status&&401!==r.status)throw new Error("API test failed with status: ".concat(r.status));t="legacy",h(p,"legacy")}return[3,4];case 3:return e.sent(),t="legacy",h(p,"legacy"),[3,4];case 4:return"new"===t?(g.fetchDefaults.updateFetchBaseURL("https://".concat(d,"/api")),g.fetchDefaults.updateFetchHeaders({Authorization:"Bearer ".concat(_)})):(g.fetchDefaults.updateFetchBaseURL("https://".concat(d,"/webrest")),g.fetchDefaults.updateFetchHeaders({Authorization:"".concat(p,":").concat(_)})),g.fetchDefaults.setFetchReady(),[2]}}))}))}}),[_,d,p]),t.useEffect((function(){m&&(function(){e.__awaiter(this,void 0,void 0,(function(){var t;return e.__generator(this,(function(n){switch(n.label){case 0:return[4,r.getCurrentUserInfo()];case 1:return(t=n.sent())&&(g.currentUser.updateCurrentUser(t),o.eventDispatch("phone-island-user-informations-update",e.__assign({},t)),g.currentUser.setCurrentUserReady(!0),t.settings&&t.settings.open_param_url?g.paramUrl.setOpenParamUrlType(t.settings.open_param_url):g.paramUrl.setOpenParamUrlType("never"),function(){e.__awaiter(this,void 0,void 0,(function(){var t,r;return e.__generator(this,(function(e){switch(e.label){case 0:return[4,s.getAllExtensions()];case 1:return(t=e.sent())&&(g.users.updateExtensions(t),r=a.getExtensionsList(),Object.values(t).filter((function(e){return r.includes(e.exten)})).forEach((function(e){return g.currentUser.updateConversations(e)}))),[2]}}))}))}()),[2]}}))}))}(),function(){e.__awaiter(this,void 0,void 0,(function(){var t;return e.__generator(this,(function(e){switch(e.label){case 0:return[4,r.getAllUsersEndpoints()];case 1:return(t=e.sent())&&g.users.updateEndpoints(t),[2]}}))}))}(),function(){e.__awaiter(this,void 0,void 0,(function(){var t;return e.__generator(this,(function(e){switch(e.label){case 0:return[4,r.getVideoSources()];case 1:return(t=e.sent())&&g.streaming.updateVideoSources(t),[2]}}))}))}(),function(){e.__awaiter(this,void 0,void 0,(function(){var t,n;return e.__generator(this,(function(e){switch(e.label){case 0:return e.trys.push([0,2,,3]),[4,r.getFeatureCodes()];case 1:return(t=e.sent())&&g.currentUser.updateFeatureCodes(t),[3,3];case 2:return n=e.sent(),console.warn("Failed to fetch feature codes:",n),[3,3];case 3:return[2]}}))}))}())}),[m]),t.useLayoutEffect((function(){p&&m&&n.retrieveAvatars(p)}),[m,p]),l.default.createElement(l.default.Fragment,null,m&&v)},exports.getApiMode=function(e){return f(e)},exports.resetApiMode=function(e){try{localStorage.removeItem("".concat(d,"_").concat(e))}catch(e){console.warn("Failed to reset API mode in localStorage:",e)}};
//# sourceMappingURL=RestAPI.js.map
