// src/migrations/message-channels-to-service-channels.ts
import fs from "fs";
import { glob } from "glob";
import os from "os";
import matter from "gray-matter";
import path from "path";
var DISABLE_CHANNEL_MIGRATION_ENV = "EVENTCATALOG_DISABLE_CHANNEL_MIGRATION";
var isChannelMigrationDisabled = () => {
  return ["true", "1", "yes"].includes((process.env[DISABLE_CHANNEL_MIGRATION_ENV] ?? "").toLowerCase());
};
var message_channels_to_service_channels_default = async (dir) => {
  if (isChannelMigrationDisabled()) {
    return { status: "skipped", message: `Channel migration disabled by ${DISABLE_CHANNEL_MIGRATION_ENV}` };
  }
  const PROJECT_DIR = path.join(dir || process.env.PROJECT_DIR);
  const messages = await glob(
    [
      "**/events/*/index.mdx",
      "**/events/*/index.md",
      "**/events/*/versioned/*/index.mdx",
      "**/events/*/versioned/*/index.md",
      // commands
      "**/commands/*/index.mdx",
      "**/commands/*/index.md",
      "**/commands/*/versioned/*/index.mdx",
      "**/commands/*/versioned/*/index.md",
      // queries
      "**/queries/*/index.mdx",
      "**/queries/*/index.md",
      "**/queries/*/versioned/*/index.mdx",
      "**/queries/*/versioned/*/index.md"
    ],
    {
      // const events = await glob(['**/events/*/index.(md|mdx)', '**/events/*/versioned/*/index.(md|mdx)'], {
      cwd: PROJECT_DIR,
      absolute: true,
      nodir: true,
      windowsPathsNoEscape: os.platform() == "win32",
      ignore: ["node_modules/**", "**/dist/**", "**/teams", "**/users", "**/package.json", "**/Dockerfile"]
    }
  );
  const services = await glob(
    [
      "**/services/*/index.mdx",
      "**/services/*/index.md",
      "**/services/*/versioned/*/index.mdx",
      "**/services/*/versioned/*/index.md"
    ],
    {
      cwd: PROJECT_DIR,
      absolute: true
    }
  );
  const messagesWithChannels = messages.reduce((acc, message) => {
    const file = fs.readFileSync(message, "utf8");
    const { data } = matter(file);
    if (data.channels?.length > 0) {
      acc.push({
        ...data,
        path: message
      });
    }
    return acc;
  }, []);
  if (messagesWithChannels.length === 0) {
    return { status: "success", message: "No messages with channels found in the catalog" };
  }
  const servicesWithChannels = services.reduce((acc, service) => {
    const file = fs.readFileSync(service, "utf8");
    const { data } = matter(file);
    const isSending = data.sends?.some((send) => messagesWithChannels.some((message) => message.id === send.id));
    const isReceiving = data.receives?.some(
      (receive) => messagesWithChannels.some((message) => message.id === receive.id)
    );
    if (isSending || isReceiving) {
      acc.push({
        ...data,
        path: service
      });
    }
    return acc;
  }, []);
  for (const service of servicesWithChannels) {
    const file = fs.readFileSync(service.path, "utf8");
    const { data } = matter(file);
    const messagesTheServiceSendsThatNeedUpdating = data.sends?.filter((send) => messagesWithChannels.some((message) => message.id === send.id)) ?? [];
    const messagesTheServiceReceivesThatNeedUpdating = data.receives?.filter((receive) => messagesWithChannels.some((message) => message.id === receive.id)) ?? [];
    if (messagesTheServiceSendsThatNeedUpdating.length > 0 || messagesTheServiceReceivesThatNeedUpdating.length > 0) {
      const newSends = (data.sends ?? []).map((send) => {
        const match = messagesWithChannels.find((message) => message.id === send.id);
        if (!match) return send;
        return { ...send, to: (match.channels ?? []).filter((channel) => channel !== null) };
      });
      const newReceives = (data.receives ?? []).map((receive) => {
        const match = messagesWithChannels.find((message) => message.id === receive.id);
        if (!match) return receive;
        return { ...receive, from: (match.channels ?? []).filter((channel) => channel !== null) };
      });
      const newData = {
        ...data,
        ...newSends.length > 0 ? { sends: newSends } : {},
        ...newReceives.length > 0 ? { receives: newReceives } : {}
      };
      fs.writeFileSync(service.path, matter.stringify(file, newData));
    }
  }
  for (const message of messagesWithChannels) {
    const file = fs.readFileSync(message.path, "utf8");
    const parsed = matter(file);
    const { channels, ...newData } = parsed.data;
    fs.writeFileSync(message.path, matter.stringify(parsed.content, newData));
  }
  console.log("\u2714 Channels migrated to new services API");
};

export {
  message_channels_to_service_channels_default
};
