"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var Breadcrumb_stories_exports = {};
__export(Breadcrumb_stories_exports, {
  Breadcrumb: () => Breadcrumb,
  default: () => Breadcrumb_stories_default
});
module.exports = __toCommonJS(Breadcrumb_stories_exports);
var import_react = __toESM(require("react"));
var import_styled_components = __toESM(require("styled-components"));
var import_Text = __toESM(require("../../asorted/Text"));
var import_Flex = __toESM(require("../../layout/Flex"));
var import_index = __toESM(require("./index"));
var Breadcrumb_stories_default = {
  title: "Navigation/Breadcrumb",
  component: import_index.default
};
const routes = [
  {
    path: "settings",
    label: "Settings",
    routes: [
      { path: "experimental", label: "Experimental" },
      { path: "developer", label: "Developer" }
    ]
  },
  {
    path: "account",
    label: "Account",
    routes: [{ path: "user", label: "User", routes: [{ path: "edit", label: "Edit" }] }]
  },
  {
    path: "apps",
    label: "Applications",
    routes: [
      { path: "buy", label: "Buy" },
      { path: "sell", label: "Sell" },
      { path: "swap", label: "Swap", routes: [{ path: "confirm", label: "Confirm" }] }
    ]
  }
];
const RouteElement = import_styled_components.default.div`
  &:hover {
    text-decoration: underline;
  }
`;
const Routes = ({
  onClick,
  routes: routes2,
  previousPath = []
}) => {
  if (!routes2 || routes2.length === 0)
    return null;
  return /* @__PURE__ */ import_react.default.createElement("div", { style: { paddingLeft: "1em" } }, routes2.map((route) => /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(RouteElement, { onClick: () => onClick([...previousPath, route.path].join("/")) }, route.label, " [", "/" + route.path, "]"), /* @__PURE__ */ import_react.default.createElement(
    Routes,
    {
      routes: route.routes,
      previousPath: [...previousPath, route.path],
      onClick
    }
  ))));
};
const Breadcrumb = (args) => {
  const [route, setRoute] = (0, import_react.useState)("apps/swap/confirm");
  const onChange = (0, import_react.useCallback)((value) => setRoute(value.join("/")), [setRoute]);
  const segments = (0, import_react.useMemo)(() => {
    const paths = route.split("/");
    const [, result] = paths.reduce(
      ([routes2, result2], path) => {
        let activeRoute = null;
        const level = routes2.map((route2) => {
          if (route2.path === path) {
            activeRoute = route2;
          }
          return {
            value: route2.path,
            label: route2.label
          };
        });
        if (!activeRoute) {
          throw new Error("Cannot match path.");
        }
        const nextRoutes = activeRoute.routes || [];
        if (level.length === 1) {
          result2.push(level[0]);
        } else {
          result2.push({
            options: level,
            value: {
              value: activeRoute.path,
              label: activeRoute.label
            }
          });
        }
        return [nextRoutes, result2];
      },
      [routes, []]
    );
    return result;
  }, [route]);
  return /* @__PURE__ */ import_react.default.createElement(import_Flex.default, { rowGap: 3, flexDirection: "column" }, /* @__PURE__ */ import_react.default.createElement(import_index.default, { ...args, segments, onChange }), /* @__PURE__ */ import_react.default.createElement("hr", null), /* @__PURE__ */ import_react.default.createElement(import_Text.default, { fontWeight: "medium", variant: "paragraph" }, "Current route: /", route), /* @__PURE__ */ import_react.default.createElement(import_Text.default, { fontWeight: "medium", variant: "paragraph" }, "Available routes:"), /* @__PURE__ */ import_react.default.createElement(import_Text.default, { fontWeight: "regular", variant: "paragraph", style: { cursor: "pointer" } }, /* @__PURE__ */ import_react.default.createElement(Routes, { routes, onClick: setRoute })));
};
//# sourceMappingURL=Breadcrumb.stories.js.map
