<template>
  <TempCircle
    :data="tData"
    :ref="(v: any) => shape = v?.shape"
    :id="data.id"
    editer
    @update-content="updateContent"
  />
  <PropertyUpdate
    :describes="describes"
    :data="data"
    :target="shape"
    @change="emit('updateShape', { ...data })"
    @delete="emit('delShape')"
  />
  <Operate :target="shape" :menus="operateMenus" />
</template>

<script lang="ts" setup>
import { CircleData, getMouseStyle, defaultStyle, matResponse, fixedStrokeOptions } from "./index.ts";
import { PropertyUpdate, Operate } from "../../html-mount/propertys/index.ts";
import TempCircle from "./temp-circle.vue";
import { useComponentStatus } from "@/core/hook/use-component.ts";
import { Transform } from "konva/lib/Util";
import { cloneRepShape, useCustomTransformer } from "@/core/hook/use-transformer.ts";
import { Ellipse } from "konva/lib/shapes/Ellipse";
import { Pos } from "@/utils/math.ts";
import { copy } from "@/utils/shared.ts";
import { setShapeTransform } from "@/utils/shape.ts";
import { useInstallStrokeWidthDescribe } from "@/core/hook/use-describe.ts";

const props = defineProps<{ data: CircleData }>();
const emit = defineEmits<{
  (e: "updateShape", value: CircleData): void;
  (e: "addShape", value: CircleData): void;
  (e: "delShape"): void;
}>();

const updateContent = (val: string) => {
  data.value.content = val;
  emit("updateShape", { ...data.value });
};

const { shape, tData, data, operateMenus, describes } = useComponentStatus<
  Ellipse,
  CircleData
>({
  emit,
  props,
  getMouseStyle,
  defaultStyle,
  // alignment: (data, mat) => {
  //   return matResponse({ data, mat: mat.multiply(new Transform(data.mat)) });
  // },
  transformType: "custom",
  customTransform(callback, shape, data) {
    let initRadius: Pos;
    useCustomTransformer(shape, data, {
      openSnap: true,
      getRepShape($shape) {
        const repShape = cloneRepShape($shape).shape;
        initRadius = { x: repShape.radiusX(), y: repShape.radiusY() };
        return {
          shape: repShape,
          update(data, shape) {
            repShape.radiusX(data.radiusX);
            repShape.radiusY(data.radiusY);
            setShapeTransform(repShape, new Transform(data.mat));
            initRadius = { x: repShape.radiusX(), y: repShape.radiusY() };
          },
        };
      },
      beforeHandler(data, mat) {
        return matResponse({ data: copy(data), mat });
      },
      handler(data, mat) {
        matResponse({ data, mat }, initRadius);
        return true;
      },
      callback,
    });
  },
  copyHandler(mat, data) {
    return matResponse({ data: { ...data }, mat: mat.multiply(new Transform(data.mat)) });
  },
  propertys: [
    "fill",
    "stroke",
    "fontColor",
    "strokeWidth",
    "fontSize",
    // "ref",
    // "opacity",
    // "dash",
    //  "zIndex"
    "align",
    "fontStyle",
    // "ref", "zIndex"
  ],
});
useInstallStrokeWidthDescribe(describes, data, fixedStrokeOptions);

</script>
