import { applyOptionalStringField } from "./domain.js";

type JsonObject = Record<string, any>

export function buildAdminAgisoShopConfigUpdate(item: JsonObject, current: JsonObject = {}) {
  const shopId = String(item?.shopId || "").trim();
  if (!shopId) {
    return null;
  }

  const next: JsonObject = { ...current };
  const shopName = String(item?.shopName || "").trim();
  const accessToken = String(item?.accessToken || "").trim();
  const messageTemplate = String(item?.messageTemplate || "").trim();
  const autoDeliveryMessageTemplate = String(
    item?.autoDeliveryMessageTemplate || ""
  ).trim();
  const appSecret = String(item?.appSecret || "").trim();
  const apiVersion = String(item?.apiVersion || "").trim();
  const sendMessageEndpoint = String(item?.sendMessageEndpoint || "").trim();

  if (shopName) {
    next.shopName = shopName;
  } else {
    delete next.shopName;
  }
  if (accessToken) {
    next.accessToken = accessToken;
  }
  if (messageTemplate) {
    next.messageTemplate = messageTemplate;
  } else if (typeof item?.messageTemplate === "string") {
    delete next.messageTemplate;
  }
  if (autoDeliveryMessageTemplate) {
    next.autoDeliveryMessageTemplate = autoDeliveryMessageTemplate;
  } else if (typeof item?.autoDeliveryMessageTemplate === "string") {
    delete next.autoDeliveryMessageTemplate;
  }
  if (appSecret) {
    next.appSecret = appSecret;
  }
  if (apiVersion) {
    next.apiVersion = apiVersion;
  }
  if (sendMessageEndpoint) {
    next.sendMessageEndpoint = sendMessageEndpoint;
  }
  if (typeof item?.enabled === "boolean") {
    next.enabled = item.enabled;
  }

  if (!next.accessToken) {
    return null;
  }

  return { shopId, config: next };
}

export function buildAdminAgisoMessagingSavePayload(
  payload: JsonObject = {},
  options: JsonObject = {}
) {
  const rawItems = Array.isArray(payload.shops) ? payload.shops : [];
  const currentDefaults: JsonObject = options.currentDefaults || {};
  const currentMap: Record<string, JsonObject> = options.currentMap || {};
  const nextDefaults: JsonObject = {
    ...currentDefaults,
  };
  const nextMap: Record<string, JsonObject> = {};

  applyOptionalStringField(nextDefaults, "messageTemplate", payload.defaults);
  applyOptionalStringField(
    nextDefaults,
    "autoDeliveryMessageTemplate",
    payload.defaults
  );

  for (const item of rawItems) {
    const shopId = String(item?.shopId || "").trim();
    if (!shopId) {
      continue;
    }

    const updated = buildAdminAgisoShopConfigUpdate(
      item,
      currentMap[shopId] || {}
    );
    if (!updated) {
      continue;
    }

    nextMap[updated.shopId] = updated.config;
  }

  return {
    defaults: nextDefaults,
    shops: nextMap,
  };
}

export function normalizeAdminCloudtentaclesSourceConfigPayload(payload: JsonObject = {}) {
  const sourceKey =
    String(payload.sourceKey || payload.key || "").trim() || "default";
  return {
    key: sourceKey,
    label: String(payload.label || "").trim(),
    enabled: payload.enabled !== false,
    baseUrl: String(payload.baseUrl || "").trim() || "https://123.207.217.176",
    username: String(payload.username || "").trim(),
    password: String(payload.password || "").trim(),
    phone: String(payload.phone || "").trim(),
    deviceId: String(payload.deviceId || "-").trim() || "-",
    deviceType: Number(payload.deviceType || 0),
  };
}
