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

export const javaReservedWords = [
  "abstract",
  "assert",
  "boolean",
  "break",
  "byte",
  "case",
  "catch",
  "char",
  "class",
  "const",
  "continue",
  "default",
  "do",
  "double",
  "else",
  "enum",
  "extends",
  "final",
  "finally",
  "float",
  "for",
  "if",
  "implements",
  "import",
  "instanceof",
  "int",
  "interface",
  "long",
  "native",
  "new",
  "null",
  "package",
  "private",
  "protected",
  "public",
  "return",
  "short",
  "static",
  "strictfp",
  "super",
  "switch",
  "synchronized",
  "this",
  "throw",
  "throws",
  "transient",
  "try",
  "void",
  "volatile",
  "while"
];

let stringResources = [
  "app_name",
  "nav_open",
  "nav_close",
  "something_went_wrong",
  "welcome",
  "email",
  "invalid_email",
  "invalid_password",
  "invalid_email_password",
  "name",
  "username",
  "password",
  "confirm_password",
  "password_does_not_match",
  "i_agree_to_user_policy_terms",
  "login",
  "sign_in",
  "create_account",
  "already_have_an_account",
  "auth_sign_up",
  "don_t_have_an_account",
  "back",
  "sign_up",
  "add_button",
  "type_message",
  "send_button", 
  "send_image",
  "image", 
  "send_video",
  "open_camera",
  "date", 
  "open_dialog"
];

export default (name, fragments) => `<resources>
    <string name="app_name">${name}</string>s
    <string name="nav_open">Open</string>
    <string name="nav_close">Close</string>
    <string name="something_went_wrong">Something went wrong</string>
    <string name="welcome">Welcome</string>
    <string name="email">Email</string>
    <string name="invalid_email">Invalid Email</string>
    <string name="invalid_password">Invalid Password</string>
    <string name="forget_password">Forget Password</string>
    <string name="invalid_email_password">Invalid Email Or Password</string>
    <string name="name">Name</string>
    <string name="username">Username</string>
    <string name="password">Password</string>
    <string name="confirm_password">Confirm Password</string>
    <string name="password_does_not_match">Password does not match</string>
    <string name="i_agree_to_user_policy_terms">I agree to Terms of Service</string>
    <string name="login">Login</string>
    <string name="sign_in">Sign in</string>
    <string name="create_account">Create Account</string>
    <string name="already_have_an_account">Already have an account?</string>
    <string name="auth_sign_up">Sign Up</string>
    <string name="sign_up">Sign Up</string>
    <string name="don_t_have_an_account">"Don't have an account?"</string>
    <string name="back">"Back"</string>
    <string name="empty_location">Empty Location not allowed</string>
    <string name="try_again">Try again</string>
    <string name="google">Continue with Google</string>
    <string name="apple">Continue with Apple</string>
    ${buildStrings(fragments)}
    <string name="add_button">Add button</string>
    <string name="type_message">Type message</string>
    <string name="send_button">send button</string>
    <string name="send_image">send image</string>
    <string name="image">Image</string>
    <string name="send_video">Send video</string>
    <string name="open_camera">Open camera</string>
    <string name="date">Date</string>
    <string name="open_dialog">Open Dialog</string>
  </resources>
  `;


function buildStrings(fragments) {
  const strings = extractTextProperties(fragments);
  const uniqueSet = new Set(strings);

  const uniqueArray = Array.from(uniqueSet);

  let content = [];
  for (let string of uniqueArray) {
    if (typeof string === "undefined") continue;
    let stringName = StringCaser(string, {
      casetype: "lowercase",
      separator: "_",
    });
  
    if ((stringName && !isNaN(stringName[0])) || javaReservedWords.includes(stringName)) stringName = "_" + stringName;
    if (stringResources.find((item) => item === stringName)) continue;
    if (stringName == null && stringResources.includes("_null")) continue;
    if (stringName == null) {
      stringName = "_null";
      string = "_null";
    }
    stringResources.push(stringName);
    content.push(`
      <string name="${stringName}">${string}</string>
    `);
  }
  return content.join("\n");
};

function extractTextProperties(fragments) {
  let textProperties = [];
  for (const fragment of fragments) {
    const strings = fragment.components.flatMap((component) => {
      if (component.children?.length) {
        return component.props?.text
          ? getTextFromChildren(component.children).concat(component.props.text)
          : getTextFromChildren(component.children);
      } else {
        return component.props?.text ?? "default text";
      }
    });

    textProperties = textProperties.concat(strings);
  }
  return textProperties;
}

function getTextFromChildren(children) {
  return children.flatMap((child) => {
    if (child.children?.length) {
      return child.props?.text
        ? getTextFromChildren(child.children).concat(child.props.text)
        : getTextFromChildren(child.children);
    } else {
      return child.props?.text ?? "default text";
    }
  });
};
