import { SettingsObject } from '../types/types'

export const isManualMode = (settings: SettingsObject, globalMode: string): boolean => {
  const settingMode = settings['critical_css_mode']
  if (!settingMode || settingMode === 'default') {
    return globalMode === 'manual'
  }
  return settingMode === 'manual'
}

export const areBothManualFieldsFilled = (settings: SettingsObject): boolean => {
  const criticalCss = typeof settings['critical_css'] === 'string' ? settings['critical_css'].trim() : ''
  const manualSecondary = typeof settings['manual_secondary_css'] === 'string' ? settings['manual_secondary_css'].trim() : ''
  return criticalCss.length > 0 && manualSecondary.length > 0
}

export const buildGeneratorUrl = (postUrl: string, pluginVersion: string, userEmail?: string): string => {
  const params = new URLSearchParams({
    page_url: postUrl,
    utm_source: 'easy-critical-css-plugin',
    utm_medium: 'editor',
    utm_campaign: `v${pluginVersion}`,
  })
  if (userEmail) {
    params.append('email', userEmail)
  }
  return `https://criticalcss.net/?${params.toString()}#free-generator`
}
