<!--
 * @Description: 
 * @Author: Format-qi 283810417@qq.com
 * @Date: 2022-05-10 17:21:00
 * @LastEditors: Format-qi 283810417@qq.com
 * @LastEditTime: 2022-06-24 13:12:00
-->
<script lang="tsx">
  import type { CSSProperties, PropType } from 'vue';
  import { defineComponent, computed, unref } from 'vue';
  import { Tooltip } from 'ant-design-vue';
  import { InfoCircleOutlined } from '@ant-design/icons-vue';
  import { getPopupContainer } from '/@/utils';
  import { isString, isArray } from '/@/utils/is';
  import { getSlot } from '/@/utils/helper/tsxHelper';
  import { useDesign } from '/@/hooks/web/useDesign';

  const props = {
    /**
     * 最大宽度
     * @default: 600px
     */
    maxWidth: { type: String, default: '600px' },
    /**
     * 是否显示序号,在 text 为 string[]情况下生效
     * @default: false
     */
    showIndex: { type: Boolean },

    color: { type: String, default: '#ffffff' },

    fontSize: { type: String, default: '14px' },
    /**
     * 显示方向，参考 Tooltip 组件
     */
    placement: { type: String, default: 'right' },
    /**
     * 文本列表
     */
    text: { type: [Array, String] as PropType<string[] | string> },
  };

  export default defineComponent({
    name: 'BzHelp',
    components: { Tooltip },
    props,
    setup(props, { slots }) {
      const { prefixCls } = useDesign('basic-help');

      const getTooltipStyle = computed(
        (): CSSProperties => ({ color: props.color, fontSize: props.fontSize }),
      );

      const getOverlayStyle = computed((): CSSProperties => ({ maxWidth: props.maxWidth }));

      function renderTitle() {
        const textList = props.text;

        if (isString(textList)) {
          return <p>{textList}</p>;
        }

        if (isArray(textList)) {
          return textList.map((text, index) => {
            return (
              <p key={text}>
                <>
                  {props.showIndex ? `${index + 1}. ` : ''}
                  {text}
                </>
              </p>
            );
          });
        }
        return null;
      }

      return () => {
        return (
          <Tooltip
            overlayClassName={`${prefixCls}__wrap`}
            title={<div style={unref(getTooltipStyle)}>{renderTitle()}</div>}
            autoAdjustOverflow={true}
            overlayStyle={unref(getOverlayStyle)}
            placement={props.placement as 'right'}
            getPopupContainer={() => getPopupContainer()}
          >
            <span class={prefixCls}>{getSlot(slots) || <InfoCircleOutlined />}</span>
          </Tooltip>
        );
      };
    },
  });
</script>
