'use strict';

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

require("./../utils/polyfills");

var _object = require("./../utils/object");

var emitter = _interopRequireWildcard(require("./../utils/emitter"));

var _import = require("./function/import");

var _config2 = require("./function/config");

var _factory = require("../utils/factory");

var _is = require("../utils/is");

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj["default"] = obj; return newObj; } }

var dependencies = ['config'];
/**
 * Math.js core. Creates a new, empty math.js instance
 * @param {Object} config     Available options:
 *                            {number} epsilon
 *                              Minimum relative difference between two
 *                              compared values, used by all comparison functions.
 *                            {string} matrix
 *                              A string 'Matrix' (default) or 'Array'.
 *                            {string} number
 *                              A string 'number' (default), 'BigNumber', or 'Fraction'
 *                            {number} precision
 *                              The number of significant digits for BigNumbers.
 *                              Not applicable for Numbers.
 *                            {boolean} predictable
 *                              Predictable output type of functions. When true,
 *                              output type depends only on the input types. When
 *                              false (default), output type can vary depending
 *                              on input values. For example `math.sqrt(-4)`
 *                              returns `complex('2i')` when predictable is false, and
 *                              returns `NaN` when true.
 *                            {string} randomSeed
 *                              Random seed for seeded pseudo random number generator.
 *                              Set to null to randomly seed.
 * @returns {Object} Returns a bare-bone math.js instance containing
 *                   functions:
 *                   - `import` to add new functions
 *                   - `config` to change configuration
 *                   - `on`, `off`, `once`, `emit` for events
 */

var createCore = (0, _factory.factory)('core', dependencies, function (_ref) {
  var config = _ref.config;

  // simple test for ES5 support
  if (typeof Object.create !== 'function') {
    throw new Error('ES5 not supported by this JavaScript engine. ' + 'Please load the es5-shim and es5-sham library for compatibility.');
  } // create the mathjs instance


  var math = emitter.mixin({
    // only here for backward compatibility for legacy factory functions
    isNumber: _is.isNumber,
    isComplex: _is.isComplex,
    isBigNumber: _is.isBigNumber,
    isFraction: _is.isFraction,
    isUnit: _is.isUnit,
    isString: _is.isString,
    isArray: _is.isArray,
    isMatrix: _is.isMatrix,
    isDenseMatrix: _is.isDenseMatrix,
    isSparseMatrix: _is.isSparseMatrix,
    isRange: _is.isRange,
    isIndex: _is.isIndex,
    isBoolean: _is.isBoolean,
    isResultSet: _is.isResultSet,
    isHelp: _is.isHelp,
    isFunction: _is.isFunction,
    isDate: _is.isDate,
    isRegExp: _is.isRegExp,
    isObject: _is.isObject,
    isNull: _is.isNull,
    isUndefined: _is.isUndefined,
    isAccessorNode: _is.isAccessorNode,
    isArrayNode: _is.isArrayNode,
    isAssignmentNode: _is.isAssignmentNode,
    isBlockNode: _is.isBlockNode,
    isConditionalNode: _is.isConditionalNode,
    isConstantNode: _is.isConstantNode,
    isFunctionAssignmentNode: _is.isFunctionAssignmentNode,
    isFunctionNode: _is.isFunctionNode,
    isIndexNode: _is.isIndexNode,
    isNode: _is.isNode,
    isObjectNode: _is.isObjectNode,
    isOperatorNode: _is.isOperatorNode,
    isParenthesisNode: _is.isParenthesisNode,
    isRangeNode: _is.isRangeNode,
    isSymbolNode: _is.isSymbolNode,
    isChain: _is.isChain
  }); // load config function and apply provided config

  var _config = config; // keep internal _config private

  math.config = (0, _config2.configFactory)(_config, math.emit);
  math.expression = {
    transform: {},
    mathWithTransform: {
      config: math.config
    } // cached factories and instances used by function load

  };
  var legacyFactories = [];
  var legacyInstances = [];
  /**
   * Load a function or data type from a factory.
   * If the function or data type already exists, the existing instance is
   * returned.
   * @param {Function} factory
   * @returns {*}
   */

  function load(factory) {
    if ((0, _factory.isFactory)(factory)) {
      return factory(math);
    }

    var firstProperty = factory[Object.keys(factory)[0]];

    if ((0, _factory.isFactory)(firstProperty)) {
      return firstProperty(math);
    }

    if (!(0, _object.isLegacyFactory)(factory)) {
      console.warn('Factory object with properties `type`, `name`, and `factory` expected', factory);
      throw new Error('Factory object with properties `type`, `name`, and `factory` expected');
    }

    var index = legacyFactories.indexOf(factory);
    var instance;

    if (index === -1) {
      // doesn't yet exist
      if (factory.math === true) {
        // pass with math namespace
        instance = factory.factory(math.type, _config, load, math.typed, math);
      } else {
        instance = factory.factory(math.type, _config, load, math.typed);
      } // append to the cache


      legacyFactories.push(factory);
      legacyInstances.push(instance);
    } else {
      // already existing function, return the cached instance
      instance = legacyInstances[index];
    }

    return instance;
  }

  var factories = {}; // load the import function

  function lazyTyped() {
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    return math.typed.apply(math.typed, args);
  }

  math['import'] = (0, _import.importFactory)(lazyTyped, load, math, factories); // listen for changes in config, import all functions again when changed

  math.on('config', function () {
    (0, _object.values)(factories).forEach(function (factory) {
      if (factory && factory.recreateOnConfigChange) {
        // FIXME: only re-create when the current instance is the same as was initially created
        // FIXME: delete the functions/constants before importing them again?
        math['import'](factory, {
          override: true
        });
      }
    });
  });
  return math;
});
exports.createCore = createCore;