"use strict";

exports.__esModule = true;
exports.Slice = Slice;
exports.SlicesResultsContext = exports.SlicesPropsContext = exports.SlicesMapContext = exports.SlicesContext = void 0;

var _react = _interopRequireWildcard(require("react"));

var _serverSlice = require("./slice/server-slice");

var _inlineSlice = require("./slice/inline-slice");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

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

function Slice(props) {
  if ("5" === `5` && process.env.GATSBY_SLICES) {
    // we use sliceName internally, so remap alias to sliceName
    const internalProps = { ...props,
      sliceName: props.alias
    };
    delete internalProps.alias;
    const slicesContext = (0, _react.useContext)(SlicesContext); // validate props

    const propErrors = validateSliceProps(props);

    if (Object.keys(propErrors).length) {
      throw new SlicePropsError(slicesContext.renderEnvironment === `browser`, internalProps.sliceName, propErrors);
    }

    if (slicesContext.renderEnvironment === `server`) {
      return /*#__PURE__*/_react.default.createElement(_serverSlice.ServerSlice, internalProps);
    } else if (slicesContext.renderEnvironment === `browser`) {
      // in the browser, we'll just render the component as is
      return /*#__PURE__*/_react.default.createElement(_inlineSlice.InlineSlice, internalProps);
    } else if (slicesContext.renderEnvironment === `engines`) {
      // if we're in SSR, we'll just render the component as is
      return /*#__PURE__*/_react.default.createElement(_inlineSlice.InlineSlice, internalProps);
    } else {
      throw new Error(`Slice context "${slicesContext.renderEnvironment}" is not supported.`);
    }
  } else {
    throw new Error(`Slices are only available in Gatsby v5`);
  }
}

class SlicePropsError extends Error {
  constructor(inBrowser, sliceName, propErrors) {
    const errors = Object.entries(propErrors).map(([key, value]) => `${key}: "${value}"`).join(`, `);
    const name = `SlicePropsError`;
    let stack = ``;
    let message = ``;

    if (inBrowser) {
      // They're just (kinda) kidding, I promise... You can still work here <3
      //   https://www.gatsbyjs.com/careers/
      const fullStack = _react.default.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactDebugCurrentFrame.getCurrentStack(); // remove the first line of the stack trace


      const stackLines = fullStack.trim().split(`\n`).slice(1);
      stackLines[0] = stackLines[0].trim();
      stack = `\n` + stackLines.join(`\n`); // look for any hints for the component name in the stack trace

      const componentRe = /^at\s+([a-zA-Z0-9]+)/;
      const componentMatch = stackLines[0].match(componentRe);
      const componentHint = componentMatch ? `in ${componentMatch[1]} ` : ``;
      message = `Slice "${sliceName}" was passed props ${componentHint}that are not serializable (${errors}).`;
    } else {
      // we can't really grab any extra info outside of the browser, so just print what we can
      message = `${name}: Slice "${sliceName}" was passed props that are not serializable (${errors}). Use \`gatsby develop\` to see more information.`;
      const stackLines = new Error().stack.trim().split(`\n`).slice(2);
      stack = `${message}\n${stackLines.join(`\n`)}`;
    }

    super(message);
    this.name = name;
    this.stack = stack;
  }

}

const validateSliceProps = (props, errors = {}, seenObjects = [], path = null) => {
  // recursively validate all props
  for (const [name, value] of Object.entries(props)) {
    if (value === undefined || value === null || !path && name === `children`) {
      continue;
    }

    const propPath = path ? `${path}.${name}` : name;

    if (typeof value === `function`) {
      errors[propPath] = typeof value;
    } else if (typeof value === `object` && seenObjects.indexOf(value) <= 0) {
      seenObjects.push(value);
      validateSliceProps(value, errors, seenObjects, propPath);
    }
  }

  return errors;
};

const SlicesResultsContext = /*#__PURE__*/_react.default.createContext({});

exports.SlicesResultsContext = SlicesResultsContext;

const SlicesContext = /*#__PURE__*/_react.default.createContext({});

exports.SlicesContext = SlicesContext;

const SlicesMapContext = /*#__PURE__*/_react.default.createContext({});

exports.SlicesMapContext = SlicesMapContext;

const SlicesPropsContext = /*#__PURE__*/_react.default.createContext({});

exports.SlicesPropsContext = SlicesPropsContext;