import React, { useState } from "react";
import { RenameIcon } from "../../icons/ContextMenuIcons";
import {
  useLeftData,
  useTopData,
} from "../../../store/visualization/useZone3DWidgetStore";
import { useRenameModeStore } from "../../../store/builder/store";

type RenameTooltipProps = {
  name: string;
  onSubmit: (newName: string) => void;
};

const RenameTooltip: React.FC<RenameTooltipProps> = ({ name, onSubmit }) => {
  const [value, setValue] = useState(name);

  const { top, setTop } = useTopData();
  const { left, setLeft } = useLeftData();

  const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
    e.preventDefault();
    onSubmit(value.trim());
    setTop(0);
    setLeft(0);
  };

  return (
    <div
      className="rename-tool-tip"
      style={{
        top: `${top}px`,
        left: `${left}px`,
      }}
    >
      <div className="header">
        <div className="icon">
          <RenameIcon />
        </div>
        <div className="name">Name</div>
      </div>
      <form className="input" onSubmit={handleSubmit}>
        <input
          type="text"
          value={value}
          onChange={(e) => setValue(e.target.value)}
          autoFocus
        />
      </form>
    </div>
  );
};

export default RenameTooltip;
