import { markRaw } from 'vue';
import { useTaskPermissions } from './use-task-permissions';
import IconMdiClipboardEditOutline from '~icons/mdi/clipboard-edit-outline';
import IconMdiDeleteOutline from '~icons/mdi/delete-outline';
import IconMdiPencilOutline from '~icons/mdi/pencil-outline';

export interface TaskAction {
  key: string;
  tooltip: string;
  icon: object;
  type: 'primary' | 'success' | 'danger' | 'warning';
  onClick: () => void;
}

export interface TaskActionEmits {
  edit: (row: Api.Project.ProjectTask) => void;
  report: (row: Api.Project.ProjectTask) => void;
  remove: (row: Api.Project.ProjectTask) => void;
  statusAction: (
    row: Api.Project.ProjectTask,
    action: Api.Project.LifecycleAction<Api.Project.ProjectTaskActionCode>
  ) => void;
}

/**
 * 任务行操作按钮的集中装配。
 *
 * 这里只收口任务行按钮的最终显示口径；编辑 / 删除身份规则统一落在 useTaskPermissions。
 * 权限码只做后端兜底，不作为前端按钮展示的单独放行条件。
 */
export function useTaskActions(emits: TaskActionEmits) {
  const { canEditTask, canDeleteTask } = useTaskPermissions();

  function createActions(row: Api.Project.ProjectTask): TaskAction[] {
    const actions: TaskAction[] = [];

    actions.push({
      key: 'report',
      tooltip: '填报',
      icon: markRaw(IconMdiClipboardEditOutline),
      type: 'primary',
      onClick: () => emits.report(row)
    });

    if (canEditTask(row)) {
      actions.push({
        key: 'edit',
        tooltip: '编辑',
        icon: markRaw(IconMdiPencilOutline),
        type: 'primary',
        onClick: () => emits.edit(row)
      });
    }

    if (canDeleteTask(row)) {
      actions.push({
        key: 'delete',
        tooltip: '删除',
        icon: markRaw(IconMdiDeleteOutline),
        type: 'danger',
        onClick: () => emits.remove(row)
      });
    }

    return actions;
  }

  return { createActions };
}
