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