'use strict';

const normalize = require('../normalize.cjs');
const definedInfo = require('./defined-info.cjs');
const schema = require('./schema.cjs');

/**
 * @import {Info, Space} from 'property-information'
 */


/**
 * @param {Definition} definition
 *   Definition.
 * @returns {Schema}
 *   Schema.
 */
function create(definition) {
  /** @type {Record<string, Info>} */
  const properties = {};
  /** @type {Record<string, string>} */
  const normals = {};

  for (const [property, value] of Object.entries(definition.properties)) {
    const info = new definedInfo.DefinedInfo(
      property,
      definition.transform(definition.attributes || {}, property),
      value,
      definition.space
    );

    if (
      definition.mustUseProperty &&
      definition.mustUseProperty.includes(property)
    ) {
      info.mustUseProperty = true;
    }

    properties[property] = info;

    normals[normalize.normalize(property)] = property;
    normals[normalize.normalize(info.attribute)] = property;
  }

  return new schema.Schema(properties, normals, definition.space)
}

exports.create = create;
