import r from"@babel/runtime/helpers/defineProperty";import e from"@babel/runtime/helpers/asyncToGenerator";import t from"@babel/runtime/helpers/slicedToArray";import n from"@babel/runtime/regenerator";import{isEqual as o,find as i}from"lodash";import{useState as c,useCallback as s,useEffect as u,useMemo as a}from"react";import{corvaAPI as f}from"../../../clients/index.js";import{DEFAULT_GRADIENTS as p}from"../configuration/constants.js";function l(r,e){var t=Object.keys(r);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(r);e&&(n=n.filter((function(e){return Object.getOwnPropertyDescriptor(r,e).enumerable}))),t.push.apply(t,n)}return t}function m(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?l(Object(n),!0).forEach((function(t){r(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):l(Object(n)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(n,r))}))}return e}var b=function(r){return f.get("/v2/users/".concat(r,"/settings"),{})},v=function(r,e){return f.post("/v2/users/".concat(r,"/settings"),e)},g=function(r,e){return f.get("/v2/users/".concat(r,"/settings/").concat(e),{})},d=function(f){var l=f.userId,d=f.storageKey,h=void 0===d?"gradient-manager-custom_gradients":d,y=f.onFetchError,O=f.onUpdateError,j=c(null),w=t(j,2),x=w[0],P=w[1],k=c(0),E=t(k,2),D=E[0],G=E[1],I=c(!1),S=t(I,2),T=S[0],A=S[1],B=s((function(r){o(x,r)||P(r)}),[x]);u((function(){var r=function(){var r=e(n.mark((function r(){var e;return n.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return G((function(r){return r+1})),r.prev=1,r.next=4,g(l,h);case 4:e=r.sent,P(e[h]||[]),r.next=12;break;case 8:r.prev=8,r.t0=r.catch(1),null==y||y(r.t0),console.error(r.t0);case 12:return r.prev=12,G((function(r){return r-1})),r.finish(12);case 15:case"end":return r.stop()}}),r,null,[[1,8,12,15]])})));return function(){return r.apply(this,arguments)}}();r()}),[l]),u((function(){if(x){var t=function(){var t=e(n.mark((function e(){var t,i;return n.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return G((function(r){return r+1})),e.prev=1,e.next=4,b(l);case 4:if(t=e.sent,o(t[h],x)){e.next=9;break}return i=m(m({},t),{},r({},h,x)),e.next=9,v(l,i);case 9:e.next=15;break;case 11:e.prev=11,e.t0=e.catch(1),null==O||O(e.t0),console.error(e.t0);case 15:return e.prev=15,G((function(r){return r-1})),e.finish(15);case 18:case"end":return e.stop()}}),e,null,[[1,11,15,18]])})));return function(){return t.apply(this,arguments)}}();t()}}),[x]);var F=function(r){return i(x,{id:r})||i(p,{id:r})},K=Boolean(D);return a((function(){return{isEditing:T,isLoading:K,gradients:null!=x?x:[],setIsEditing:A,setGradients:B,find:F}}),[T,K,x,B])};export{d as useGradientStorage};
//# sourceMappingURL=useGradientStorage.js.map
