Liferay.Loader.define("dynamic-data-mapping-form-web@4.0.105/admin/js/hooks/useAutoSave.es",["module","exports","require","@liferay/frontend-js-react-web","data-engine-js-components-web","dynamic-data-mapping-form-web$object-hash","liferay!frontend-js-react-web$react","dynamic-data-mapping-form-web$react-router-dom","./useStateSync.es","./useValidateFormWithObjects"],(function(e,t,a){var r;window;{Object.defineProperty(t,"__esModule",{value:!0}),t.AutoSaveProvider=f,t.useAutoSave=function(){return(0,c.useContext)(e)},t.default=void 0;var n=a("@liferay/frontend-js-react-web"),o=a("data-engine-js-components-web"),u=(r=a("dynamic-data-mapping-form-web$object-hash"))&&r.__esModule?r:{default:r},c=function(e){if(e&&e.__esModule)return e;if(null===e||"object"!=typeof e&&"function"!=typeof e)return{default:e};var t=i();if(t&&t.has(e))return t.get(e);var a={},r=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var n in e)if(Object.prototype.hasOwnProperty.call(e,n)){var o=r?Object.getOwnPropertyDescriptor(e,n):null;o&&(o.get||o.set)?Object.defineProperty(a,n,o):a[n]=e[n]}a.default=e,t&&t.set(e,a);return a}(a("liferay!frontend-js-react-web$react")),s=a("dynamic-data-mapping-form-web$react-router-dom"),l=a("./useStateSync.es"),d=a("./useValidateFormWithObjects");function i(){if("function"!=typeof WeakMap)return null;var e=new WeakMap;return i=function(){return e},e}const e=c.default.createContext({});e.displayName="AutoSaveContext";const p=e=>(0,u.default)(e,{algorithm:"md5",unorderedObjects:!0}),g=({name:e,portletNamespace:t})=>{const a=document.querySelector("#".concat(t,"editForm")),r=new FormData(a);return r.append("".concat(t,"name"),JSON.stringify(e)),r.append("".concat(t,"saveAsDraft"),"true"),(0,o.convertToFormData)(r)},y=(e,t)=>{const a=document.querySelector("#".concat(e,"formInstanceId"));a&&"0"===a.value&&(a.value=t.formInstanceId);const r=document.querySelector("#".concat(e,"ddmStructureId"));r&&"0"===r.value&&(r.value=t.ddmStructureId)},b=({modifiedDate:e,portletNamespace:t})=>{document.querySelector("#".concat(t,"autosaveMessage")).innerHTML=o.StringUtils.sub(Liferay.Language.get("draft-x"),[e])},v=6e4;function f({children:t,interval:a,location:r,url:u}){const{portletNamespace:s}=(0,o.useConfig)(),{availableLanguageIds:i,defaultLanguageId:f,localizedDescription:m,localizedName:S,pages:h,paginationMode:j,rules:w,successPageSettings:I}=(0,o.useFormState)(),O=(0,l.useStateSync)(),M=(0,n.useIsMounted)(),P=(0,c.useRef)(null),D=(0,c.useRef)(null),F=(0,c.useRef)(null),L=(0,c.useRef)(null),k=(0,d.useValidateFormWithObjects)(),C=(0,c.useCallback)((()=>p({availableLanguageIds:i,defaultLanguageId:f,description:m,name:S,pages:h,paginationMode:j,rules:w,successPageSettings:I})),[i,f,m,S,h,j,w,I]),N=(0,c.useCallback)((()=>{const e=C();O();return("/"===r.pathname||"/rules"===r.pathname)&&k()&&(D.current=(0,o.makeFetch)({body:g({name:S,portletNamespace:s}),url:u}).then((t=>(D.current=null,y(s,t),F.current=e,b({modifiedDate:t.modifiedDate,portletNamespace:s}),t))).catch((e=>{D.current=null,console.error(e)}))),D.current}),[O,C,F,S,r,D,s,u,k]),$=(0,c.useCallback)((()=>F.current===C()),[F,C]),_=(0,c.useCallback)((()=>{M()&&(D.current?D.current.then((()=>_())).catch((e=>console.error(e))):$()||o.FormSupport.isEmpty(h)||N())}),[N,M,$,h,D]);return(0,c.useEffect)((()=>(a>0&&(P.current=setInterval((()=>_()),a*v)),()=>{P.current&&clearInterval(P.current)})),[P,a,_]),(0,c.useEffect)((()=>{F.current=C(),L.current=p(w)}),[]),(0,c.useEffect)((()=>{const e=p(w);L.current!==e&&(L.current=e,_())}),[L,w]),c.default.createElement(e.Provider,{value:{doSave:N,doSyncInput:O,isSaved:$}},t)}var m=(0,s.withRouter)(f);t.default=m}}));
//# sourceMappingURL=useAutoSave.es.js.map