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