"use strict";

var _lib = require("../lib");

var _testData = require("../../../../data/testData");

var _aggregate = require("../../aggregate");

var _grouping = require("../../grouping");

var _isPlainObject = require("../../../../lib/isPlainObject");

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

describe("prepareRows", function () {
  test("prepareRows({ tableData[noGrouping] }) => { meta[empty], rows: [ { ...check } ]}", function () {
    var _prepareRows = (0, _lib.prepareRows)({
      tableData: _testData.subsetOne
    }),
        rows = _prepareRows.rows,
        meta = _prepareRows.meta;

    rows.forEach(function (obj, i) {
      return expect(obj).toEqual((0, _lib.makeRow)({
        row: _testData.subsetOne[i]
      }));
    });
    expect(meta.pivotSet).toEqual(new Set());
    expect(meta.pivotBy).toBe(null);
    expect(meta.pivoted).toBe(false);
  });
  test("prepareRows({ tableData[noGrouping], pivotBy }) => { meta[filled], rows: [ { pivoted: true, [pivot]: { ...check } ]}", function () {
    var pivotBy = "namespace";

    var _prepareRows2 = (0, _lib.prepareRows)({
      tableData: _testData.subsetOne,
      pivotBy: pivotBy
    }),
        rows = _prepareRows2.rows,
        meta = _prepareRows2.meta;

    rows.forEach(function (obj, i) {
      var _ref;

      var pivot = _testData.subsetOne[i].namespace !== "" ? (_ref = "piv0t-".concat(_testData.subsetOne[i].namespace)) !== null && _ref !== void 0 ? _ref : "piv0t-other" : "piv0t-other";
      expect(obj).toEqual(_defineProperty({
        valueLookup: pivot,
        pivoted: true
      }, pivot, (0, _lib.makeRow)({
        row: _testData.subsetOne[i]
      })));
    });
    expect(meta.pivotSet).toEqual(new Set(["piv0t-aggregate", "piv0t-demo", "piv0t-nametest", "piv0t-other"]));
    expect(meta.pivotBy).toBe(pivotBy);
    expect(meta.pivoted).toBe(true);
  });
  test("prepareRows({ tableData[noGrouping], pivotBy, hideNamespacePrefix: false }) => { meta[filled], rows: [ { pivoted: true, [pivot]: { ...check } ]}", function () {
    var pivotBy = "namespace";

    var _prepareRows3 = (0, _lib.prepareRows)({
      tableData: _testData.subsetOne,
      pivotBy: pivotBy,
      hideNamespacePrefix: false
    }),
        rows = _prepareRows3.rows,
        meta = _prepareRows3.meta;

    rows.forEach(function (obj, i) {
      var _ref2;

      var pivot = _testData.subsetOne[i].namespace !== "" ? (_ref2 = "piv0t-".concat(_testData.subsetOne[i].namespace)) !== null && _ref2 !== void 0 ? _ref2 : "piv0t-other" : "piv0t-other";
      expect(obj).toEqual(_defineProperty({
        valueLookup: pivot,
        pivoted: true
      }, pivot, (0, _lib.makeRow)({
        row: _testData.subsetOne[i],
        hideNamespacePrefix: false
      })));
    });
    expect(meta.pivotSet).toEqual(new Set(["piv0t-aggregate", "piv0t-demo", "piv0t-nametest", "piv0t-other"]));
    expect(meta.pivotBy).toBe(pivotBy);
    expect(meta.pivoted).toBe(true);
  });
  test("prepareRows({ tableData[groupBy='name'], pivotBy: 'namespace', hideNamespacePrefix: false }) => { meta[filled], rows: [ { ...check, subRows[transformed] } ]}", function () {
    var pivotBy = "namespace";
    var hideNamespacePrefix = false;

    var subsetMap = _testData.subsetOne.reduce(function (acc, curr) {
      var _curr$name;

      acc[(_curr$name = curr.name) !== null && _curr$name !== void 0 ? _curr$name : ""] = curr;
      return acc;
    }, {});

    var aggregateChecks = (0, _aggregate.getAggregatedGroupedChecks)((0, _grouping.getGroupedChecks)(_testData.subsetOne, "name"), false);
    var values = Object.values(aggregateChecks);

    var _prepareRows4 = (0, _lib.prepareRows)({
      tableData: values,
      pivotBy: pivotBy,
      hideNamespacePrefix: hideNamespacePrefix
    }),
        rows = _prepareRows4.rows,
        meta = _prepareRows4.meta;

    rows.forEach(function (obj) {
      var _obj$piv0tAggregate, _obj$piv0tAggregate2;

      var isAggregate = ((_obj$piv0tAggregate = obj["piv0t-aggregate"]) === null || _obj$piv0tAggregate === void 0 ? void 0 : _obj$piv0tAggregate.isAggregate) != null && ((_obj$piv0tAggregate2 = obj["piv0t-aggregate"]) === null || _obj$piv0tAggregate2 === void 0 ? void 0 : _obj$piv0tAggregate2.isAggregate) === true;

      if (isAggregate) {
        var _aggregateChecks$sour, _groupedValue$subRows;

        var aggObj = obj["piv0t-aggregate"];
        var sourceName = aggObj.sourceName;
        var groupedValue = (_aggregateChecks$sour = aggregateChecks[sourceName]) !== null && _aggregateChecks$sour !== void 0 ? _aggregateChecks$sour : {};
        var sourceSubRows = (_groupedValue$subRows = groupedValue === null || groupedValue === void 0 ? void 0 : groupedValue.subRows) !== null && _groupedValue$subRows !== void 0 ? _groupedValue$subRows : [];

        var _prepareRows5 = (0, _lib.prepareRows)({
          tableData: sourceSubRows,
          pivotBy: pivotBy,
          hideNamespacePrefix: hideNamespacePrefix
        }),
            subRows = _prepareRows5.rows;

        var newRow = _objectSpread(_objectSpread({}, groupedValue), {}, {
          subRows: subRows
        });

        var aggregateFilters = (0, _lib.filterRowsByPivotSet)(subRows, new Set(["piv0t-aggregate", "piv0t-demo", "piv0t-nametest", "piv0t-other"]));

        var testRow = _objectSpread({
          valueLookup: "piv0t-aggregate",
          pivoted: true,
          "piv0t-aggregate": (0, _lib.makeRow)({
            row: newRow,
            hideNamespacePrefix: hideNamespacePrefix
          })
        }, aggregateFilters); // eslint-disable-next-line jest/no-conditional-expect


        expect(obj).toEqual(testRow);
      } else {
        var _obj$obj$valueLookup, _subsetMap$value$sour, _ref3;

        var value = (obj === null || obj === void 0 ? void 0 : obj.pivoted) === true ? (_obj$obj$valueLookup = obj[obj === null || obj === void 0 ? void 0 : obj.valueLookup]) !== null && _obj$obj$valueLookup !== void 0 ? _obj$obj$valueLookup : null : obj;
        var sourceObj = (_subsetMap$value$sour = subsetMap[value === null || value === void 0 ? void 0 : value.sourceName]) !== null && _subsetMap$value$sour !== void 0 ? _subsetMap$value$sour : {};
        var pivot = sourceObj.namespace !== "" ? (_ref3 = "piv0t-".concat(sourceObj.namespace)) !== null && _ref3 !== void 0 ? _ref3 : "piv0t-other" : "piv0t-other"; // eslint-disable-next-line jest/no-conditional-expect

        expect(obj).toEqual(_defineProperty({
          valueLookup: pivot,
          pivoted: true
        }, pivot, (0, _lib.makeRow)({
          row: sourceObj,
          hideNamespacePrefix: false
        })));
      }
    });
    expect(meta.pivotSet).toEqual(new Set(["piv0t-aggregate", "piv0t-demo", "piv0t-nametest", "piv0t-other"]));
    expect(meta.pivotBy).toBe("namespace");
    expect(meta.pivoted).toBe(true);
  });
  test("prepareRows({ tableData[groupBy='name'], pivotBy: 'labels', pivotLookup: 'test', hideNamespacePrefix: false }) => { meta[filled], rows: [ { ...check, subRows[transformed] } ]}", function () {
    var pivotBy = "labels";
    var hideNamespacePrefix = false;
    var pivotLookup = "test";

    var subsetMap = _testData.subsetOne.reduce(function (acc, curr) {
      var _curr$name2;

      acc[(_curr$name2 = curr.name) !== null && _curr$name2 !== void 0 ? _curr$name2 : ""] = curr;
      return acc;
    }, {});

    var aggregateChecks = (0, _aggregate.getAggregatedGroupedChecks)((0, _grouping.getGroupedChecks)(_testData.subsetOne, "name"), false);
    var values = Object.values(aggregateChecks);

    var _prepareRows6 = (0, _lib.prepareRows)({
      tableData: values,
      pivotBy: pivotBy,
      hideNamespacePrefix: hideNamespacePrefix,
      pivotLookup: pivotLookup
    }),
        rows = _prepareRows6.rows,
        meta = _prepareRows6.meta;

    rows.forEach(function (obj) {
      var _obj$piv0tAggregate3, _obj$piv0tAggregate4;

      var isAggregate = ((_obj$piv0tAggregate3 = obj["piv0t-aggregate"]) === null || _obj$piv0tAggregate3 === void 0 ? void 0 : _obj$piv0tAggregate3.isAggregate) != null && ((_obj$piv0tAggregate4 = obj["piv0t-aggregate"]) === null || _obj$piv0tAggregate4 === void 0 ? void 0 : _obj$piv0tAggregate4.isAggregate) === true;

      if (isAggregate) {
        var _aggregateChecks$sour2, _groupedValue$subRows2;

        var aggObj = obj["piv0t-aggregate"];
        var sourceName = aggObj.sourceName;
        var groupedValue = (_aggregateChecks$sour2 = aggregateChecks[sourceName]) !== null && _aggregateChecks$sour2 !== void 0 ? _aggregateChecks$sour2 : {};
        var sourceSubRows = (_groupedValue$subRows2 = groupedValue === null || groupedValue === void 0 ? void 0 : groupedValue.subRows) !== null && _groupedValue$subRows2 !== void 0 ? _groupedValue$subRows2 : [];

        var _prepareRows7 = (0, _lib.prepareRows)({
          tableData: sourceSubRows,
          pivotBy: pivotBy,
          hideNamespacePrefix: hideNamespacePrefix,
          pivotLookup: pivotLookup
        }),
            subRows = _prepareRows7.rows;

        var newRow = _objectSpread(_objectSpread({}, groupedValue), {}, {
          subRows: subRows
        });

        var aggregateFilters = (0, _lib.filterRowsByPivotSet)(subRows, new Set(["piv0t-aggregate", "piv0t-istest", "piv0t-istest2"])); // eslint-disable-next-line jest/no-conditional-expect

        var testRow = _objectSpread({
          valueLookup: "piv0t-aggregate",
          pivoted: true,
          "piv0t-aggregate": (0, _lib.makeRow)({
            row: newRow,
            hideNamespacePrefix: hideNamespacePrefix
          })
        }, aggregateFilters); // eslint-disable-next-line jest/no-conditional-expect


        expect(obj).toEqual(testRow);
      } else {
        var _obj$obj$valueLookup2, _subsetMap$value$sour2;

        var value = (obj === null || obj === void 0 ? void 0 : obj.pivoted) === true ? (_obj$obj$valueLookup2 = obj[obj === null || obj === void 0 ? void 0 : obj.valueLookup]) !== null && _obj$obj$valueLookup2 !== void 0 ? _obj$obj$valueLookup2 : null : obj;
        var sourceObj = (_subsetMap$value$sour2 = subsetMap[value === null || value === void 0 ? void 0 : value.sourceName]) !== null && _subsetMap$value$sour2 !== void 0 ? _subsetMap$value$sour2 : {};
        var isLabelsPlainObject = (0, _isPlainObject.isPlainObject)(sourceObj.labels);

        if (isLabelsPlainObject ? Object.keys(sourceObj.labels).length === 0 : false) {
          // eslint-disable-next-line jest/no-conditional-expect
          expect(obj).toEqual({
            valueLookup: "piv0t-none",
            pivoted: true,
            "piv0t-none": (0, _lib.makeRow)({
              row: sourceObj,
              hideNamespacePrefix: false
            })
          });
        }

        if (isLabelsPlainObject ? Object.keys(sourceObj.labels).length > 0 : false) {
          var keyObject = Object.entries(sourceObj.labels).reduce(function (acc, _ref4) {
            var _ref5 = _slicedToArray(_ref4, 2),
                k = _ref5[0],
                v = _ref5[1];

            if (k === pivotLookup) {
              new Set(["piv0t-aggregate", "piv0t-istest", "piv0t-istest2"]).add("piv0t-".concat(v));
              acc["piv0t-".concat(v)] = (0, _lib.makeRow)({
                row: sourceObj,
                hideNamespacePrefix: hideNamespacePrefix
              });
            }

            return acc;
          }, {});

          var _newRow = _objectSpread({
            valueLookup: Object.keys(keyObject)[0],
            pivoted: true
          }, keyObject); // eslint-disable-next-line jest/no-conditional-expect


          expect(obj).toEqual(_newRow);
        }
      }
    });
    expect(meta.pivotSet).toEqual(new Set(["piv0t-aggregate", "piv0t-istest", "piv0t-istest2"]));
    expect(meta.pivotBy).toBe("labels");
    expect(meta.pivoted).toBe(true);
  });
});