import { appApi } from 'src/boot/axios';
//import { ref } from 'vue';

export interface Service {
  name: string;
  workingDirectory: string;
  executablePath: string;
  description: string;
  arguments: string[];
  state: State;
  stateColor: string;
  stateTextColor: string;
}
export type Services = Service[];
export type State = 'Stopped' | 'Stopping' | 'Starting' | 'Running' | 'Error';

export function useServices() {
  async function initServices() {
    return appApi
      .get('services/load')
      .then((resp) => {
        const normalized = resp.data.services.map((item: Service) => {
          if (!item.state) {
            item.state = 'Stopped';
          }

          let stateColor = 'grey-4';
          let stateTextColor = 'black';

          switch (item.state) {
            case 'Running':
              stateColor = 'green';
              stateTextColor = 'white';
              break;
            case 'Starting':
            case 'Stopping':
              stateColor = 'green';
              stateTextColor = 'white';
              break;
            case 'Stopped':
            default:
              stateColor = 'grey-4';
              stateTextColor = 'black';
              break;
          }

          return {
            ...item,
            stateColor,
            stateTextColor,
          };
        });

        return normalized;
      })
      .catch((err) => {
        throw err;
      });
  }

  return {
    initServices,
  };
}
