import type { Slots } from 'vue';
import { message } from 'linker-ui-components';
import { cloneDeep, isArray } from 'lodash-es';

/**
 * 静态图片资源处理
 * @param path {String} 路径
 * @param isAddPath
 */
export const getImage = (path: string, isAddPath: Boolean = true) => {
  if (undefined === path || '' === path) {
    return undefined;
  }
  if (isAddPath) {
    return new URL('/images' + path, import.meta.url).href;
  } else {
    return new URL(path, window.location.origin).href;
  }
};

/**
 * TreeSelect过滤
 * @param value 过滤值
 * @param treeNode
 * @param key
 */
export const filterTreeSelectNode = (value: string, treeNode: any, key = 'name'): boolean => {
  return treeNode[key]?.includes(value);
};

/**
 * Select过滤
 * @param value 过滤值
 * @param option
 * @param key
 */
export const filterSelectNode = (value: string, option: any, key = 'label'): boolean => {
  return option[key]?.includes(value);
};

export function getSlot2<T>(slots: Slots, props: Record<string, unknown>, prop = 'default'): T | false {
  if (props[prop] === false) {
    // force not render
    return false;
  }
  return (props[prop] || slots[prop]) as T;
}

export function getSlotVNode<T>(slots: Slots, props: Record<string, unknown>, prop = 'default'): T | false {
  if (props[prop] === false) {
    return false;
  }
  return (props[prop] || slots[prop]?.()) as T;
}

/**
 * 修改Select参数column的值
 * @param e // 查询参数 e
 * @param column {Object} {需要修改的值: 修改后的值}
 * {
        username: 'context.username',
    }
 */
export const modifySearchColumnValue = (e: any, column: object) => {
  e.terms.forEach((item: any) => {
    item.terms.forEach((t: any) => {
      if (column[t.column]) {
        t.column = column[t.column];
      }
    });
  });
  return e;
};

/**
 * 仅提示一次的message
 * @param msg 消息内容
 * @param type 消息类型
 */
export const onlyMessage = (msg: string, type: 'success' | 'error' | 'warning' = 'success') => {
  message[type]({
    content: msg,
    key: type
  });
};

export interface SearchItemData {
  column: any;
  value: any;
  termType: string;
  type?: string;
}

export const handleParamsToString = (terms: SearchItemData[] = []) => {
  const _terms: any[] = [{ terms: [null, null, null] }, { terms: [null, null, null], type: 'and' }];
  let termsIndex = 0;
  let termsStar = 0;
  terms.forEach((item, index) => {
    if (index > 2) {
      termsIndex = 1;
      termsStar = 4;
    }
    _terms[termsIndex].terms[index - termsStar] = item;
  });

  return JSON.stringify({ terms: _terms });
};

export const treeFilter = (data: any[], value: any, key = 'name'): any[] => {
  if (!data) return [];

  return data.filter((item) => {
    if (item.children && item.children.length) {
      item.children = treeFilter(item.children || [], value, key);
      return !!item.children.length;
    } else {
      return item[key] === value;
    }
  });
};

/**
 * 通过子节点获取上级相应数据
 * @param data 树形数据
 * @param search 搜索值
 * @param searchKey 搜索key
 * @param returnKey 返回key
 */
export const openKeysByTree = (data: any[], search: any, searchKey = 'id', returnKey = 'id'): any[] => {
  if (!data || (data && !isArray(data))) return [];
  const cloneData = cloneDeep(data);
  const filterTree = treeFilter(cloneData, search, searchKey);
  const openKeys: any[] = [];

  const findKey = (treeData: any[]) => {
    for (let i = 0; i < treeData.length; i++) {
      const item = treeData[i];
      openKeys.push(item[returnKey]);
      if (item.children && item.children.length) {
        findKey(item.children);
      }
    }
  };

  findKey(filterTree);
  return openKeys;
};

export const numberToString = {
  1: '星期一',
  2: '星期二',
  3: '星期三',
  4: '星期四',
  5: '星期五',
  6: '星期六',
  7: '星期日'
};

export const timeUnitEnum = {
  seconds: '秒',
  minutes: '分',
  hours: '小时'
};
