import { describe, expect, it } from 'vitest';

import {
  resolveWorkflowFormParameters,
  resolveWorkflowParameterDisplayName,
  resolveWorkflowParameterLabel,
} from '../workflowFormParameters';

describe('resolveWorkflowFormParameters', () => {
  it('uses the image parameter when a legacy schema still declares text', () => {
    const parameters = resolveWorkflowFormParameters({
      parameters: [
        {
          name: 'image_input',
          contentType: 'image',
          dataType: 'Object',
          formType: 'input',
        },
      ],
      startFormSchema: [
        {
          key: 'image_input',
          label: '图片',
          type: 'text',
          contentType: 'text',
        },
      ],
    });

    expect(parameters).toEqual([
      expect.objectContaining({
        name: 'image_input',
        contentType: 'image',
        dataType: 'Object',
      }),
    ]);
  });

  it('uses a custom parameter name when the schema keeps a default label', () => {
    const parameters = resolveWorkflowFormParameters({
      parameters: [
        {
          name: '补充信息',
          formLabel: '文本字段',
        },
      ],
      startFormSchema: [
        {
          key: '补充信息',
          label: '文本字段',
          type: 'text',
          required: false,
        },
      ],
    });

    expect(parameters[0]).toMatchObject({
      name: '补充信息',
      formLabel: '补充信息',
      required: false,
    });
  });

  it('preserves an explicitly customized field label', () => {
    const parameters = resolveWorkflowFormParameters({
      startFormSchema: [
        {
          key: 'context',
          label: '背景资料',
          type: 'textarea',
        },
      ],
    });

    expect(parameters[0]).toMatchObject({
      name: 'context',
      formLabel: '背景资料',
    });
  });

  it('uses the configured parameter name for a default trial-run label', () => {
    expect(
      resolveWorkflowParameterLabel({
        name: 'customer_name',
        formLabel: '新字段',
      }),
    ).toBe('customer_name');
    expect(
      resolveWorkflowParameterDisplayName({
        name: 'start_1.customer_name',
        displayName: '开始节点 > 新字段',
      }),
    ).toBe('开始节点 > customer_name');
  });

  it('uses the configured parameter name instead of a type-derived label', () => {
    const parameter = {
      name: 'start_1.file111',
      formLabel: '文件',
      displayName: '开始节点 > 文件',
    };

    expect(resolveWorkflowParameterLabel(parameter)).toBe('file111');
    expect(resolveWorkflowParameterDisplayName(parameter)).toBe(
      '开始节点 > file111',
    );
  });

  it('preserves the configured system question label', () => {
    const parameter = {
      name: 'user_input',
      formLabel: '用户问题123',
      displayName: '流程开始 > 用户问题123',
      systemReserved: true,
    };

    expect(resolveWorkflowParameterLabel(parameter)).toBe('用户问题123');
    expect(resolveWorkflowParameterDisplayName(parameter)).toBe(
      '流程开始 > 用户问题123',
    );
  });
});
