import e from"@babel/runtime/helpers/toConsumableArray";import r from"@babel/runtime/helpers/asyncToGenerator";import t from"@babel/runtime/helpers/defineProperty";import n from"@babel/runtime/helpers/slicedToArray";import s from"@babel/runtime/regenerator";import{useState as i,useEffect as o}from"react";import{isEqual as a}from"lodash";import{dataFormatter as p,findTemplate as c,ensureTemplateSettings as m,processLoadedTemplates as l}from"../utils.js";import{perPage as u,baseFields as f}from"../Store.js";import{useGet as d}from"./sharedHooks.js";import{getAppSettingsTemplates as b}from"../../../../clients/jsonApi/index.js";import j from"../../../../permissions/usePermissions.js";import{PERMISSIONS as v}from"../../../../permissions/constants/index.js";import"../../../../permissions/withPermissionsHOC.js";import"../../../../permissions/ProvidePermissions.js";import"../../../../permissions/PermissionsContext.js";import"../../../../utils/index.js";import{showErrorNotification as g}from"../../../../utils/notificationToasts.js";function h(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function O(e){for(var r=1;r<arguments.length;r++){var n=null!=arguments[r]?arguments[r]:{};r%2?h(Object(n),!0).forEach((function(r){t(e,r,n[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):h(Object(n)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(n,r))}))}return e}var y=function(){var h=d("get"),y=d("set"),P=i(!1),x=n(P,2),w=x[0],k=x[1],T=i(),S=n(T,2),C=S[0],D=S[1],A={canPublish:v.userCreate},E=j(A);o((function(){var e=Object.entries(E).reduce((function(e,r){var s=n(r,2),i=s[0],o=s[1],a=o.loading,p=o.active;return{loaded:e.loaded&&!a,permissions:O(O({},e.permissions),{},t({},i,p))}}),{loaded:!0,permissions:{}}),r=e.loaded,s=e.permissions;r&&(y({permissions:s}),k(!0))}),[E,y]),o((function(){var e=function(){var e=r(s.mark((function e(){var r,t,n;return s.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,r=h(),t=r.appKey,e.t0=p,e.next=5,b(t,{per_page:u,fields:f});case 5:e.t1=e.sent,n=e.t0.deserialize.call(e.t0,e.t1),D(n),e.next=14;break;case 10:e.prev=10,e.t2=e.catch(0),g("Error loading app templates"),console.error(e.t2);case 14:case"end":return e.stop()}}),e,null,[[0,10]])})));return function(){return e.apply(this,arguments)}}();e()}),[h,y]),o((function(){var t=function(){var t=r(s.mark((function r(){var t,n,i,o,p,u,f,d,b,j,v;return s.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:if(w&&C){r.next=2;break}return r.abrupt("return");case 2:return t=h(),n=t.currentTemplateId,i=t.templateSettings,o=t.onChange,p=t.hooks.getDefaultTemplate,r.next=5,l(h,C);case 5:if(u=r.sent,y(u),f=c(h,n),!(d=(null==p?void 0:p([].concat(e(h().presetTemplates),e(h().loadedTemplates)),{currentTemplateId:n,templateSettings:i,event:"loadList"}))||f)){r.next=16;break}return r.next=12,m(h,y,d);case 12:b=r.sent,j=!f&&!!d,v=!a(b.settings,null==f?void 0:f.settings),(j||v)&&o&&(y({templateSettings:b.settings}),o(b,"initialLoad"));case 16:y({isInitialized:!0});case 17:case"end":return r.stop()}}),r)})));return function(){return t.apply(this,arguments)}}();t()}),[C,h,w,y])};export{y as useInitialize};
//# sourceMappingURL=useInitialize.js.map
