'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-1206254d.js');
require('./index-f9fece3f.js');
const runStore = require('./runStore-c9c2c566.js');
const core = require('./core-3efcc000.js');
require('./_commonjsHelpers-5cfcba41.js');
const index$1 = require('./index-33b35744.js');

const PostCanvasGQL = `
    mutation createCanvas($canvasPageInput: CanvasPageInput!) {
        canvasPageCreate(canvasPageInput: $canvasPageInput) {
            id
            seo {
                url
            }
            canvas
        }
    }
`;

const templateFnc = (value, styles, title) => {
  let styleString = "";
  styles.forEach((style) => {
    styleString += style.outerHTML;
  });
  return `
<html>
    <head>
        <title>${title}</title>
        ${styleString}
        <script type="module" src="https://unpkg.com/@corejam/core-components@latest/web-components/index.js"></script>
        <script nomodule src="https://unpkg.com/@corejam/core-components@latest/web-components/index.cjs"></script>
    </head>
    <body>
        ${value}
    </body>
</html>
`;
};

const CorejamDeploy = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
  }
  async deploy(e) {
    e.preventDefault();
    const styleTags = document.querySelectorAll("style");
    const stringifiedHtml = document.getElementsByTagName("corejam-canvas")[0].outerHTML;
    const name = window.prompt("name");
    if (name) {
      const request = await core.coreState.client.mutate({
        mutation: index$1.src(PostCanvasGQL),
        variables: {
          canvasPageInput: {
            canvas: JSON.stringify(templateFnc(stringifiedHtml, styleTags, name)),
            seo: {
              url: name,
            },
          },
        },
      });
      if (request.data.canvasPageCreate.id) {
        runStore.runState.routes = [
          {
            url: `/${name}`,
            canvasPage: true,
            component: JSON.stringify(stringifiedHtml),
            exact: true,
          },
        ];
        runStore.runState.router.push(`/${name}`);
      }
    }
  }
  render() {
    return (index.h(index.Host, null, index.h("corejam-box", { flex: true, direction: "col", pt: 24 }, index.h("corejam-type", { as: "h3", size: "lg", weight: "hairline" }, "Deploy to CDN"), index.h("corejam-button", { w: "6", bg: "green-200", hoverBg: "green-300", p: "2", color: "gray-700", hoverColor: "white", onPointerUp: this.deploy }, "Deploy"))));
  }
};

exports.corejam_deploy = CorejamDeploy;
