<template>
    <v-dialog v-model="open" max-width="560">
        <v-card>
            <v-card-title>Регионы — для {{ count }} проектов</v-card-title>
            <v-card-text>
                <div class="mb-4">
                    <div class="text-caption text-success font-weight-medium mb-2">➕ Добавить</div>
                    <div class="d-flex flex-wrap gap-2">
                        <v-chip
                            v-for="r in FEDERAL_DISTRICTS"
                            :key="`add-${r.bit}`"
                            :data-testid="`region-add-${r.bit}`"
                            :color="addMask & r.bit ? 'success' : undefined"
                            :variant="addMask & r.bit ? 'flat' : 'outlined'"
                            size="small"
                            @click="toggleAdd(r.bit)"
                            >{{ r.label }}</v-chip
                        >
                    </div>
                </div>
                <div>
                    <div class="text-caption text-error font-weight-medium mb-2">➖ Убрать</div>
                    <div class="d-flex flex-wrap gap-2">
                        <v-chip
                            v-for="r in FEDERAL_DISTRICTS"
                            :key="`remove-${r.bit}`"
                            :data-testid="`region-remove-${r.bit}`"
                            :color="removeMask & r.bit ? 'error' : undefined"
                            :variant="removeMask & r.bit ? 'flat' : 'outlined'"
                            size="small"
                            @click="toggleRemove(r.bit)"
                            >{{ r.label }}</v-chip
                        >
                    </div>
                </div>
            </v-card-text>
            <v-card-actions>
                <v-spacer />
                <v-btn data-testid="cancel" @click="open = false">Отмена</v-btn>
                <v-btn color="primary" data-testid="apply" :disabled="addMask === 0 && removeMask === 0" @click="apply"
                    >Применить к {{ count }}</v-btn
                >
            </v-card-actions>
        </v-card>
    </v-dialog>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue';
import { FEDERAL_DISTRICTS } from '../../constants/federal-districts';

const props = defineProps<{ modelValue: boolean; count: number }>();
const emit = defineEmits<{
    'update:modelValue': [value: boolean];
    apply: [payload: { add: number; remove: number }];
}>();

const open = ref(props.modelValue);
const addMask = ref(0);
const removeMask = ref(0);

watch(
    () => props.modelValue,
    (val) => {
        open.value = val;
        if (val) {
            addMask.value = 0;
            removeMask.value = 0;
        }
    },
);

watch(open, (val) => {
    emit('update:modelValue', val);
});

function toggleAdd(bit: number) {
    addMask.value ^= bit;
    if (addMask.value & bit) removeMask.value &= ~bit;
}

function toggleRemove(bit: number) {
    removeMask.value ^= bit;
    if (removeMask.value & bit) addMask.value &= ~bit;
}

function apply() {
    emit('apply', { add: addMask.value, remove: removeMask.value });
    addMask.value = 0;
    removeMask.value = 0;
    open.value = false;
}
</script>
