/**
 * Minified by jsDelivr using Terser v5.48.0.
 * Original file: /npm/react-native-ota-hot-update@2.4.71/lib/commonjs/index.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;var _reactNative=require("react-native"),_index=_interopRequireDefault(require("./gits/index.js"));function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}const LINKING_ERROR="The package 'react-native-ota-hot-update' doesn't seem to be linked. Make sure: \n\n"+_reactNative.Platform.select({ios:"- You have run 'pod install'\n",default:""})+"- You rebuilt the app after installing the package\n- You are not using Expo Go\n",isTurboModuleEnabled=null!=global.__turboModuleProxy,OtaHotUpdateModule=isTurboModuleEnabled?require("./NativeOtaHotUpdate").default:_reactNative.NativeModules.OtaHotUpdate,RNhotupdate=OtaHotUpdateModule||new Proxy({},{get(){throw new Error(LINKING_ERROR)}}),downloadBundleFile=async(e,t,r,a)=>(await e.config({fileCache:"android"===_reactNative.Platform.OS,path:e?.fs?.dirs?.LibraryDir&&"ios"===_reactNative.Platform.OS?`${e.fs.dirs.LibraryDir}/${(new Date).valueOf()}_hotupdate.zip`:void 0}).fetch("GET",t,{...r}).progress((e,t)=>{a&&a(e,t)})).path();function setupBundlePath(e,t,r,a,n){const o=n?JSON.stringify(n):void 0;return RNhotupdate.setupBundlePath(e,t,r,a||2,o)}function setupExactBundlePath(e){return RNhotupdate.setExactBundlePath(e)}function deleteBundlePath(){return RNhotupdate.deleteBundle(1)}function getCurrentVersion(){return RNhotupdate.getCurrentVersion(0)}function getUpdateMetadata(){return RNhotupdate.getUpdateMetadata(0).then(e=>{try{return e?JSON.parse(e):null}catch(e){return Promise.reject(new Error("Error parsing metadata"))}})}function rollbackToPreviousBundle(){return RNhotupdate.rollbackToPreviousBundle(0)}async function getVersionAsNumber(){return+await getCurrentVersion()}function setCurrentVersion(e){return RNhotupdate.setCurrentVersion(e+"")}function setUpdateMetadata(e){try{const t=JSON.stringify(e);return RNhotupdate.setUpdateMetadata(t)}catch(e){return Promise.reject(new Error("Failed to stringify metadata"))}}async function resetApp(){RNhotupdate.restart()}function removeBundle(e){deleteBundlePath().then(t=>{t&&e&&(setTimeout(()=>{resetApp()},300),t&&setCurrentVersion(0))})}const serializeError=e=>{if(null==e)return"Unknown error";if("string"==typeof e)return e;if(e instanceof Error){const t=e.message||e.name||"Unknown error";return"undefined"!=typeof __DEV__&&__DEV__&&e.stack?`${t}\n${e.stack}`:t}if("object"==typeof e){if("string"==typeof e.message&&e.message)return e.message;try{const t=JSON.stringify(e);if(t&&"{}"!==t)return t}catch(e){}return String(e)}return String(e)},installFail=(e,t)=>{const r=serializeError(t);e?.updateFail?.(r),console.error("Download bundle fail",r)};async function downloadBundleUri(e,t,r,a){if(!t)return installFail(a,"Please give a valid URL!");if(r){if("number"!=typeof r||!Number.isFinite(r))return installFail(a,`Version must be a number, received: ${JSON.stringify(r)} (${typeof r}). Use a numeric build number instead of a semver string, e.g. 1, 2, 3.`);const e=await getVersionAsNumber();if(r<=e)return installFail(a,"Please give a bigger version than the current version, the current version now has setted by: "+e)}try{const n=await downloadBundleFile(e,t,a?.headers,a?.progress);if(!n)return installFail(a,`Cannot download bundle file: ${n}`);if(!await setupBundlePath(n,a?.extensionBundle,r,a?.maxBundleVersions,a?.metadata))return installFail(a,`Cannot setup bundle path: ${n}. The downloaded file may be corrupted or is not a valid bundle.`);r&&setCurrentVersion(r),a?.metadata&&setUpdateMetadata(a.metadata),a?.updateSuccess?.(),a?.restartAfterInstall&&setTimeout(()=>{resetApp()},a?.restartDelay||300)}catch(e){installFail(a,e)}}const checkForGitUpdate=async e=>{try{if(!e.url||!e.bundlePath)throw new Error("url or bundlePath should not be null");const[t,r]=await Promise.all([_index.default.getConfig(),_index.default.getBranchName()]);if(r&&t){const t=await _index.default.pullUpdate({branch:r,onProgress:e?.onProgress,folderName:e?.folderName});t.success?(e?.onPullSuccess?.(),e?.restartAfterInstall&&setTimeout(()=>{resetApp()},300)):e?.onPullFailed?.(t.msg)}else{const t=await _index.default.cloneRepo({onProgress:e?.onProgress,folderName:e?.folderName,url:e.url,branch:e?.branch,bundlePath:e.bundlePath});t.success&&t.bundle?(await setupExactBundlePath(t.bundle),e?.onCloneSuccess?.(),e?.restartAfterInstall&&setTimeout(()=>{resetApp()},300)):e?.onCloneFailed?.(t.msg)}}catch(t){e?.onCloneFailed?.(t.toString())}finally{e?.onFinishProgress?.()}},parseBundleMetadata=e=>{if("string"!=typeof e)return e;try{return JSON.parse(e)}catch(t){return e}};function getBundleList(){return RNhotupdate.getBundleList(0).then(e=>{try{return JSON.parse(e).map(e=>({...e,date:new Date(e.date),metadata:parseBundleMetadata(e.metadata)}))}catch(e){return Promise.reject(new Error("Error parsing bundle list"))}})}function deleteBundleById(e){return RNhotupdate.deleteBundleById(e)}function clearAllBundles(){return RNhotupdate.clearAllBundles(0)}var _default=exports.default={setupBundlePath:setupBundlePath,setupExactBundlePath:setupExactBundlePath,removeUpdate:removeBundle,downloadBundleUri:downloadBundleUri,resetApp:resetApp,getCurrentVersion:getVersionAsNumber,setCurrentVersion:setCurrentVersion,getUpdateMetadata:getUpdateMetadata,setUpdateMetadata:setUpdateMetadata,rollbackToPreviousBundle:rollbackToPreviousBundle,getBundleList:getBundleList,deleteBundleById:deleteBundleById,clearAllBundles:clearAllBundles,git:{checkForGitUpdate:checkForGitUpdate,..._index.default,removeGitUpdate:e=>{RNhotupdate.setExactBundlePath(""),_index.default.removeGitUpdate(e)}}};
//# sourceMappingURL=/sm/fcd1a1f9fec8ccf7cf451368cdcfa47b5eade571590a2e8ea780640ae2f08b07.map