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