import picgo from '@core/picgo'
import updater from 'electron-updater'

import { isPortable } from '~/apis/core/datastore/dirs'
import { checkUpdateAndNotify } from '~/lifeCycle/autoUpdater'
import { configPaths } from '~/utils/configPaths'

const updateChecker = async () => {
  let showTip = picgo.getConfig<boolean | undefined>(configPaths.settings.showUpdateTip)
  if (showTip === undefined) {
    picgo.saveConfig({ [configPaths.settings.showUpdateTip]: true })
    showTip = true
  }
  if (showTip) {
    try {
      if (!isPortable()) {
        await updater.autoUpdater.checkForUpdatesAndNotify()
      } else {
        await checkUpdateAndNotify()
      }
    } catch (_err) {}
  }
}

export default updateChecker
