<template>
  <div :style="{ width: props.dimensions.width * props.ratioWidth + 'px', height: props.dimensions.height * props.ratioHeight + 'px' }">
    <NodeResizer @resizeEnd="(e) => $emit('resize', e)" color="#fff" v-if="!props.isView && !props.isHideHandle && props.selected" @resize="resize" />

    <div
      class="custom-node"
      :style="{
        textAlign: props.data.options.align,
        width: props.dimensions.width * props.ratioWidth + 'px',
        lineHeight: props.dimensions.height * props.ratioHeight + 'px',
        height: props.dimensions.height * props.ratioHeight + 'px',
        pointerEvents: props.isView ? 'auto' : 'none'
      }"
    >
      <span
        :style="{
          whiteSpace: props.data.options.whiteSpace || 'nowrap',
          color: props.data.options.color,
          fontSize: props.dimensions.height * props.ratioHeight + 'px'
        }"
        >{{ apiData.text || props.inputData?.text || props.data.options.text }}</span
      >
    </div>
    <Handle v-if="!props.isView" :id="`${props.id}.-t`" type="target" :position="Position.Left" />
  </div>
</template>
<script setup>
import { defineEmits, defineProps, ref } from 'vue';
import { NodeResizer } from '@vue-flow/node-resizer';
import { Handle, Position } from '@vue-flow/core';
import { getData } from '../../api/getData.js';

const props = defineProps({
  ratioWidth: {
    type: Number,
    required: false,
    default: 1
  },
  ratioHeight: {
    type: Number,
    required: false,
    default: 1
  },
  isView: {
    type: Boolean,
    required: false
  },
  inputData: {
    type: Object,
    required: false
  },
  id: {
    type: String,
    required: true
  },
  isHideHandle: {
    type: Boolean,
    required: false
  },
  selected: {
    type: Boolean,
    required: false
  },
  data: {
    type: Object,
    required: true
  },
  dimensions: {
    type: Object,
    required: true
  }
});

const  apiData = ref({})

onMounted(async () => {
  await getData({ request: props.data.options.request, inputData: props.inputData }).then((e) => {
    apiData.value =e
  });
});

watch(
  () => [JSON.parse(JSON.stringify(props.data.options.request ||'[]')), JSON.parse(JSON.stringify(props.inputData || ''))],
  async (obj1, obj2) => {
    if (JSON.stringify(obj1) !== JSON.stringify(obj2)) {
      await getData({ request: props.data.options.request||[], inputData: props.inputData }).then((e) => {
        apiData.value =e
      });
    }
  },
  { deep: true, immediate: true }
);
const emit = defineEmits(['resize']);
const resize = (e) => {
  emit('resize', e, props.id);
};
</script>
<style scoped>
.custom-node {
  position: absolute;
}
</style>
