import { request } from '/@/utils/service';

/** 与 OpenAPI/后端约定一致，勿在页面内散落路径 */
export const USER_WORKBENCH_SHORTCUT_PREFIX = '/api/system/user_workbench_shortcut/';

export function listUserWorkbenchShortcuts() {
	return request({
		url: USER_WORKBENCH_SHORTCUT_PREFIX,
		method: 'get',
	});
}

export function createUserWorkbenchShortcut(data: Record<string, unknown>) {
	return request({
		url: USER_WORKBENCH_SHORTCUT_PREFIX,
		method: 'post',
		data,
	});
}

export function patchUserWorkbenchShortcut(id: string | number, data: Record<string, unknown>) {
	return request({
		url: `${USER_WORKBENCH_SHORTCUT_PREFIX}${id}/`,
		method: 'patch',
		data,
	});
}

export function putUserWorkbenchShortcut(id: string | number, data: Record<string, unknown>) {
	return request({
		url: `${USER_WORKBENCH_SHORTCUT_PREFIX}${id}/`,
		method: 'put',
		data,
	});
}

export function deleteUserWorkbenchShortcut(id: string | number) {
	return request({
		url: `${USER_WORKBENCH_SHORTCUT_PREFIX}${id}/`,
		method: 'delete',
	});
}

/** 拉列表：可能是数组或分页 { results: [] } */
export function unwrapShortcutListData(data: unknown): unknown[] {
	if (data == null) return [];
	if (Array.isArray(data)) return data;
	if (typeof data === 'object' && data !== null && 'results' in data) {
		const r = (data as { results?: unknown }).results;
		if (Array.isArray(r)) return r;
	}
	return [];
}
