"use strict";

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

var _logger = require("../logger");

function nestedSelect(obj, key) {
  let temp = obj;
  const keys = key.split('.');

  for (let i = 0; i < keys.length && temp; i++) {
    const element = keys[i];
    temp = temp[element];
  }

  return temp;
}

function nestedUpdate(obj, key, value) {
  let temp = obj;
  const keys = key.split('.');
  let i = 0;

  for (; i < keys.length - 1 && temp; i++) {
    const element = keys[i];
    temp = temp[element];
  }

  if (temp) {
    temp[keys[i]] = value;
  }
}

function deprecatedOptionsHandler(options) {
  function checkAndUpdateAndPrintWaring(oldKey, newkey) {
    const oldVal = nestedSelect(options, oldKey);
    const newVal = nestedSelect(options, newkey); // console.log({ oldVal, newVal });

    if (oldVal !== undefined && newVal === undefined) {
      nestedUpdate(options, newkey, newVal);
      (0, _logger.warnLogger)(`"${oldKey}" deprecated use "${newkey}" instead`);
    }
  } // return;


  checkAndUpdateAndPrintWaring('app.context', 'context'); // TODO handle deprecations  here
  // we will finish this logic later when options are confirmed

  if (nestedSelect(options, 'app.hasRTL') === true) {
    nestedUpdate(options, 'app.plugins.hasRTL', true);
  }

  if (nestedSelect(options, 'docs.hasRTL') === true) {
    nestedUpdate(options, 'docs.plugins.hasRTL', true);
  }

  if (nestedSelect(options, 'app.rtlExclude.length') > 0) {
    nestedUpdate(options, 'app.exclude.rtl', nestedSelect(options, 'app.rtlExclude'));
  }

  if (nestedSelect(options, 'docs.rtlExclude.length') > 0) {
    nestedUpdate(options, 'docs.exclude.rtl', nestedSelect(options, 'docs.rtlExclude'));
  }
}