import { LinearClient } from "@linear/sdk";
import { logger } from "../../core/monitoring/logger.js";
class LinearPlugin {
  name = "linear-integration";
  version = "1.0.0";
  description = "Linear task synchronization for StackMemory";
  client = null;
  apiKey = null;
  async initialize() {
    this.apiKey = process.env["LINEAR_API_KEY"] || null;
    if (!this.apiKey) {
      logger.info("Linear plugin: No API key found, running in offline mode");
      return;
    }
    try {
      this.client = new LinearClient({ apiKey: this.apiKey });
      await this.client.me;
      logger.info("Linear plugin initialized successfully");
    } catch (error) {
      logger.warn("Linear plugin: Failed to connect", error);
      this.client = null;
    }
  }
  async shutdown() {
    this.client = null;
    logger.info("Linear plugin shut down");
  }
  registerCommands() {
    return [
      {
        name: "linear-sync",
        description: "Sync tasks with Linear",
        action: this.syncTasks.bind(this),
        options: [
          {
            flag: "--team <id>",
            description: "Linear team ID"
          },
          {
            flag: "--project <id>",
            description: "Linear project ID"
          }
        ]
      },
      {
        name: "linear-create",
        description: "Create a Linear issue from current context",
        action: this.createIssue.bind(this),
        options: [
          {
            flag: "--title <title>",
            description: "Issue title"
          },
          {
            flag: "--description <desc>",
            description: "Issue description"
          }
        ]
      }
    ];
  }
  registerTools() {
    return [
      {
        name: "linear_sync",
        description: "Sync current context with Linear tasks",
        inputSchema: {
          type: "object",
          properties: {
            teamId: { type: "string", description: "Linear team ID" },
            projectId: { type: "string", description: "Linear project ID" }
          }
        },
        handler: async (input) => {
          return await this.syncTasks(input);
        }
      },
      {
        name: "linear_create_issue",
        description: "Create a Linear issue",
        inputSchema: {
          type: "object",
          properties: {
            title: { type: "string" },
            description: { type: "string" },
            teamId: { type: "string" }
          },
          required: ["title"]
        },
        handler: async (input) => {
          return await this.createIssue(input);
        }
      }
    ];
  }
  onFrameCreated(frame) {
    if (frame.type === "task" && this.client) {
      this.syncFrameToLinear(frame).catch((error) => {
        logger.debug("Failed to sync frame to Linear", error);
      });
    }
  }
  async syncTasks(options) {
    if (!this.client) {
      return { success: false, message: "Linear not connected" };
    }
    try {
      const issues = await this.client.issues({
        filter: {
          team: { id: { eq: options.teamId } }
        }
      });
      return {
        success: true,
        synced: issues.nodes.length,
        message: `Synced ${issues.nodes.length} issues from Linear`
      };
    } catch (error) {
      logger.error("Linear sync failed", error);
      return { success: false, error: error.message };
    }
  }
  async createIssue(options) {
    if (!this.client) {
      return { success: false, message: "Linear not connected" };
    }
    try {
      const issue = await this.client.createIssue({
        title: options.title,
        description: options.description,
        teamId: options.teamId || await this.getDefaultTeamId()
      });
      return {
        success: true,
        issueId: issue.issue?.id,
        url: issue.issue?.url,
        message: `Created Linear issue: ${issue.issue?.identifier}`
      };
    } catch (error) {
      logger.error("Failed to create Linear issue", error);
      return { success: false, error: error.message };
    }
  }
  async syncFrameToLinear(frame) {
    if (frame.name && frame.content) {
      await this.createIssue({
        title: frame.name,
        description: frame.content
      });
    }
  }
  async getDefaultTeamId() {
    if (!this.client) throw new Error("Linear not connected");
    const teams = await this.client.teams();
    if (teams.nodes.length > 0) {
      return teams.nodes[0].id;
    }
    throw new Error("No Linear teams found");
  }
}
var linear_default = LinearPlugin;
export {
  LinearPlugin,
  linear_default as default
};
//# sourceMappingURL=index.js.map
