<template>
  <v-dialog :model-value="modelValue" @update:modelValue="$emit('update:modelValue', $event)">
    <v-card style="max-width: 25em" class="mx-auto">
      <v-card-title>
        <v-icon icon="mdi-location-enter" color="grey-darken-1"/>&nbsp;Wrap ETH into WETH
      </v-card-title>
      <v-card-item>
        <v-text-field class="text-end" type="number" variant="outlined" :min="0" :max="balanceFloat"
                      v-model="floatAmount" :step="step">
          <template v-slot:prepend-inner>
            <v-btn variant="text" text="max" @click="floatAmount=balanceFloat"/>
          </template>
          <template v-slot:append-inner>
            <span>ETH</span>
          </template>
        </v-text-field>
        <v-card-actions>
          <v-btn text="Cancel" @click="$emit('update:modelValue', false)"/>
          <v-btn text="Wrap" color="red" @click="wrapNative"/>
        </v-card-actions>
      </v-card-item>
    </v-card>
  </v-dialog>
</template>

<script setup>
import {useStore} from "@/store/store";
import {computed, ref} from "vue";
import {FixedNumber} from "ethers";
import {WrapTransaction} from "@/blockchain/transaction.js";

const s = useStore()
const props = defineProps(['modelValue', 'vault', 'maxAmount'])
const emit = defineEmits(['update:modelValue'])
const balanceFloat = computed(() => Number(props.maxAmount)/1e18) // todo configurable native decimals
const step = computed(()=>balanceFloat.value/20)
const floatAmount = ref(0)

function wrapNative() {
  const vaultAddr = props.vault
  const valueStr = floatAmount.value.toString();
  const amount = floatAmount.value === balanceFloat.value ? props.maxAmount : // maximum
      FixedNumber.fromString(valueStr,{decimals:18, width:256, signed: false}).value;
  if( amount === 0n )
    return
  console.log('pending wrap', valueStr, amount)
  new WrapTransaction(s.chainId, vaultAddr, amount).submit()
  floatAmount.value = 0
  emit('update:modelValue', false)
}

</script>

<style scoped lang="scss">
@use "src/styles/vars" as *;

</style>
