/**
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

import {FC, useContext, useEffect, useState} from 'react';
import {TestCaseState} from '../core';
import {expect as expect_, AssertionError} from 'chai';
import {TestCaseContext} from './TestingContext';
import {TestCaseStateComponent} from './TestCaseState';

interface LogicalTestCaseProps {
  name: string;
  skip?: boolean | string;
  fn: (utils: {expect: typeof expect_}) => Promise<void> | void;
}

export const LogicalTestCase: FC<LogicalTestCaseProps> = ({name, fn, skip}) => {
  const [result, setResult] = useState<TestCaseState>({status: 'running'});
  const {reportTestCaseResult} = useContext(TestCaseContext)!;

  useEffect(() => {
    (async () => {
      try {
        if (skip) {
          setResult({
            status: 'skipped',
            message: typeof skip === 'string' ? skip : undefined,
          });
          reportTestCaseResult('skipped');
          return;
        }
        setResult({status: 'running'});
        await fn({expect: expect_});
        setResult({status: 'pass'});
        reportTestCaseResult('pass');
      } catch (err) {
        if (err instanceof AssertionError) {
          setResult({status: 'fail', message: err.message});
          reportTestCaseResult('fail');
        } else if (err instanceof Error) {
          setResult({status: 'broken', message: err.message});
          reportTestCaseResult('broken');
        } else {
          setResult({status: 'broken', message: ''});
          reportTestCaseResult('broken');
        }
      }
    })();
  }, []);

  return <TestCaseStateComponent name={name} result={result} />;
};