Liferay.Loader.define("dynamic-data-mapping-form-web@4.0.84/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","./useStateSync.es","./useValidateFormWithObjects"],(function(e,t,a){var r;window;{Object.defineProperty(t,"__esModule",{value:!0}),t.AutoSaveProvider=function({children:t,interval:a,location:r,url:u}){const{portletNamespace:i}=(0,o.useConfig)(),{availableLanguageIds:y,defaultLanguageId:b,localizedDescription:v,localizedName:S,pages:h,paginationMode:j,rules:w,successPageSettings:I}=(0,o.useFormState)(),O=(0,s.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,l.useValidateFormWithObjects)(),C=(0,c.useCallback)((()=>d({availableLanguageIds:y,defaultLanguageId:b,description:v,name:S,pages:h,paginationMode:j,rules:w,successPageSettings:I})),[y,b,v,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:f({name:S,portletNamespace:i}),url:u}).then((t=>(D.current=null,p(i,t),F.current=e,m({modifiedDate:t.modifiedDate,portletNamespace:i}),t))).catch((e=>{D.current=null,console.error(e)}))),D.current}),[O,C,F,S,r.pathname,D,i,u,k]),_=(0,c.useCallback)((()=>F.current===C()),[F,C]),q=(0,c.useCallback)((()=>{M&&(D.current?D.current.then((()=>q())).catch((e=>console.error(e))):_()||o.FormSupport.isEmpty(h)||N())}),[N,M,_,h,D]);return(0,c.useEffect)((()=>(a>0&&(P.current=setInterval((()=>q()),a*g)),()=>{P.current&&clearInterval(P.current)})),[P,a,q]),(0,c.useEffect)((()=>{F.current=C(),L.current=d(w)}),[]),(0,c.useEffect)((()=>{const e=d(w);L.current!==e&&(L.current=e,q())}),[L,w]),c.default.createElement(e.Provider,{value:{doSave:N,doSyncInput:O,isSaved:_}},t)},t.useAutoSave=function(){return(0,c.useContext)(e)};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("./useStateSync.es"),l=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 d=e=>(0,u.default)(e,{algorithm:"md5",unorderedObjects:!0}),f=({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)},p=(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)},m=({modifiedDate:e,portletNamespace:t})=>{document.querySelector("#".concat(t,"autosaveMessage")).innerHTML=o.StringUtils.sub(Liferay.Language.get("draft-x"),[e])},g=6e4}}));
//# sourceMappingURL=useAutoSave.es.js.map