import { StringCaser } from "@/components/WebExportReact/src/utils/helpers";

export function generateComponentId(component) {
  // button with id 35a0c712-981d-292d-1f39-67690b8e0545 should generate button_35a0c712
  let id = StringCaser(component?.name, {
    separator: "_",
    casetype: "lowercase",
  });
  id += "_";
  id += component?.id.split("-")[0];
  return id;
}

export function generateComponentId2(component) {
  // button with id 35a0c712-981d-292d-1f39-67690b8e0545 should generate button_35a0c712
  let id = StringCaser(component?.name, {
    separator: "_",
    casetype: "lowercase",
  });
  id += component?.id.split("-")[0];
  return id;
}

export function getComponentViewBindingId(component) {
  // button with id 35a0c712-981d-292d-1f39-67690b8e0545 should generate button35a0c712
  let id = StringCaser(component.name, {
    separator: "_",
    casetype: "lowercase",
  });
  id += StringCaser(component.id.split("-")[0], { casetype: "capitalize" });

  return id;
}
// let set = new Set()
export const getType = (type) => {
  if (typeof type === "undefined") return "String";
  switch (type) {
    case "Object":
      return "Map<String, Any>";
    case "Integer":
      return "Int";
    case "decimal":
      return "Number?";
    case "decimal?":
      return "Number?";
    case "Number":
      return "Number?";
    case "Integer?":
      return "Int?";
    case "Int":
      return "Int?";
    case "Boolean":
      return "Boolean?";
    case "String?":
      return `String?`;
    case "Array":
      return "ArrayList<Map<String, Any>>";
    case "":
      return "Any";

    default:
      return "String?";
  }
  // Int Number Boolean Integer
};

export const getDefinitionType = (type) => {
  if (typeof type === "undefined") return "String";
  switch (type) {
    case "Object":
      return "Map<String, Any>";
    case "Integer":
      return "Int";
    case "Integer?":
      return "Int? = 0";
    case "String?":
      return `String? = ""`;
    case "Array":
      return "ArrayList<Map<String, Any>>";

    case "decimal":
      return "Number?";
    case "decimal?":
      return "Number?";
    case "Number":
      return "Number?";
    case "Int":
      return "Int?";
    case "Boolean":
      return "Boolean?";
    case "":
      return "Any";
    default:
      return "String?";
  }
};
// default:
//   return type;

//replace :param with {param}
export function replaceEndpointFormat(endpoint) {
  return endpoint.replace(/:[^/]+/g, (match) => `{${match.substring(1)}}`);
}

export function findComponentsByKeyValue(data, key, value, result = []) {
  if (!Array.isArray(data)) data = [data];
  for (const item of data) {
    if ("components" in item) {
      findComponentsByKeyValue(item.components, key, value, result);
    }
    if ("children" in item) {
      findComponentsByKeyValue(item.children, key, value, result);
    }
    if (key in item && item[key] == value) {
      result.push(item);
    }
  }
  return result;
}

export function findComponentsWithFileUploadAction(data, result = []) {
  if (!Array.isArray(data)) data = [data];
  for (const item of data) {
    if ("components" in item) {
      findComponentsWithFileUploadAction(item.components, result);
    }
    if ("children" in item) {
      findComponentsWithFileUploadAction(item.children, result);
    }
    if ("props" in item && item.props.onclick == "file_upload") {
      result.push(item);
    }
  }
  return result;
}
