import chalk from "chalk";
function getEnv(key, defaultValue) {
  const value = process.env[key];
  if (value === void 0) {
    if (defaultValue !== void 0) return defaultValue;
    throw new Error(`Environment variable ${key} is required`);
  }
  return value;
}
function getOptionalEnv(key) {
  return process.env[key];
}
function registerLinearTestCommand(parent) {
  parent.command("linear:test").description("Test Linear API connection").option("--api-key <key>", "Linear API key").action(async (options) => {
    try {
      const apiKey = options.apiKey || process.env["LINEAR_API_KEY"];
      if (!apiKey) {
        console.log(chalk.red("\u274C No API key provided"));
        console.log(
          "Use --api-key or set LINEAR_API_KEY environment variable"
        );
        return;
      }
      console.log(chalk.yellow("\u{1F504} Testing Linear connection..."));
      const response = await fetch("https://api.linear.app/graphql", {
        method: "POST",
        headers: {
          Authorization: apiKey,
          "Content-Type": "application/json"
        },
        body: JSON.stringify({
          query: `
              query Me {
                viewer {
                  id
                  name
                  email
                }
                teams {
                  nodes {
                    id
                    key
                    name
                  }
                }
              }
            `
        })
      });
      if (!response.ok) {
        console.log(
          chalk.red(`\u274C API Error: ${response.status} ${response.statusText}`)
        );
        return;
      }
      const result = await response.json();
      if (result.errors) {
        console.log(chalk.red("\u274C GraphQL Errors:"));
        result.errors.forEach(
          (err) => console.log(`  - ${err.message}`)
        );
        return;
      }
      if (result.data?.viewer) {
        console.log(chalk.green("\u2705 Connected to Linear!"));
        console.log(chalk.cyan("\n\u{1F464} User:"));
        console.log(`  Name: ${result.data.viewer.name}`);
        console.log(`  Email: ${result.data.viewer.email}`);
      }
      if (result.data?.teams?.nodes?.length > 0) {
        console.log(chalk.cyan("\n\u{1F3E2} Teams:"));
        result.data.teams.nodes.forEach((team) => {
          console.log(`  - ${team.name} (${team.key})`);
        });
      }
      console.log(chalk.yellow("\n\u{1F504} Testing issue creation..."));
      if (result.data?.teams?.nodes?.length > 0) {
        const teamId = result.data.teams.nodes[0].id;
        const createResponse = await fetch("https://api.linear.app/graphql", {
          method: "POST",
          headers: {
            Authorization: apiKey,
            "Content-Type": "application/json"
          },
          body: JSON.stringify({
            query: `
                mutation CreateIssue($input: IssueCreateInput!) {
                  issueCreate(input: $input) {
                    success
                    issue {
                      id
                      identifier
                      title
                      url
                    }
                  }
                }
              `,
            variables: {
              input: {
                teamId,
                title: "[TEST] StackMemory Integration Test",
                description: "This is a test issue created by StackMemory to verify Linear integration.",
                priority: 4
                // Low priority
              }
            }
          })
        });
        const createResult = await createResponse.json();
        if (createResult.data?.issueCreate?.success) {
          const issue = createResult.data.issueCreate.issue;
          console.log(chalk.green("\u2705 Test issue created successfully!"));
          console.log(`  ID: ${issue.identifier}`);
          console.log(`  Title: ${issue.title}`);
          console.log(`  URL: ${issue.url}`);
          console.log(
            chalk.gray("\n(You can delete this test issue from Linear)")
          );
        } else {
          console.log(chalk.yellow("\u26A0 Could not create test issue"));
        }
      }
      console.log(chalk.green("\n\u2705 Linear integration test completed!"));
    } catch (error) {
      console.error(chalk.red("\u274C Test failed:"), error.message);
      process.exit(1);
    }
  });
}
export {
  registerLinearTestCommand
};
//# sourceMappingURL=linear-test.js.map
