type JsonObject = Record<string, any>

export function pickFirstNonEmpty(values: unknown[]) {
  for (const value of values) {
    const normalized = String(value || "").trim();
    if (normalized) {
      return normalized;
    }
  }

  return "";
}

export function isPlainObject(value: unknown): value is JsonObject {
  return Boolean(value) && typeof value === "object" && !Array.isArray(value);
}

export function resolveKuaishouEticketAdminContext(payload: JsonObject = {}, options: JsonObject = {}) {
  const savedSource = options.savedSource || {};
  const findShopConfig =
    (options.findShopConfig || (() => null)) as (shopId: string, source: JsonObject) => unknown;
  const getFirstAvailableShop =
    (options.getFirstAvailableShop || (() => null)) as (source: JsonObject) => unknown;
  const defaultBaseUrl = String(
    options.defaultBaseUrl || "https://s.kwaixiaodian.com"
  ).trim();
  const requestedShopId = String(payload.shopId || "").trim();
  const configuredShop = (requestedShopId
    ? findShopConfig(requestedShopId, savedSource)
    : getFirstAvailableShop(savedSource)) as JsonObject | null;

  return {
    baseUrl: pickFirstNonEmpty([
      payload.baseUrl,
      savedSource.baseUrl,
      defaultBaseUrl,
    ]),
    cookie: pickFirstNonEmpty([payload.cookie, configuredShop?.cookie]),
    shopId: pickFirstNonEmpty([requestedShopId, configuredShop?.shopId]),
    shop: configuredShop,
  };
}

import { getCloudtentaclesSourceByKey } from "../../platforms/cloudtentacles/source-config-service.js";
import { getCloudtentaclesSessionStateByKey } from "../../platforms/cloudtentacles/session-state-service.js";

export function resolveCloudtentaclesAdminContext(payload: JsonObject = {}, options: JsonObject = {}) {
  const sourceKey = String(
    payload.sourceKey || options.sourceKey || "default"
  ).trim();
  const savedSource = options.savedSource || getCloudtentaclesSourceByKey(sourceKey) || {};
  const persistedSession = options.persistedSession ||
    getCloudtentaclesSessionStateByKey(sourceKey) ||
    {};
  const defaultBaseUrl = String(
    options.defaultBaseUrl || "https://123.207.217.176"
  ).trim();
  return {
    sourceKey,
    baseUrl: pickFirstNonEmpty([
      payload.baseUrl,
      savedSource.baseUrl,
      defaultBaseUrl,
    ]),
    token: pickFirstNonEmpty([payload.token, persistedSession.token]),
    deviceId: pickFirstNonEmpty([
      payload.deviceId,
      savedSource.deviceId,
      persistedSession.deviceId,
      "-",
    ]),
    deviceType:
      payload.deviceType ??
      savedSource.deviceType ??
      persistedSession.deviceType,
  };
}

export function hasCloudtentaclesCredentialContextChanged(
  current: JsonObject = {},
  next: JsonObject = {}
) {
  return (
    String(current.baseUrl || "").trim() !==
      String(next.baseUrl || "").trim() ||
    String(current.username || "").trim() !==
      String(next.username || "").trim() ||
    String(current.phone || "").trim() !== String(next.phone || "").trim() ||
    String(current.deviceId || "").trim() !==
      String(next.deviceId || "").trim() ||
    Number(current.deviceType || 0) !== Number(next.deviceType || 0)
  );
}
