"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require('./../../../ext-esm/tslib/tslib.es6.js'),r=require("lodash"),t=require("react"),n=require("../../../clients/index.js"),s=require("../configuration/constants.js"),u=function(e){return n.corvaAPI.get("/v2/users/".concat(e,"/settings"),{})},i=function(e,r){return n.corvaAPI.post("/v2/users/".concat(e,"/settings"),r)},a=function(e,r){return n.corvaAPI.get("/v2/users/".concat(e,"/settings/").concat(r),{})};exports.useGradientStorage=function(n){var o=n.userId,c=n.storageKey,l=void 0===c?"gradient-manager-custom_gradients":c,d=n.onFetchError,f=n.onUpdateError,g=t.useState(null),v=g[0],_=g[1],E=t.useState(0),b=E[0],h=E[1],p=t.useState(!1),q=p[0],I=p[1],A=t.useCallback((function(e){r.isEqual(v,e)||_(e)}),[v]);t.useEffect((function(){e.__awaiter(void 0,void 0,void 0,(function(){var r,t;return e.__generator(this,(function(e){switch(e.label){case 0:h((function(e){return e+1})),e.label=1;case 1:return e.trys.push([1,3,4,5]),[4,a(o,l)];case 2:return r=e.sent(),_(r[l]||[]),[3,5];case 3:return t=e.sent(),null==d||d(t),console.error(t),[3,5];case 4:return h((function(e){return e-1})),[7];case 5:return[2]}}))}))}),[o]),t.useEffect((function(){if(v){e.__awaiter(void 0,void 0,void 0,(function(){var t,n,s,a;return e.__generator(this,(function(c){switch(c.label){case 0:h((function(e){return e+1})),c.label=1;case 1:return c.trys.push([1,5,6,7]),[4,u(o)];case 2:return t=c.sent(),r.isEqual(t[l],v)?[3,4]:(n=e.__assign(e.__assign({},t),((a={})[l]=v,a)),[4,i(o,n)]);case 3:c.sent(),c.label=4;case 4:return[3,7];case 5:return s=c.sent(),null==f||f(s),console.error(s),[3,7];case 6:return h((function(e){return e-1})),[7];case 7:return[2]}}))}))}}),[v]);var S=function(e){return r.find(v,{id:e})||r.find(s.DEFAULT_GRADIENTS,{id:e})},j=Boolean(b);return t.useMemo((function(){return{isEditing:q,isLoading:j,gradients:v,setIsEditing:I,setGradients:A,find:S}}),[q,j,v,A])};
