"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@babel/runtime/helpers/toConsumableArray"),t=require("@babel/runtime/helpers/defineProperty"),r=require("@babel/runtime/helpers/asyncToGenerator"),n=require("@babel/runtime/regenerator"),a=require("react"),i=require("lodash"),s=require("../../types.js"),u=require("../../utils.js"),c=require("../utils.js"),o=require("./sharedHooks.js"),p=require("../../../../clients/jsonApi/index.js");function l(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var d=l(e),f=l(t),b=l(r),m=l(n);function g(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function v(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?g(Object(r),!0).forEach((function(t){f.default(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):g(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}exports.useSetTemplateData=function(){var e=o.useGet("get"),t=o.useGet("set"),r=a.useCallback(function(){var r=b.default(m.default.mark((function r(n,a){return m.default.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return t({templateSettings:a}),r.prev=1,r.next=4,p.patchAppSettingsTemplate(e().appKey,n.id,{settings:a});case 4:r.next=9;break;case 6:r.prev=6,r.t0=r.catch(1),console.error(r.t0);case 9:case"end":return r.stop()}}),r,null,[[1,6]])})));return function(e,t){return r.apply(this,arguments)}}(),[e,t]),n=a.useMemo((function(){return i.debounce(r,200)}),[r]),l=a.useCallback(function(){var r=b.default(m.default.mark((function r(n,a){var i,s,o,l,f,b;return m.default.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return i=e(),s=i.appKey,r.t0=c.dataFormatter,r.next=4,p.getAppSettingsTemplates(s,{ownership:"own"});case 4:if(r.t1=r.sent,o=r.t0.deserialize.call(r.t0,r.t1),!(l=o.find((function(e){var t;return u.isPresetOverride(e)&&(null===(t=e.metadata)||void 0===t?void 0:t.parentTemplateId)===n.id})))){r.next=15;break}return f=!!c.findTemplate(e,l.id),b=v(v({},l),{},{settings:a}),t((function(e){var t=e.loadedTemplates;return{loadedTemplates:f?t.map((function(e){return e.id===b.id?b:e})):[].concat(d.default(t),[b])}})),r.next=13,p.patchAppSettingsTemplate(s,l.id,{settings:a});case 13:r.next=17;break;case 15:return r.next=17,c.addTemplate(e,t,{name:"### INTERNAL OVERRIDE ### id=".concat(n.id),settings:a,metadata:{isPresetOverride:!0,parentTemplateId:n.id}});case 17:case"end":return r.stop()}}),r)})));return function(e,t){return r.apply(this,arguments)}}(),[e,t]),f=a.useMemo((function(){return i.debounce(l,200)}),[l]);return a.useCallback(function(){var r=b.default(m.default.mark((function r(a,i){var o,p,l,d,b,g;return m.default.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:if(o=c.findTemplate(e,a)){r.next=5;break}t({currentTemplateId:a,templateSettings:i}),r.next=17;break;case 5:if(i){r.next=8;break}return console.error("`templateSettings` is undefined but required when changing `templateId`"),r.abrupt("return");case 8:if(t({currentTemplateId:a}),p=e(),l=p.dataEqualityFn,p.isInitialized){r.next=12;break}return r.abrupt("return");case 12:return r.next=14,c.ensureTemplateSettings(e,t,o);case 14:d=r.sent,!l(d,v(v({},d),{},{settings:i}))&&(b=c.setBusy(t,o.id,s.Action.SaveSettings),t({templateSettings:i}),u.isOwnTemplate(o)?(g=c.getUpdatedTemplates(e,a,{settings:i}),t({loadedTemplates:g}),n(o,i)):d.isPreset&&f(d,i),b());case 17:case"end":return r.stop()}}),r)})));return function(e,t){return r.apply(this,arguments)}}(),[n,f,e,t])};
//# sourceMappingURL=useSetTemplateData.js.map
