import React, { useContext } from "react";
import { SHAPE_TYPE } from "./../../ricos-content/index.js";
import { EditorContext, ModalServiceContext } from "./../../ricos-context/index.js";
import { getShapeStyles } from "./../../_shared/shape-select-modal/get-shape-styles.js";
import { ShapeSelectModal as SharedShapeSelectModal } from "./../../_shared/shape-select-modal/shape-select-modal.js";
import { RICOS_DEFAULTS } from "../utils/consts.js";
import { SHAPE_SELECT_MODAL } from "../utils/data-hooks.js";
const ShapeSelectModal = ({
  nodeId,
  getShapeUrl
}) => {
  const { getPublicCommands } = useContext(EditorContext);
  const modalService = useContext(ModalServiceContext) || {};
  const closeModal = () => modalService.closeModal(SHAPE_SELECT_MODAL);
  const loadShapeStyles = async (src, targetNodeId) => {
    const colorMap = await getShapeStyles(src, getShapeUrl);
    getPublicCommands().updateBlock(targetNodeId, SHAPE_TYPE, {
      styles: {
        colors: colorMap
      },
      isLoading: false
    });
  };
  const onShapeSelect = async (shape) => {
    const shapeData = shape.assets?.vector?.image;
    if (!shapeData) {
      console.error("Shape data not found");
      return;
    }
    closeModal();
    const src = shapeData.id ? { id: shapeData.id } : { url: shapeData.url };
    const baseShapeData = {
      shape: { src },
      styles: { colors: {} }
    };
    if (nodeId) {
      getPublicCommands().updateBlock(nodeId, SHAPE_TYPE, {
        ...baseShapeData,
        isLoading: true,
        dimensions: void 0
      });
      await loadShapeStyles(src, nodeId);
      return;
    }
    const newNodeId = getPublicCommands().insertBlockWithBlankLines(SHAPE_TYPE, {
      ...baseShapeData,
      containerData: RICOS_DEFAULTS.containerData,
      isLoading: true
    });
    await loadShapeStyles(src, newNodeId);
  };
  return /* @__PURE__ */ React.createElement(SharedShapeSelectModal, { onShapeSelect, onClose: closeModal });
};
export {
  ShapeSelectModal
};
//# sourceMappingURL=shape-select-modal.js.map
