import { cli, Strategy } from '@jackwener/opencli/registry';
cli({
    site: 'jimeng',
    name: 'workspaces',
    access: 'read',
    description: '即梦AI 查看所有工作区（会话窗口）',
    domain: 'jimeng.jianying.com',
    strategy: Strategy.COOKIE,
    browser: true,
    columns: ['workspace_id', 'name', 'is_pinned', 'updated_at'],
    pipeline: [
        { navigate: 'https://jimeng.jianying.com/ai-tool/generate?type=image&workspace=0' },
        { evaluate: `(async () => {
  const res = await fetch('/mweb/v1/workspace/list?aid=513695&web_version=7.5.0&da_version=3.3.12', {
    method: 'POST',
    credentials: 'include',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({})
  });
  const data = await res.json();
  if (data.ret === '1014' || data.ret === 1014) {
    throw new Error('Not logged in — open jimeng.jianying.com in Chrome and sign in first');
  }
  if (data.ret !== '0' && data.ret !== 0) {
    throw new Error('workspace/list failed: ret=' + data.ret + ' errmsg=' + (data.errmsg || ''));
  }
  return (data.data?.workspaces || []).map(ws => ({
    workspace_id: String(ws.workspace_id),
    name: ws.name || '',
    is_pinned: ws.is_pinned ? 'yes' : 'no',
    updated_at: ws.update_time ? new Date(ws.update_time).toLocaleString('zh-CN') : '',
  }));
})()
` },
        { map: {
                workspace_id: '${{ item.workspace_id }}',
                name: '${{ item.name }}',
                is_pinned: '${{ item.is_pinned }}',
                updated_at: '${{ item.updated_at }}',
            } },
    ],
});
