// src/lib/analytics/index.ts
import { PostHog } from "posthog-node";
import * as os2 from "os";
import * as crypto from "crypto";

// src/lib/project/config.ts
import * as fs from "fs";
import * as path from "path";
var PROJECT_ID_FILE = ".specific/project_id";
var ProjectNotLinkedError = class extends Error {
  constructor() {
    super(
      `Project not linked to Specific.

The file ${PROJECT_ID_FILE} does not exist.

Run: specific deploy`
    );
    this.name = "ProjectNotLinkedError";
  }
};
function readProjectId(projectDir = process.cwd()) {
  const projectIdPath = path.join(projectDir, PROJECT_ID_FILE);
  if (!fs.existsSync(projectIdPath)) {
    throw new ProjectNotLinkedError();
  }
  const projectId = fs.readFileSync(projectIdPath, "utf-8").trim();
  if (!projectId) {
    throw new Error(`${PROJECT_ID_FILE} is empty`);
  }
  return projectId;
}
function hasProjectId(projectDir = process.cwd()) {
  const projectIdPath = path.join(projectDir, PROJECT_ID_FILE);
  return fs.existsSync(projectIdPath);
}

// src/lib/auth/credentials.ts
import * as fs2 from "fs";
import * as path2 from "path";
import * as os from "os";

// src/lib/auth/login.tsx
import React from "react";
import { render, Box, Text } from "ink";
import Spinner from "ink-spinner";

// src/lib/auth/credentials.ts
function getUserCredentialsDir() {
  return path2.join(os.homedir(), ".specific");
}
function getCredentialsPath() {
  return path2.join(getUserCredentialsDir(), "credentials.json");
}
function readUserCredentials() {
  const credentialsPath = getCredentialsPath();
  if (!fs2.existsSync(credentialsPath)) {
    return null;
  }
  try {
    const content = fs2.readFileSync(credentialsPath, "utf-8");
    return JSON.parse(content);
  } catch {
    return null;
  }
}
function getUserId() {
  const credentials = readUserCredentials();
  return credentials?.userId ?? null;
}

// src/lib/analytics/index.ts
var POSTHOG_HOST = "https://eu.i.posthog.com";
var client = null;
var anonymousId = null;
var identified = false;
function isEnabled() {
  return true;
}
function getAnonymousId() {
  if (anonymousId) return anonymousId;
  const machineId = `${os2.hostname()}-${os2.userInfo().username}`;
  anonymousId = crypto.createHash("sha256").update(machineId).digest("hex").slice(0, 16);
  return anonymousId;
}
function getProjectId() {
  try {
    if (hasProjectId()) {
      return readProjectId();
    }
  } catch {
  }
  return void 0;
}
function getClient() {
  if (!isEnabled()) return null;
  if (!client) {
    client = new PostHog("phc_qNQCEUXh6ErdciQRRmeG2xlVvwFjkcW6A5bnOFJ8vXZ", {
      host: POSTHOG_HOST,
      flushAt: 1,
      // Flush immediately for CLI
      flushInterval: 0
    });
  }
  return client;
}
function trackEvent(event, properties) {
  const ph = getClient();
  if (!ph) return;
  const userId = getUserId();
  if (userId && !identified) {
    identified = true;
    ph.identify({
      distinctId: userId,
      properties: {
        anonymous_id: getAnonymousId()
      }
    });
    ph.alias({
      distinctId: userId,
      alias: getAnonymousId()
    });
  }
  ph.capture({
    distinctId: userId ?? getAnonymousId(),
    event,
    properties: {
      ...properties,
      cli_version: "0.1.72",
      platform: process.platform,
      node_version: process.version,
      project_id: getProjectId()
    }
  });
}
async function shutdown() {
  if (client) {
    await client.shutdown();
    client = null;
  }
}

// src/postinstall.ts
async function main() {
  try {
    trackEvent("cli_installed");
    await shutdown();
  } catch {
  }
  console.log();
  console.log("Thanks for installing Specific!");
  console.log("Get started: https://docs.specific.dev/");
  console.log();
}
main();
