/**
 * Minimal shim for `openclaw/plugin-sdk`.
 *
 * The `@tencent-weixin/openclaw-weixin` npm package imports from
 * `openclaw/plugin-sdk` which is normally provided by the OpenClaw host.
 * VisionClaw provides this lightweight replacement so the package's
 * transport modules (api, cdn, auth, messaging, media) resolve without
 * installing the full OpenClaw runtime.
 *
 * Only the transport-layer functions are implemented faithfully; the
 * heavy framework functions (used in process-message.ts / channel.ts)
 * are no-op stubs since VisionClaw replaces those modules entirely.
 */

// ---------------------------------------------------------------------------
// Runtime functions used by transport modules we actually import
// ---------------------------------------------------------------------------

export function normalizeAccountId(id: string): string {
  return id.replace(/[^a-z0-9-]/gi, "-").toLowerCase();
}

export function stripMarkdown(text: string): string {
  return text
    .replace(/\*\*(.+?)\*\*/g, "$1")
    .replace(/__(.+?)__/g, "$1")
    .replace(/\*(.+?)\*/g, "$1")
    .replace(/_(.+?)_/g, "$1")
    .replace(/~~(.+?)~~/g, "$1")
    .replace(/^#{1,6}\s+/gm, "")
    .replace(/^[*-]\s+/gm, "")
    .replace(/^\d+\.\s+/gm, "");
}

export async function withFileLock<T>(
  _path: string,
  _opts: unknown,
  fn: () => T | Promise<T>,
): Promise<T> {
  return fn();
}

export function buildChannelConfigSchema(schema: unknown): { schema: unknown } {
  return { schema };
}

// ---------------------------------------------------------------------------
// Stub functions used only by process-message.ts / channel.ts
// (VisionClaw never imports those modules -- these exist solely so the
//  package's other modules don't fail to load due to re-exports.)
// ---------------------------------------------------------------------------

export function createTypingCallbacks(_opts: unknown): {
  start: () => Promise<void>;
  stop: () => Promise<void>;
} {
  return {
    start: async () => {},
    stop: async () => {},
  };
}

export function resolveSenderCommandAuthorizationWithRuntime(
  _opts: unknown,
): Promise<{ senderAllowedForCommands: boolean; commandAuthorized: boolean }> {
  return Promise.resolve({ senderAllowedForCommands: true, commandAuthorized: true });
}

export function resolveDirectDmAuthorizationOutcome(
  _opts: unknown,
): "allowed" {
  return "allowed";
}

export function resolvePreferredOpenClawTmpDir(): string {
  return "/tmp/openclaw";
}

export function loadConfig(): Record<string, unknown> {
  return {};
}

export async function writeConfigFile(_config: unknown): Promise<void> {}

// ---------------------------------------------------------------------------
// Type stubs — permissive so all transport code compiles
// ---------------------------------------------------------------------------

export type OpenClawConfig = Record<string, unknown> & {
  channels?: Record<string, unknown>;
};
export type OpenClawPluginApi = Record<string, unknown>;
export type PluginRuntime = Record<string, unknown>;
export type ChannelPlugin<T = unknown> = Record<string, unknown> & { id: string; _phantom?: T };
export type ChannelAccountSnapshot = Record<string, unknown>;
export type ReplyPayload = { text?: string; mediaUrl?: string; mediaUrls?: string[] };
