/* global console, process, YAML_SILENCE_DEPRECATION_WARNINGS, YAML_SILENCE_WARNINGS */

function shouldWarn(deprecation) {
  const env = (typeof process !== 'undefined' && process.env) || {}

  if (deprecation) {
    if (typeof YAML_SILENCE_DEPRECATION_WARNINGS !== 'undefined')
      return !YAML_SILENCE_DEPRECATION_WARNINGS
    return !env.YAML_SILENCE_DEPRECATION_WARNINGS
  }

  if (typeof YAML_SILENCE_WARNINGS !== 'undefined')
    return !YAML_SILENCE_WARNINGS
  return !env.YAML_SILENCE_WARNINGS
}

export function warn(warning, type) {
  if (shouldWarn(false)) {
    const emit = typeof process !== 'undefined' && process.emitWarning
    // This will throw in Jest if `warning` is an Error instance due to
    // https://github.com/facebook/jest/issues/2549
    if (emit) emit(warning, type)
    else {
      // eslint-disable-next-line no-console
      console.warn(type ? `${type}: ${warning}` : warning)
    }
  }
}

export function warnFileDeprecation(filename) {
  if (shouldWarn(true)) {
    const path = filename
      .replace(/.*yaml[/\\]/i, '')
      .replace(/\.js$/, '')
      .replace(/\\/g, '/')
    warn(
      `The endpoint 'yaml/${path}' will be removed in a future release.`,
      'DeprecationWarning'
    )
  }
}

const warned = {}
export function warnOptionDeprecation(name, alternative) {
  if (!warned[name] && shouldWarn(true)) {
    warned[name] = true
    let msg = `The option '${name}' will be removed in a future release`
    msg += alternative ? `, use '${alternative}' instead.` : '.'
    warn(msg, 'DeprecationWarning')
  }
}
