/**
 * 按钮定义工具：消除 buttonList / tableButtonList 重复定义
 *
 * @param {Object} options
 * @param {Array}  options.toolbar   顶部工具栏按钮
 * @param {Array}  options.row       行内操作按钮
 * @param {Function} permissionCheck  权限校验函数，默认 $permission
 * @returns {{ toolbarButtons: Array, rowButtons: Array }}
 *
 * @example
 * import useTableButtons from '@/composables/useTableButtons'
 * // 在组件 data() 或 created() 中调用
 * const { toolbarButtons, rowButtons } = useTableButtons({
 *   toolbar: [{ key: 'add', label: '新建', type: 'primary', auth: '/xxx/create', onClick: this.openDialog }],
 *   row: [{ key: 'edit', label: '编辑', auth: '/xxx/edit', onClick: this.handleEdit }],
 * }, this.$permission)
 */
export function useTableButtons (options = {}, permissionCheck) {
  const check = permissionCheck || (() => true)

  const toolbarButtons = (options.toolbar || []).map(btn => ({
    key: btn.key || btn.label,
    label: btn.label,
    icon: btn.icon,
    type: btn.type || '',
    size: btn.size || 'mini',
    auth: btn.auth,
    cssStyle: btn.cssStyle || {},
    needSelection: !!btn.needSelection,
    disabledTip: btn.disabledTip,
    onClick: btn.onClick,
    hasPermission: btn.auth ? check(btn.auth) : true
  }))

  const rowButtons = (options.row || []).map(btn => ({
    key: btn.key || btn.label,
    label: btn.label,
    icon: btn.icon,
    color: btn.color || '',
    cssStyle: btn.cssStyle || { marginRight: '10px', cursor: 'pointer' },
    auth: btn.auth,
    confirm: btn.confirm || false,
    onClick: btn.onClick,
    hasPermission: btn.auth ? check(btn.auth) : true,
    visible: btn.visible || (() => true)
  }))

  return { toolbarButtons, rowButtons }
}

export default useTableButtons
