"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.collectSlices = collectSlices;
exports.mergePreviouslyCollectedSlices = mergePreviouslyCollectedSlices;

var _traverse = _interopRequireDefault(require("@babel/traverse"));

const SLICE_NAME_PROP = `alias`;

function mergePreviouslyCollectedSlice(newInfo, previousInfo) {
  var _previousInfo$allowEm;

  return {
    name: newInfo.name,
    // it's enough for one use of slice that doesn't allow empty
    // to require passing everyhing
    allowEmpty: (previousInfo === null || previousInfo === void 0 ? void 0 : previousInfo.allowEmpty) === false ? false : (_previousInfo$allowEm = previousInfo === null || previousInfo === void 0 ? void 0 : previousInfo.allowEmpty) !== null && _previousInfo$allowEm !== void 0 ? _previousInfo$allowEm : newInfo.allowEmpty
  };
}

function mergePreviouslyCollectedSlices(newInfo, previousInfo) {
  const ret = previousInfo !== null && previousInfo !== void 0 ? previousInfo : {};

  for (const [name, info] of Object.entries(newInfo)) {
    ret[name] = mergePreviouslyCollectedSlice(info, ret[name]);
  }

  return ret;
}

function collectSlices(ast, filename) {
  const result = {};
  let hasResults = false;
  (0, _traverse.default)(ast, {
    JSXOpeningElement(nodePath) {
      if (!nodePath.get(`name`).referencesImport(`gatsby`, `Slice`)) {
        return;
      }

      let name;
      let allowEmpty = false; // TODO: probably want to use something higher level (like babel-jsx-utils)
      // to "calc" attribute values instead of trying to handle AST ourselves

      for (const attribute of nodePath.node.attributes) {
        if (attribute.type === `JSXAttribute`) {
          if (attribute.name.name === SLICE_NAME_PROP) {
            var _attribute$value;

            if (((_attribute$value = attribute.value) === null || _attribute$value === void 0 ? void 0 : _attribute$value.type) === `StringLiteral`) {
              name = attribute.value.value;
            } else {
              console.warn(`Not literal value for "${attribute.name.name}"`, {
                filename,
                value: attribute.value
              });
            }
          } else if (attribute.name.name === `allowEmpty`) {
            if (attribute.value === null) {
              allowEmpty = true;
            } else {
              console.warn(`allowEmpty can't get any value`, {
                filename
              });
            }
          }
        } else if (attribute.type === `JSXSpreadAttribute`) {// this needs special handling - not sure if worth doing so
          // will leave it for when we use `getAttributeValues` from babel-jsx-utils
          // as we do for `<StaticImage>` instead of digging into AST ourselves
          // to potentially support spread props that can be analyzed?
        }
      }

      if (name) {
        result[name] = mergePreviouslyCollectedSlice({
          name,
          allowEmpty
        }, result[name]);
        hasResults = true;
      }
    }

  });
  return hasResults ? result : null;
}
//# sourceMappingURL=find-slices.js.map