import { Setting } from 'obsidian'
import type LocatorPlugin from 'src/main'
import type { LocatorSettings } from './utils'
import { htmlDescription, isCacheEnabled, needsARestart } from './utils'

export function injectSettingsDanger(
  plugin: LocatorPlugin,
  settings: LocatorSettings,
  containerEl: HTMLElement
) {
  const database = plugin.database

  new Setting(containerEl).setName('Danger Zone').setHeading()

  // Clear cache data
  if (isCacheEnabled()) {
    new Setting(containerEl)
      .setName('Clear cache data')
      .setDesc(
        htmlDescription(`Erase all Locator cache data.
        Use this if Locator results are inconsistent, missing, or appear outdated.<br>
        ${needsARestart}`)
      )
      .addButton(btn => {
        btn.setButtonText('Clear cache')
        btn.onClick(async () => {
          await database.clearCache()
        })
      })
  }
}
