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

<script lang="ts" setup>
import { SerialData, getMouseStyle, defaultStyle, matResponse } from "./index.ts";
import { PropertyUpdate, Operate } from "../../html-mount/propertys/index.ts";
import { TempComponent } from "./";
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";

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

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

const { shape, tData, data, operateMenus, describes } = useComponentStatus<
  Ellipse,
  SerialData
>({
  emit,
  props,
  getMouseStyle,
  defaultStyle,
  alignment: (data, mat) => {
    return matResponse({ mat: mat.multiply(new Transform(data.mat)), data });
  },
  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,
        };
      },
      // beforeHandler(data, mat) {
      //   return matToData(copy(data), mat);
      // },
      handler(data, mat) {
        matResponse({ data, mat }, initRadius);
        return true;
      },
      callback,
      transformerConfig: {
        rotateEnabled: false,
        enabledAnchors: ["top-left", "top-right", "bottom-left", "bottom-right"],
      },
    });
  },
  copyHandler(mat, data) {
    return matResponse({ data, mat: mat.multiply(new Transform(data.mat)) });
  },
  propertys: [
    "fill",
    "stroke",
    "fontColor",
    "strokeWidth",
    // "fontSize",
    // "ref",
    "opacity",
    "dash",
    //  "zIndex"
    "fontStyle",
    // "ref", "zIndex"
  ],
});
</script>
