<template>
  <div class="ps-transfer">
    <transfer-panel
      ref="leftPanel"
      :data="sourceData"
      :option-render="optionRender"
      :placeholder="panelFilterPlaceholder"
      :title="leftPanelTitle"
      :filterable="filterable"
      :format="format"
      :filter-method="filterMethod"
      :default-checked="leftDefaultChecked"
      :props="props"
      @checked-change="onSourceCheckedChange"
    >
      <slot name="left-footer"></slot>
    </transfer-panel>
    <div class="ps-transfer__buttons">
      <ps-button
        type="primary"
        :class="['ps-transfer__button', hasButtonTexts ? 'is-with-texts' : '']"
        :disabled="rightChecked.length === 0"
        @click="addToLeft"
      >
        <i class="ps-icon-arrow-left"></i>
        <span v-if="buttonTexts[0] !== undefined">{{ buttonTexts[0] }}</span>
      </ps-button>
      <ps-button
        type="primary"
        :class="['ps-transfer__button', hasButtonTexts ? 'is-with-texts' : '']"
        :disabled="leftChecked.length === 0"
        @click="addToRight"
      >
        <span v-if="buttonTexts[1] !== undefined">{{ buttonTexts[1] }}</span>
        <i class="ps-icon-arrow-right"></i>
      </ps-button>
    </div>
    <transfer-panel
      ref="rightPanel"
      :data="targetData"
      :option-render="optionRender"
      :placeholder="panelFilterPlaceholder"
      :filterable="filterable"
      :format="format"
      :filter-method="filterMethod"
      :title="rightPanelTitle"
      :default-checked="rightDefaultChecked"
      :props="props"
      @checked-change="onTargetCheckedChange"
    >
      <slot name="right-footer"></slot>
    </transfer-panel>
  </div>
</template>

<script lang="ts">
import {
  computed, defineComponent, inject, h,
  reactive, ref, toRefs, watch,
} from 'vue'
import { t } from '@present-ui/locale'
import PsButton from '@present-ui/button'
import TransferPanel from './transfer-panel.vue'
import { useComputedData } from './useComputedData'
import { useCheckedChange } from './useCheckedChange'
import { useMove } from './useMove'

import { UPDATE_MODEL_EVENT } from '@present-ui/utils/constants'
import { psFormItemKey } from '@present-ui/form'

import { LEFT_CHECK_CHANGE_EVENT, RIGHT_CHECK_CHANGE_EVENT } from './useCheckedChange'

import type { PropType, VNode } from 'vue'
import type { PsFormItemContext } from '@present-ui/form'
import type {
  DataItem, Format, Key,
  Props, TargetOrder,
} from './transfer'

export const CHANGE_EVENT = 'change'

export default defineComponent({
  name: 'PsTransfer',

  components: {
    TransferPanel,
    PsButton,
  },

  props: {
    data: {
      type: Array as PropType<DataItem[]>,
      default: () => [],
    },
    titles: {
      type: Array as PropType<any> as PropType<[string, string]>,
      default: () => [],
    },
    buttonTexts: {
      type: Array as PropType<any> as PropType<[string, string]>,
      default: () => [],
    },
    filterPlaceholder: {
      type: String,
      default: '',
    },
    filterMethod: Function as PropType<(query: string, item: DataItem) => boolean>,
    leftDefaultChecked: {
      type: Array as PropType<Key[]>,
      default: () => [],
    },
    rightDefaultChecked: {
      type: Array as PropType<Key[]>,
      default: () => [],
    },
    renderContent: Function as PropType<(h, option) => VNode>,
    modelValue: {
      type: Array as PropType<Key[]>,
      default: () => [],
    },
    format: {
      type: Object as PropType<Format>,
      default: () => ({}),
    },
    filterable: {
      type: Boolean,
      default: false,
    },
    props: {
      type: Object as PropType<Props>,
      default: () => ({
        label: 'label',
        key: 'key',
        disabled: 'disabled',
      }),
    },
    targetOrder: {
      type: String as PropType<TargetOrder>,
      default: 'original',
      validator: (val: string) => {
        return ['original', 'push', 'unshift'].includes(val)
      },
    },
  },

  emits: [
    UPDATE_MODEL_EVENT,
    CHANGE_EVENT,
    LEFT_CHECK_CHANGE_EVENT,
    RIGHT_CHECK_CHANGE_EVENT,
  ],

  setup(props, { emit, slots }) {
    const elFormItem = inject(psFormItemKey, {} as PsFormItemContext)

    const checkedState = reactive({
      leftChecked: [],
      rightChecked: [],
    })

    const {
      propsKey,
      sourceData,
      targetData,
    } = useComputedData(props)

    const {
      onSourceCheckedChange,
      onTargetCheckedChange,
    } = useCheckedChange(checkedState, emit)

    const {
      addToLeft,
      addToRight,
    } = useMove(props, checkedState, propsKey, emit)

    const leftPanel = ref(null)
    const rightPanel = ref(null)

    const clearQuery = (which: 'left' | 'right') => {
      if (which === 'left') {
        leftPanel.value.query = ''
      } else if (which === 'right') {
        rightPanel.value.query = ''
      }
    }

    const hasButtonTexts = computed(() => props.buttonTexts.length === 2)

    const leftPanelTitle = computed(() => props.titles[0] || t('ps.transfer.titles.0'))

    const rightPanelTitle = computed(() => props.titles[1] || t('ps.transfer.titles.1'))

    const panelFilterPlaceholder = computed(() => props.filterPlaceholder || t('ps.transfer.filterPlaceholder'))

    watch(() => props.modelValue, val => {
      elFormItem.formItemMitt?.emit('ps.form.change', val)
    })

    const optionRender = computed(() => option => {
      if (props.renderContent) return props.renderContent(h, option)

      if (slots.default) return slots.default({ option })

      return h('span', option[props.props.label] || option[props.props.key])
    })

    return {
      sourceData,
      targetData,
      onSourceCheckedChange,
      onTargetCheckedChange,
      addToLeft,
      addToRight,

      ...toRefs(checkedState),

      hasButtonTexts,
      leftPanelTitle,
      rightPanelTitle,
      panelFilterPlaceholder,
      clearQuery,

      optionRender,
    }
  },
})
</script>
