"use strict";

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

var _arson = _interopRequireDefault(require("arson"));

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

var _server = require("react-dom/server");

var _components = require("@patternplate/components");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

const DETONATOR = process.env.WEBPACK ? require("raw-loader!./detonator") : String(require("fs").readFileSync(require("path").resolve(__dirname, "detonator.js")));
var _default = layout;
exports.default = _default;

function layout(props) {
  const layoutSheet = new _components.ServerStyleSheet();
  layoutSheet.collectStyles((0, _server.renderToStaticMarkup)(_react.default.createElement(Layout, null)));
  const layoutCSS = layoutSheet.getStyleElement();
  return `<!doctype html>${(0, _server.renderToStaticMarkup)(_react.default.createElement(Layout, _extends({}, props, {
    layoutCSS: layoutCSS
  })))}`;
}

function Layout(props) {
  const attributes = props.attributes ? props.attributes.toComponent() : {};
  const scripts = Array.isArray(props.scripts) ? props.scripts : [];
  return _react.default.createElement(StyledDocument, attributes, _react.default.createElement("head", null, props.title && props.title.toComponent(), props.meta && props.meta.toComponent(), props.link && props.link.toComponent(), _react.default.createElement("script", {
    dangerouslySetInnerHTML: {
      __html: DETONATOR
    }
  }), props.layoutCSS, props.css), _react.default.createElement(StyledBody, {
    "data-base": props.base
  }, _react.default.createElement(Content, {
    "data-application-el": "patternplate",
    content: props.html
  }), _react.default.createElement(State, {
    "data-application-state": "patternplate",
    data: props.data
  }), scripts.map(src => _react.default.createElement("script", {
    key: src,
    src: src
  }))));
}

const StyledDocument = _components.styled.html.withConfig({
  displayName: "layouts__StyledDocument"
})(["height:100%;overflow:hidden;"]);

const StyledBody = _components.styled.body.withConfig({
  displayName: "layouts__StyledBody"
})(["margin:0;height:100%;"]);

function Content(props) {
  return _react.default.createElement(StyledContent, {
    "data-application-el": props["data-application-el"],
    dangerouslySetInnerHTML: {
      __html: props.content
    }
  });
}

const StyledContent = _components.styled.div.withConfig({
  displayName: "layouts__StyledContent"
})(["height:100%;"]);

function State(props) {
  const value = encodeURIComponent(_arson.default.stringify(props.data));
  return _react.default.createElement(StyledState, {
    "data-application-state": props["data-application-state"],
    value: value,
    readOnly: true
  });
}

const StyledState = _components.styled.textarea.withConfig({
  displayName: "layouts__StyledState"
})(["display:none;"]);
//# sourceMappingURL=index.js.map