<template>
  <row-bar :color="builder.color">
    <color-band :color="builder.color"/>
    <slot/>
    <div class="align-self-center ml-auto mr-3 trashcan">
      <v-btn icon="mdi-delete" @click="deleteMyBuilder"/>
    </div>
  </row-bar>
</template>

<script setup>
import {builderFuncs, deleteBuilder} from "@/orderbuild.js";
import ColorBand from "@/components/chart/ColorBand.vue";
import RowBar from "@/components/chart/RowBar.vue";
import {onBeforeUnmount, onMounted, onUnmounted, onUpdated, watchEffect} from "vue";

const props = defineProps({
  order: Object,
  builder: Object,
  buildTranches: {type: Function},
  adjustShapes: {type: Function, default: null},
  deleteShapes: {type: Function, default: null},
})
const emit = defineEmits(['update:builder'])


let lastId = props.builder.id
builderFuncs[props.builder.id] = props.buildTranches
onUpdated(()=>{
  if (lastId !== props.builder.id ) {
    delete builderFuncs[lastId]
    builderFuncs[props.builder.id] = props.buildTranches
    lastId = props.builder.id
  }
})
onUnmounted(() => delete builderFuncs[lastId])

if (props.adjustShapes) {
  watchEffect(props.adjustShapes)
  onMounted(props.adjustShapes)
}

if (props.deleteShapes)
  onBeforeUnmount(props.deleteShapes)

function deleteMyBuilder() {
  deleteBuilder(props.order, props.builder);
}


</script>

<style scoped lang="scss">
@import "@/styles/vars";

.trashcan {
  :hover {
    color: $red;
  }
}
</style>
