import { navigation } from "@saasquatch/component-boilerplate";
import { useState } from "@saasquatch/stencil-hooks";
import { h } from "@stencil/core";
import { createHookStory } from "../sqm-stencilbook/HookStoryAddon";
export default {
  title: "Tests/Router",
};
const templates = `
    <template path="/foo"><h1>foo</h1>
    
    <p>These elements don't produce a specific box by themselves. They are replaced by their pseudo-box and their child boxes. Please note that the CSS Display Level 3 spec defines how the contents value should affect "unusual elements" — elements that aren’t rendered purely by CSS box concepts such as replaced elements. See Appendix B: Effects of display: contents on Unusual Elements for more details.

    Due to a bug in browsers this will currently remove the element from the accessibility tree — screen readers will not look at what's inside. See the Accessibility concerns section below for more details.</p>
    </template>
    <template path="/bar"><h2>bar</h2>
    
    <p>Turns off the display of an element so that it has no effect on layout (the document is rendered as though the element did not exist). All descendant elements also have their display turned off.
    To have an element take up the space that it would normally take, but without actually rendering anything, use the visibility property instead.</p>
    </template>
    <template path="/baz/bang"><h3>baz/bang</h3>
    <pre>
    ▄▄▄▄▄▄▄░▄▄▄▄▄▄▄░▄▄▄▄▄▄░▄▄▄▄▄
    ░░▀███░░░░▀██░░░░██▀░░░░██░░
    ░░░▀██░░░░░▀██░░▄█░░░░░▄█░░░
    ░░░░███░░░░░▀██▄█░░░░░░█░░░░
    ░░░░░███░░░░░▀██░░░░░░█▀░░░░
    ░░░░░░███░░░░▄███░░░░█▀░░░░░
    ░░░░░░░██▄░░▄▀░███░░█▀░░░░░░
    ░░░░░░░▀██▄█▀░░░███▄▀░░░░░░░
    ░░░░░░░░▀██▀░░░░░███░░░░░░░░
    ░░░░░░░░░▀▀░░░░░░░▀░░░░░░░░░
    </pre>
    </template>
    <template path="/refer/:page"><h3>/refer/:page</h3>
    <pre>
      refer/:page
    </pre>
    </template>
`;
const routes = `
    <sqm-route path="/foo"><h1>foo</h1>
    
    <p>These elements don't produce a specific box by themselves. They are replaced by their pseudo-box and their child boxes. Please note that the CSS Display Level 3 spec defines how the contents value should affect "unusual elements" — elements that aren’t rendered purely by CSS box concepts such as replaced elements. See Appendix B: Effects of display: contents on Unusual Elements for more details.

    Due to a bug in browsers this will currently remove the element from the accessibility tree — screen readers will not look at what's inside. See the Accessibility concerns section below for more details.</p>
    </sqm-route>
    <sqm-route path="/bar"><h2>bar</h2>
    <p>Turns off the display of an element so that it has no effect on layout (the document is rendered as though the element did not exist). All descendant elements also have their display turned off.
    To have an element take up the space that it would normally take, but without actually rendering anything, use the visibility property instead.</p>
    </sqm-route>
    <sqm-route path="/baz/bang"><h3>baz/bang</h3>
    <pre>
    ▄▄▄▄▄▄▄░▄▄▄▄▄▄▄░▄▄▄▄▄▄░▄▄▄▄▄
    ░░▀███░░░░▀██░░░░██▀░░░░██░░
    ░░░▀██░░░░░▀██░░▄█░░░░░▄█░░░
    ░░░░███░░░░░▀██▄█░░░░░░█░░░░
    ░░░░░███░░░░░▀██░░░░░░█▀░░░░
    ░░░░░░███░░░░▄███░░░░█▀░░░░░
    ░░░░░░░██▄░░▄▀░███░░█▀░░░░░░
    ░░░░░░░▀██▄█▀░░░███▄▀░░░░░░░
    ░░░░░░░░▀██▀░░░░░███░░░░░░░░
    ░░░░░░░░░▀▀░░░░░░░▀░░░░░░░░░
    </pre>
    </sqm-route>
    <sqm-route path="/refer/:page"><h3>/refer/:page</h3>
    <pre>
      refer/:page
    </pre>
    </sqm-route>
`;
export const TemplateNavigation = createHookStory(() => {
  return (h("div", null,
    h("button", { onClick: () => navigation.push("/") }, "/"),
    h("button", { onClick: () => navigation.push("/foo") }, "/foo"),
    h("button", { onClick: () => navigation.push("/bar") }, "/bar"),
    h("button", { onClick: () => navigation.push("/baz/bang") }, "/baz/bang"),
    h("button", { onClick: () => navigation.push("/refer") }, "/refer"),
    h("button", { onClick: () => navigation.push("/refer/1") }, "/refer/1"),
    h("button", { onClick: () => navigation.push("/refer/2") }, "/refer/2"),
    h("button", { onClick: () => navigation.back() }, "Back"),
    h("button", { onClick: () => navigation.forward() }, "Forward"),
    h("hr", null),
    h("sqm-router", { innerHTML: templates })));
});
export const RouteNavigation = createHookStory(() => {
  return (h("div", null,
    h("button", { onClick: () => navigation.push("/") }, "/"),
    h("button", { onClick: () => navigation.push("/foo") }, "/foo"),
    h("button", { onClick: () => navigation.push("/bar") }, "/bar"),
    h("button", { onClick: () => navigation.push("/baz/bang") }, "/baz/bang"),
    h("button", { onClick: () => navigation.push("/refer") }, "/refer"),
    h("button", { onClick: () => navigation.push("/refer/1") }, "/refer/1"),
    h("button", { onClick: () => navigation.push("/refer/2") }, "/refer/2"),
    h("button", { onClick: () => navigation.back() }, "Back"),
    h("button", { onClick: () => navigation.forward() }, "Forward"),
    h("hr", null),
    h("sqm-router", { innerHTML: routes })));
});
export const Styling = createHookStory(() => {
  return (h("div", null,
    h("button", { onClick: () => navigation.push("/") }, "/"),
    h("button", { onClick: () => navigation.push("/foo") }, "/foo"),
    h("hr", null),
    h("div", { style: { display: "flex", justifyContent: "space-around" } },
      h("sqm-router", { innerHTML: `<template path="/">
      
      <div>Column 1</div>
      <div>Column 2 </div>
      </template>` }))));
});
function useTemplate(templateString) {
  const [editedTemplate, setEditedTemplate] = useState(templateString);
  const [previewTemplate, setPreviewTemplate] = useState(templateString);
  function setPath(e) {
    //@ts-ignore
    navigation.push(e.target.value);
  }
  return {
    states: { previewTemplate, editedTemplate },
    callbacks: { setEditedTemplate, setPreviewTemplate, setPath },
  };
}
const defaultRouter = `
<sqm-router>
  <template path="/">
  <div>hello world</div>
  </template>
</sqm-router>
`;
function TemplateView(props) {
  const { states, callbacks } = props;
  return [
    h("textarea", { style: { width: "100%", height: "300px" }, onChange: (e) => callbacks.setEditedTemplate(e.target.value) }, states.editedTemplate),
    h("button", { onClick: () => callbacks.setPreviewTemplate(states.editedTemplate) }, "Update Preview"),
    h("label", null,
      "Current path:",
      h("input", { onInput: callbacks.setPath, value: "/" })),
    h("div", { innerHTML: states.previewTemplate }),
  ];
}
export const RouterPlayground = createHookStory(() => {
  const { states, callbacks } = useTemplate(defaultRouter);
  return h(TemplateView, { states: states, callbacks: callbacks });
});
