import {normalize} from './normalize.js'
import {DefinedInfo} from './util/defined-info.js'
import {Info} from './util/info.js'

var valid = /^data[-\w.:]+$/i
var dash = /-[a-z]/g
var cap = /[A-Z]/g

/**
 * @param {import('./util/schema.js').Schema} schema
 * @param {string} value
 * @returns {import('./util/info.js').Info}
 */
export function find(schema, value) {
  var normal = normalize(value)
  var prop = value
  var Type = Info

  if (normal in schema.normal) {
    return schema.property[schema.normal[normal]]
  }

  if (normal.length > 4 && normal.slice(0, 4) === 'data' && valid.test(value)) {
    // Attribute or property.
    if (value.charAt(4) === '-') {
      prop = datasetToProperty(value)
    } else {
      value = datasetToAttribute(value)
    }

    Type = DefinedInfo
  }

  return new Type(prop, value)
}

/**
 * @param {string} attribute
 * @returns {string}
 */
function datasetToProperty(attribute) {
  var value = attribute.slice(5).replace(dash, camelcase)
  return 'data' + value.charAt(0).toUpperCase() + value.slice(1)
}

/**
 * @param {string} property
 * @returns {string}
 */
function datasetToAttribute(property) {
  var value = property.slice(4)

  if (dash.test(value)) {
    return property
  }

  value = value.replace(cap, kebab)

  if (value.charAt(0) !== '-') {
    value = '-' + value
  }

  return 'data' + value
}

/**
 * @param {string} $0
 * @returns {string}
 */
function kebab($0) {
  return '-' + $0.toLowerCase()
}

/**
 * @param {string} $0
 * @returns {string}
 */
function camelcase($0) {
  return $0.charAt(1).toUpperCase()
}
