{"size": 2270, "ext": "js", "max_stars_repo_path": "src/config.js", "max_stars_repo_name": "ruzicka/xvalidator", "max_stars_repo_stars_event_min_datetime": "2018-07-11T13:15:36.000Z", "max_stars_repo_stars_event_max_datetime": "2019-10-04T07:59:32.000Z", "max_issues_repo_path": "src/config.js", "max_issues_repo_name": "ruzicka/xvalidator", "max_issues_repo_issues_event_min_datetime": "2019-12-26T22:38:55.000Z", "max_issues_repo_issues_event_max_datetime": "2021-05-25T13:30:33.000Z", "max_forks_repo_path": "src/config.js", "max_forks_repo_name": "ruzicka/xvalidator", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 4, "max_issues_count": 8, "max_forks_count": null, "avg_line_length": 25.7954545455, "max_line_length": 119, "alphanum_fraction": 0.6929515419}
'use strict'

const preProcessObjectFnc = (obj, defaultFnc) => obj.$schema || defaultFnc(obj)
const postProcessCommonFnc = (type, schema, value, defaultFunc) => { // prevents override of required field in $schemas
  if (type === 'object' && value.$schema) {
    return schema // disabling default post processing (which is setting the require param)
  }
  return defaultFunc(type, schema, value)
}


const defaultValidatorOptions = {
  arrays: {
    mode: 'all',
  },
  strings: {
    formatDetectionMode: 'both', // none|name|content|both
  },
  objects: {
    additionalProperties: false,
  },
}

const stringFormats = [
  'date-time',
  'ip-address',
  'host-name',
  'utc-millisec',
  'date',
  'time',
  'email',
  'ipv6',
  'ipv4', // same as 'ip-address'
  'uri',
  'color',
  'hostname',
  'alpha',
  'alphanumeric',
  'regex',
  'style',
  'phone',
]

function stringsCustomFunction(value, defaultFnc) {
  if (stringFormats.indexOf(value) >= 0) {
    return {type: 'string', format: value}
  }
  return defaultFnc(value)
}

function getValidatorOptions(userOptions) {
  return {
    arrays: {...defaultValidatorOptions.arrays, ...userOptions.arrays},
    strings: {...defaultValidatorOptions.strings, ...userOptions.strings},
    objects: {...defaultValidatorOptions.objects, ...userOptions.objects},
  }
}

function convertToToJsonSchemaOptions(validatorOptions) {
  const {formatDetectionMode} = validatorOptions.strings
  const stringsDetectFormat = formatDetectionMode === 'both' || formatDetectionMode === 'content'
  const toJsonSchemaOptions = {
    required: true,
    postProcessFnc: postProcessCommonFnc, // prevents override of required field in $schemas
    arrays: {
      mode: validatorOptions.arrays.mode,
    },
    strings: {
      detectFormat: stringsDetectFormat,
    },
    objects: {
      preProcessFnc: preProcessObjectFnc, // makes $schema work
      additionalProperties: validatorOptions.objects.additionalProperties,
    },
  }

  if (formatDetectionMode === 'both' || formatDetectionMode === 'name') {
    toJsonSchemaOptions.strings.preProcessFnc = stringsCustomFunction // takes care of string format auto-detection
  }

  return toJsonSchemaOptions
}

module.exports = {
  getValidatorOptions,
  convertToToJsonSchemaOptions,
}
