"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.applyValuesToSchema = applyValuesToSchema;

var _isObject = require("./isObject");

var _isValid = require("./isValid");

function applyValuesToSchema(schemas, customOptions, processEnv) {
  const output = {};

  function decideValue(schema, key, customOptions) {
    const specificSchema = schema[key];
    const correspondingValue = customOptions && customOptions[key];

    if ((0, _isObject.isObject)(specificSchema)) {
      if (typeof specificSchema.cli === 'string') {
        return processEnv[specificSchema.cli] ?? (0, _isValid.isValid)(correspondingValue, specificSchema.value);
      }

      return applyValuesToSchema(specificSchema, correspondingValue, processEnv);
    }

    return (0, _isValid.isValid)(correspondingValue, specificSchema);
  }

  Object.keys(schemas).forEach(key => {
    output[key] = decideValue(schemas, key, customOptions);
  });
  return output;
}