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