'use strict';

const yar = require('@btst/yar');
const sitemapUtils = require('../packages/stack/src/client/sitemap-utils.cjs');
const metaUtils = require('../packages/stack/src/client/meta-utils.cjs');
const pathUtils = require('../packages/stack/src/client/path-utils.cjs');

function createStackClient(config) {
  const { plugins, basePath } = config;
  const allRoutes = {};
  const clientStackContext = {
    plugins,
    basePath
  };
  for (const [pluginKey, plugin] of Object.entries(plugins)) {
    const pluginRoutes = plugin.routes(clientStackContext);
    Object.assign(allRoutes, pluginRoutes);
  }
  const router = yar.createRouter(allRoutes);
  return {
    router,
    async generateSitemap() {
      const sitemapEntries = [];
      for (const plugin of Object.values(plugins)) {
        if (typeof plugin.sitemap === "function") {
          const entries = await plugin.sitemap();
          if (Array.isArray(entries)) sitemapEntries.push(...entries);
        }
      }
      const seen = /* @__PURE__ */ new Set();
      const deduped = [];
      for (const entry of sitemapEntries) {
        if (!entry?.url || seen.has(entry.url)) continue;
        seen.add(entry.url);
        deduped.push(entry);
      }
      return deduped;
    }
  };
}

exports.sitemapEntryToXmlString = sitemapUtils.sitemapEntryToXmlString;
exports.metaElementsToObject = metaUtils.metaElementsToObject;
exports.normalizePath = pathUtils.normalizePath;
exports.createStackClient = createStackClient;
