// {{folder_name}} React Hooks
// Generated by apifox-flow, do not edit manually

import { useState, useEffect } from 'react';
import { {{class_name}} } from './api';

export const use{{class_name}} = (baseUrl: string) => {
  const [client] = useState(() => new {{class_name}}(baseUrl));
  return client;
};

{{#each methods}}
// {{summary}} Hook
export const use{{method_name_pascal}} = (
  {{#if parameters}}{{#each parameters}}{{#unless @first}}, {{/unless}}{{name}}{{#unless required}}?{{/unless}}: {{type_}}{{/each}},{{/if}}
  baseUrl: string
) => {
  const [data, setData] = useState<{{response_type}} | null>(null);
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState<Error | null>(null);

  const execute = async () => {
    setLoading(true);
    setError(null);
    
    try {
      const client = new {{class_name}}(baseUrl);
      const result = await client.{{method_name}}({{#if parameters}}{ {{#each parameters}}{{name}}{{#unless @last}}, {{/unless}}{{/each}} }{{/if}});
      setData(result);
    } catch (err) {
      setError(err as Error);
    } finally {
      setLoading(false);
    }
  };

  useEffect(() => {
    execute();
  }, [{{#if parameters}}{{#each parameters}}{{name}}{{#unless @last}}, {{/unless}}{{/each}}{{/if}}]);

  return { data, loading, error, refetch: execute };
};

{{/each}}
