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