"use strict";

require("core-js/modules/es.array.iterator");

require("core-js/modules/es.array.map");

require("core-js/modules/es.function.name");

require("core-js/modules/es.object.to-string");

require("core-js/modules/es.set");

require("core-js/modules/es.string.iterator");

require("core-js/modules/web.dom-collections.iterator");

exports.__esModule = true;
exports.GetDependenciesQuery = void 0;

var _AbsoluteCellRange = require("../AbsoluteCellRange");

var _absolutizeDependencies = require("../absolutizeDependencies");

var _parser = require("../parser");

var _ = require("./");

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

var GetDependenciesQuery = /*#__PURE__*/function () {
  function GetDependenciesQuery(rangeMapping, addressMapping, lazilyTransformingAstService, functionsWhichDoesNotNeedArgumentsToBeComputed, namedExpressions) {
    _classCallCheck(this, GetDependenciesQuery);

    this.rangeMapping = rangeMapping;
    this.addressMapping = addressMapping;
    this.lazilyTransformingAstService = lazilyTransformingAstService;
    this.functionsWhichDoesNotNeedArgumentsToBeComputed = functionsWhichDoesNotNeedArgumentsToBeComputed;
    this.namedExpressions = namedExpressions;
  }

  _createClass(GetDependenciesQuery, [{
    key: "call",
    value: function call(vertex) {
      var _this = this;

      var formula;
      var address;

      if (vertex instanceof _.FormulaCellVertex) {
        address = vertex.getAddress(this.lazilyTransformingAstService);
        formula = vertex.getFormula(this.lazilyTransformingAstService);
      } else if (vertex instanceof _.MatrixVertex && vertex.isFormula()) {
        address = vertex.getAddress();
        formula = vertex.getFormula();
      } else {
        return null;
      }

      var deps = (0, _parser.collectDependencies)(formula, this.functionsWhichDoesNotNeedArgumentsToBeComputed);
      var absoluteDeps = (0, _absolutizeDependencies.absolutizeDependencies)(deps, address);
      return new Set(absoluteDeps.map(function (dep) {
        if (dep instanceof _AbsoluteCellRange.AbsoluteCellRange) {
          return _this.rangeMapping.fetchRange(dep.start, dep.end);
        } else if (dep instanceof _parser.NamedExpressionDependency) {
          var namedExpression = _this.namedExpressions.namedExpressionOrPlaceholder(dep.name, address.sheet);

          return _this.addressMapping.fetchCell(namedExpression.address);
        } else {
          return _this.addressMapping.fetchCell(dep);
        }
      }));
    }
  }]);

  return GetDependenciesQuery;
}();

exports.GetDependenciesQuery = GetDependenciesQuery;