import React, { FC } from "react";
import { Aligment, Modal, Text } from "venice-ui";
import { t } from "../../helpers/userHelper";
import { dictionary } from "../../untils/dictionary";
import { SiteProps } from "../Section/Section";
import { ifHaveReferences, isReferencePoint } from "../../helpers/weldHelpers";
import { IReferenceElement } from "../../definitions/element";
import { useStoreState } from "../../store";
import { DeleteElementFrame } from "../components";
import {
  ADDITIONAL_TYPES,
  TAdditionalType,
} from "../../definitions/additional";

interface IDeleteModalProps {
  handleCancel: () => any;
  handleDelete: (
    id: string,
    isMapeElement: boolean,
    isReference: boolean,
  ) => void;
  id: string;
  name: string;
  type: string;
  isOpen: boolean;
}

export const DeleteModal: FC<IDeleteModalProps> = ({
  handleCancel,
  handleDelete,
  name,
  id,
  type,
  isOpen,
}) => {
  const { references } = useStoreState(
    (state) => state.projectDetails.projectDetails,
  );

  const deleteElement = (id: string) => {
    handleDelete(id, type !== "joint", isReferencePoint(id, references));
  };

  
  const setDeleteTitle = () => {
    return ifHaveReferences(id, references)
      ? t(dictionary.deleteWeldHeader)
      : t(dictionary.deleteWeldHeader);
  };
  
  const setDeleteContent = () => {
    return ifHaveReferences(id, references)
      ? t(dictionary.doYouWantDeletePointWithReferences)
      : t(dictionary.doYouWantDeletePoint);
  };

  const getName = (type: TAdditionalType) => {
    switch (type) {
      case ADDITIONAL_TYPES.dot:
        return t(dictionary.mapElementDot);
      case ADDITIONAL_TYPES.edge:
        return t(dictionary.mapElementLine);
      case ADDITIONAL_TYPES.triage:
        return t(dictionary.mapElementTriage);
      case ADDITIONAL_TYPES.cloud:
        return t(dictionary.mapElementCloud);
    }
  };
  return (
    <Modal
      title={t(dictionary.confirmDelete)}
      handleClose={handleCancel}
      handleConfirm={() => deleteElement(id)}
      isOpen={isOpen}
      exitOnEsc={true}
      labels={{
        primary: t(dictionary.delete),
        secondary: t(dictionary.cancel),
      }}
      headerDivader={true}
      primaryVariant="danger"
    >
      <Aligment
        direction="column"
        align="flex-start"
        justify="flex-start"
        gap={16}
      >
        <Text>
          {type !== "joint"
            ? t(dictionary.deleteElementHeader)
            : setDeleteTitle()}
        </Text>

        <DeleteElementFrame>
          {type === "joint" ? name : getName(type as TAdditionalType)}
        </DeleteElementFrame>
        <Text>
          {type !== "joint"
            ? t(dictionary.doYouWantDeleteElement)
            : setDeleteContent()}
        </Text>
      </Aligment>
    </Modal>
  );
};
