import { createRequire as _createRequire } from 'node:module';
const require = _createRequire(import.meta.url);
import {
  isCliError,
  isCliSystemError,
  require_is_ci,
  wixCliCliCommand,
  wixCliCliCommandStatus
} from "./chunk-PVENT7M4.js";
import {
  __toESM,
  init_esm_shims
} from "./chunk-PYIAC2GK.js";

// ../cli-telemetry/src/add-user-fields.ts
init_esm_shims();
import { randomUUID } from "node:crypto";
function addUserFields(logger, userId) {
  logger.updateDefaults(resolveUserFields(userId));
}
function resolveUserFields(userId) {
  if (!userId) {
    return {
      _client_id: randomUUID()
    };
  }
  return {
    _client_id: userId,
    _uuid: userId,
    logged_user_id: userId,
    logged_account_id: userId,
    target_account_id: userId
  };
}

// ../cli-telemetry/src/command-bi-event.ts
init_esm_shims();
import { uptime } from "node:process";

// ../cli-telemetry/src/environment.ts
init_esm_shims();
var import_is_ci = __toESM(require_is_ci(), 1);
import os from "node:os";
import { platform, version as nodeVersion } from "node:process";
function getEnvironmentInfo({
  cliVersion,
  flow
}) {
  return {
    ...flow ? { flow } : {},
    cliVersion,
    isCI: import_is_ci.default,
    nodeVersion,
    osName: getOsName(),
    osVersion: os.release()
  };
}
function getOsName() {
  switch (platform) {
    case "darwin":
      return "mac";
    case "win32":
      return "windows";
    default:
      return "linux";
  }
}

// ../cli-telemetry/src/command-bi-event.ts
var getDuration = () => Math.round(uptime() * 1e3);
function reportCommandStartEvent({
  biLogger,
  command,
  flow,
  cliVersion,
  projectId,
  extensions,
  extraFields
}) {
  const options = command.opts();
  const args = command.args;
  const privateOptions = /* @__PURE__ */ new Set(["apiKey"]);
  const sanitizedOptions = Object.fromEntries(
    Object.entries(options).map(([flagName, flagValue]) => {
      const value = privateOptions.has(flagName) ? "<censored>" : flagValue;
      return [flagName, value];
    })
  );
  const commandFields = {
    command: command.name(),
    arguments: args.join(","),
    flags: Object.keys(sanitizedOptions).length ? JSON.stringify(sanitizedOptions) : ""
  };
  void biLogger.report(
    wixCliCliCommand({
      ...getEnvironmentInfo({ cliVersion, flow }),
      ...commandFields,
      ...projectId ? { projectId } : {},
      ...extraFields ? { extraFields: JSON.stringify(extraFields) } : {},
      isValid: true
    })
  );
  return {
    succeed() {
      void biLogger.report(
        wixCliCliCommandStatus({
          ...commandFields,
          ...extensions ? { extensions: JSON.stringify(extensions) } : {},
          duration: getDuration(),
          isSuccess: true,
          isValid: true
        })
      );
    },
    failed(error) {
      const errorFields = isCliError(error) ? {
        errorType: isCliSystemError(error) ? "SystemError" : "UserError",
        errorName: error.name,
        errorMessage: error.message
      } : error instanceof Error ? {
        errorType: "UnknownError",
        errorName: error.name,
        errorMessage: error.message
      } : {
        errorType: "UnknownError",
        errorMessage: JSON.stringify(error)
      };
      void biLogger.report(
        wixCliCliCommandStatus({
          ...commandFields,
          ...errorFields,
          ...extensions ? { extensions: JSON.stringify(extensions) } : {},
          duration: getDuration(),
          isSuccess: false,
          isValid: true
        })
      );
    }
  };
}

// ../cli-telemetry/src/index.ts
init_esm_shims();

export {
  addUserFields,
  reportCommandStartEvent
};
//# sourceMappingURL=chunk-AMTFUUHP.js.map