import { ref } from "vue";
import { defineStore } from "pinia";

export const usePositionKeycloakStore = defineStore("positionKeycloak", () => {
  const dataPositionKeycloak = ref<any>(null);

  function setPositionKeycloak(data: any) {
    dataPositionKeycloak.value = data;
  }

  function findOrgName(obj: any) {
    if (obj) {
      let name =
        obj.child4 != null &&
        obj.child4 !== "" &&
        obj.child3 != null &&
        obj.child3 !== ""
          ? obj.child4 + (obj.child3 ? "/" : "")
          : obj.child4 != null && obj.child4 !== ""
          ? obj.child4
          : "";

      name +=
        obj.child3 != null &&
        obj.child3 !== "" &&
        obj.child2 != null &&
        obj.child2 !== ""
          ? obj.child3 + (obj.child2 ? "/" : "")
          : obj.child3 != null && obj.child3 !== ""
          ? obj.child3
          : "";

      name +=
        obj.child2 != null &&
        obj.child2 !== "" &&
        obj.child1 != null &&
        obj.child1 !== ""
          ? obj.child2 + (obj.child1 ? "/" : "")
          : obj.child2 != null && obj.child2 !== ""
          ? obj.child2
          : "";

      name +=
        obj.child1 != null &&
        obj.child1 !== "" &&
        obj.root != null &&
        obj.root !== ""
          ? obj.child1 + (obj.root ? "/" : "")
          : obj.child1 != null && obj.child1 !== ""
          ? obj.child1
          : "";
      name += obj.root != null && obj.root !== "" ? obj.root : "";
      return name == "" ? "-" : name;
    } else {
      return "";
    }
  }

  return { dataPositionKeycloak, setPositionKeycloak, findOrgName };
});
