import { emptySection } from "../components/Home/projectsConst";
import { EditSectionData } from "../components/Modals/EditSectionModal";
import { Scopes } from "../definitionsNew/fields";
import { sectionFormExclude } from "../definitionsNew/serializaConfigs";
import { IProject } from "../store/interface/projects";
import { IOrgFiledsConfigs, UserContract } from "../store/interface/user";
import { ListSectionProps } from "../types/sectionTypes";

interface IGetProjectParamsReturn {
  projectName: string;
  contractID: string;
}
// ToDo check and probalby delete
export const getProjectParams = (
  projects: IProject[],
  id: string
): IGetProjectParamsReturn => {
  const params: IGetProjectParamsReturn = {
    projectName: "",
    contractID: "",
  };
  const project = projects.find((item) => item.projectID === id);
  if (project) {
    params.projectName = project.projectName as string;
    params.contractID = project.contractID as string;
  }
  return params;
};

export const reduceProjectList = (projects: IProject[], id: string) => {
  return projects.filter((item) => item.projectID !== id);
};

interface IUpdateProjectData {
  id: string;
  projectName: string;
}
export const updateProjectDetails = (
  projects: IProject[],
  updateSet: IUpdateProjectData
) => {
  const updatedProject = projects.find(
    (item) => item.projectID === updateSet.id
  );
  if (updatedProject) {
    updatedProject.projectName = updateSet.projectName;
  }
  return projects;
};

//Todo to change for newGetSectionObj and delete this  after test!!1
export const getSectionObj = (
  sections: ListSectionProps[],
  id: string
): ListSectionProps => {
  const section = sections.find((item) => item.sectionID === id);
  return section ? section : emptySection;
};

//ToDo create test for this
export const newGetSectionObj = (
  sections: ListSectionProps[],
  configs: IOrgFiledsConfigs[],
  id: string
): ListSectionProps => {
  let sectionDetails: Record<string, any> = {
    sectionID: id,
  };

  const config = configs
    .find((_config) => _config.configName === Scopes.AddSectionForm)
    ?.config.filter((_item) => !sectionFormExclude.includes(_item.id));

  const section = sections.find((item) => item.sectionID === id);

  if (config && section) {
    config.forEach((_item) => {
      sectionDetails[_item.id] = section[_item.id];
    });
  }

  return sectionDetails;
};

export const reduceSectionList = (sections: ListSectionProps[], id: string) => {
  return sections.filter((item) => item.sectionID !== id);
};

export const updateSectionDetails = (
  sections: ListSectionProps[],
  updateSet: EditSectionData
) => {
  const updatedSection = sections.find(
    (item) => item.sectionID === updateSet.sectionID
  );
  if (updatedSection) {
    updatedSection.sectionName = updateSet.sectionName;
    updatedSection.kks = updateSet.kks;
  }
  return sections;
};

export const getContractName = (
  contractID: string,
  contracts: UserContract[]
) => {
  if (contracts.length === 0) {
    return "";
  }
  return (
    contracts.find((_contract) => _contract.contractID === contractID)
      ?.contractName || ""
  );
};

export const getContactsNames = (
  projects: IProject[],
  contracts: UserContract[]
) => {
  projects.forEach((_project) => {
    _project.contractName = getContractName(
      _project.contractID as string,
      contracts
    );
  });

  return projects;
};

export const getSectionDetails = (
  sectionID: string,
  sections: ListSectionProps[]
) => {
  const defaultDetails = {
    sectionName: "",
    file: "",
    fileName: "",
    kks: "",
    revision: 1,
  };
  const foundSection = sections.find(
    (section) => section.sectionID === sectionID
  );

  if (foundSection) {
    return foundSection;
  } else {
    return defaultDetails;
  }
};

export const setDefaultSectionData = (
  sectionState: Record<string, string | number | undefined | File>
) => {
  sectionState.revision = 1;
  return sectionState;
};
