import { ref, computed } from 'vue';
import { useLocale } from '../use-locale/index.mjs';
import '../../shared/index.mjs';
import useTimeout from '../private/use-timeout.mjs';
import { kebabCase } from '../../utils/util.mjs';
import DrawStatus from '../../shared/src/DrawStatus.mjs';

function useDrawingAction(props, ctx, instance, cmpName, $services) {
  instance.cesiumClass = cmpName;
  instance.cesiumEvents = [];
  const { t } = useLocale();
  const { emit } = ctx;
  const tips = kebabCase(cmpName).split("-");
  if (cmpName === "VcMeasurementDistance" && props.showComponentLines) {
    tips[2] = "component-distance";
  }
  if (cmpName === "VcDrawingRegular" || cmpName === "VcMeasurementRegular") {
    if (props.edge === 4) {
      tips[2] = "rectangle";
    }
    if (props.edge === 360) {
      tips[2] = "circle";
    }
  }
  let drawingType = tips[2];
  tips[3] && (drawingType = `${tips[2]}-${tips[3]}`);
  const drawTip = ref("");
  const drawTipOpts = computed(() => {
    return {
      drawingTipStart: props.drawtip.drawingTipStart || t(`${tips[0]}.${tips[1]}.${tips[2]}.drawingTipStart`),
      drawingTipEnd: props.drawtip.drawingTipEnd || t(`${tips[0]}.${tips[1]}.${tips[2]}.drawingTipEnd`),
      drawingTipEditing: props.drawtip.drawingTipEditing || t(`${tips[0]}.${tips[1]}.${tips[2]}.drawingTipEditing`)
    };
  });
  const drawStatus = ref(DrawStatus.BeforeDraw);
  const canShowDrawTip = ref(false);
  const drawTipPosition = ref([0, 0, 0]);
  const showEditor = ref(false);
  const editorPosition = ref([0, 0, 0]);
  const mouseoverPoint = ref(null);
  const editingPoint = ref(null);
  const primitiveCollectionRef = ref(null);
  const editorType = ref("");
  const { registerTimeout, removeTimeout } = useTimeout();
  instance.createCesiumObject = async () => {
    return primitiveCollectionRef;
  };
  const onMouseoverPoints = (e) => {
    var _a, _b;
    const { drawingHandlerActive, viewer } = $services;
    if (props.editable && drawStatus.value !== DrawStatus.Drawing && drawingHandlerActive) {
      e.pickedFeature.primitive.pixelSize = ((_a = props.pointOpts) == null ? void 0 : _a.pixelSize) * 1.5;
      removeTimeout();
      registerTimeout(() => {
        mouseoverPoint.value = e.pickedFeature.primitive;
        editorPosition.value = e.pickedFeature.primitive.position;
        showEditor.value = true;
        canShowDrawTip.value = false;
        drawTipPosition.value = [0, 0, 0];
      }, (_b = props.editorOpts) == null ? void 0 : _b.delay);
    }
    emit("mouseEvt", {
      type: e.type,
      name: drawingType,
      target: e
    }, viewer);
  };
  const onMouseoutPoints = (e) => {
    var _a, _b;
    const { viewer, selectedDrawingActionInstance } = $services;
    if (props.editable) {
      e.pickedFeature.primitive.pixelSize = ((_a = props.pointOpts) == null ? void 0 : _a.pixelSize) * 1;
      removeTimeout();
      registerTimeout(() => {
        editorPosition.value = [0, 0, 0];
        mouseoverPoint.value = void 0;
        showEditor.value = false;
      }, (_b = props.editorOpts) == null ? void 0 : _b.hideDelay);
      selectedDrawingActionInstance && (canShowDrawTip.value = true);
    }
    emit("mouseEvt", {
      type: e.type,
      name: drawingType,
      target: e
    }, viewer);
  };
  const onMouseenterEditor = (evt) => {
    removeTimeout();
  };
  const onMouseleaveEditor = (evt) => {
    var _a;
    removeTimeout();
    registerTimeout(() => {
      var _a2;
      editorPosition.value = [0, 0, 0];
      mouseoverPoint.value.pixelSize = ((_a2 = props.pointOpts) == null ? void 0 : _a2.pixelSize) * 1;
      mouseoverPoint.value = void 0;
      showEditor.value = false;
    }, (_a = props.editorOpts) == null ? void 0 : _a.hideDelay);
  };
  const onPrimitiveCollectionReady = (readyObj) => {
    ;
    readyObj.cesiumObject._vcId = cmpName;
  };
  const onVcCollectionPointReady = function(e) {
    const { cesiumObject: pointPrimitiveCollection } = e;
    const originalUpdate = pointPrimitiveCollection.update;
    pointPrimitiveCollection.update = function(frameState) {
      const originalLength = frameState.commandList.length;
      originalUpdate.call(this, frameState);
      const endLength = frameState.commandList.length;
      for (let i = originalLength; i < endLength; ++i) {
        frameState.commandList[i].pass = Cesium["Pass"].TRANSLUCENT;
        frameState.commandList[i].renderState = Cesium["RenderState"].fromCache({
          depthTest: {
            enabled: false
          },
          depthMask: false
        });
      }
    };
  };
  const onVcCollectionLabelReady = (e) => {
    if (props.disableDepthTest)
      return;
    const labelCollection = e.cesiumObject;
    const originalUpdate = labelCollection.update;
    labelCollection.update = function(frameState) {
      const originalLength = frameState.commandList.length;
      originalUpdate.call(this, frameState);
      const endLength = frameState.commandList.length;
      for (let i = originalLength; i < endLength; ++i) {
        frameState.commandList[i].pass = Cesium["Pass"].OVERLAY;
        frameState.commandList[i].renderState = Cesium["RenderState"].fromCache({
          depthTest: {
            enabled: false
          },
          depthMask: false,
          blending: Cesium.BlendingState.ALPHA_BLEND
        });
      }
    };
  };
  const onVcPrimitiveReady = (e) => {
    if (props.disableDepthTest)
      return;
    const primitive = e.cesiumObject;
    const originalPrimitiveUpdate = primitive.update;
    primitive.update = function(frameState) {
      const originalLength = frameState.commandList.length;
      originalPrimitiveUpdate.call(this, frameState);
      const endLength = frameState.commandList.length;
      for (let i = originalLength; i < endLength; ++i) {
        if (frameState.commandList[i].pass !== Cesium["Pass"].TRANSLUCENT) {
          continue;
        }
        frameState.commandList[i].pass = Cesium["Pass"].OPAQUE;
        frameState.commandList[i].renderState = Cesium["RenderState"].fromCache({
          depthTest: {
            enabled: false
          },
          depthMask: false,
          blending: Cesium.BlendingState.ALPHA_BLEND
        });
      }
    };
  };
  return {
    drawingType,
    drawTip,
    drawTipOpts,
    drawStatus,
    canShowDrawTip,
    drawTipPosition,
    showEditor,
    editorPosition,
    mouseoverPoint,
    editingPoint,
    primitiveCollectionRef,
    editorType,
    onMouseoverPoints,
    onMouseoutPoints,
    onMouseenterEditor,
    onMouseleaveEditor,
    onPrimitiveCollectionReady,
    onVcCollectionPointReady,
    onVcPrimitiveReady,
    onVcCollectionLabelReady
  };
}

export { useDrawingAction as default };
//# sourceMappingURL=use-drawing-action.mjs.map
