{"version":3,"file":"c8y-ngx-components-services-shared.mjs","sources":["../../services/shared/services.model.ts","../../services/shared/service-command.service.ts","../../services/shared/c8y-ngx-components-services-shared.ts"],"sourcesContent":["import { gettext } from '@c8y/ngx-components/gettext';\n\nexport const SERVICE_FRAGMENT = 'c8y_Service';\n\nexport const SERVICE_COMMANDS_MAP = {\n  START: {\n    label: gettext('Start'),\n    command: 'START',\n    icon: 'play-circle',\n    styleClass: 'text-success'\n  },\n  STOP: {\n    label: gettext('Stop'),\n    command: 'STOP',\n    icon: 'stop-circle',\n    styleClass: 'text-danger'\n  },\n  RESTART: {\n    label: gettext('Restart'),\n    command: 'RESTART',\n    icon: 'refresh',\n    styleClass: 'text-info'\n  }\n};\n\nexport interface Service {\n  id: string;\n  name: string;\n  serviceType: string;\n  status: string;\n  c8y_SupportedOperations?: string[];\n  c8y_SupportedServiceCommands?: string[];\n  [key: string]: unknown;\n}\n","import { Injectable } from '@angular/core';\nimport { IOperation, IResult, OperationService } from '@c8y/client';\nimport { gettext } from '@c8y/ngx-components/gettext';\nimport { ActionControl, AlertService } from '@c8y/ngx-components';\nimport { map } from 'lodash-es';\nimport { Service, SERVICE_COMMANDS_MAP } from './services.model';\n\n@Injectable({\n  providedIn: 'root'\n})\nexport class ServiceCommandService {\n  constructor(\n    private operationService: OperationService,\n    private alertService: AlertService\n  ) {}\n\n  /**\n   * Check if the service supports service commands.\n   * @param service The service object containing supported operations.\n   * @returns boolean indicating if the service supports service commands.\n   */\n  isServiceCommandSupported(service: Service): boolean {\n    return service.c8y_SupportedOperations?.includes('c8y_ServiceCommand');\n  }\n\n  /**\n   * Get the list of supported service commands for the given service object.\n   * @param service The service object containing the c8y_SupportedServiceCommands fragment.\n   * @returns Array of supported commands.\n   */\n  getSupportedCommands(service: Service): string[] {\n    return service.c8y_SupportedServiceCommands || map(SERVICE_COMMANDS_MAP, 'command');\n  }\n\n  /**\n   * Check if a specific command is supported for the given service.\n   * @param service The service object.\n   * @param command The command to check (e.g., START, STOP).\n   * @returns boolean indicating if the command is supported.\n   */\n  isCommandSupported(service: Service, command: string): boolean {\n    const supportedCommands = this.getSupportedCommands(service);\n    return supportedCommands.includes(command);\n  }\n\n  /**\n   * Get all supported commands for a list of services.\n   * @param services The list of service objects to evaluate.\n   * @returns Array of all unique supported commands.\n   */\n  getAllSupportedCommands(services: Service[]): string[] {\n    const allCommands = services\n      .filter(service => this.isServiceCommandSupported(service))\n      .flatMap(service => this.getSupportedCommands(service));\n\n    return Array.from(new Set(allCommands));\n  }\n\n  /**\n   * Create an operation for the selected service command.\n   * @param service The service object the operation should target.\n   * @param command The command to execute (e.g., START, STOP).\n   * @returns Promise of the created operation.\n   */\n  createOperation(\n    { id, name, serviceType }: Service,\n    command: string\n  ): Promise<IResult<IOperation>> {\n    const operation: IOperation = {\n      deviceId: id,\n      description: `${command} ${name}`,\n      c8y_ServiceCommand: {\n        command,\n        serviceName: name,\n        serviceType: serviceType\n      }\n    };\n\n    return this.operationService.create(operation);\n  }\n\n  /**\n   * Generate a static list of action controls.\n   * The specific logic for each service is handled dynamically in showIf and callback.\n   * @returns Array of ActionControl objects.\n   */\n  generateActionControls(commands: string[]): ActionControl[] {\n    return commands.map(command => ({\n      type: command.toLowerCase(),\n      icon: this.getCommandIcon(command),\n      iconClasses: this.getCommandIconClass(command),\n      text: command,\n      showIf: (service: Service) =>\n        this.isServiceCommandSupported(service) && this.isCommandSupported(service, command),\n      callback: async (service: Service) => {\n        try {\n          await this.createOperation(service, command);\n          this.alertService.success(gettext('Operation created.'));\n        } catch (error) {\n          const alertMessage = gettext('Could not create operation.');\n          this.alertService.danger(alertMessage, error);\n        }\n      }\n    }));\n  }\n\n  /**\n   * Get the icon for a specific command.\n   * @param command The command name.\n   * @returns Icon string for the command.\n   */\n  private getCommandIcon(command: string): string {\n    return SERVICE_COMMANDS_MAP[command]?.icon || 'console';\n  }\n\n  /**\n   * Get the icon class for a specific command.\n   * @param command The command name.\n   * @returns Icon class string for the command.\n   */\n  private getCommandIconClass(command: string): string {\n    return SERVICE_COMMANDS_MAP[command]?.styleClass;\n  }\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;;;;;AAEO,MAAM,gBAAgB,GAAG;AAEzB,MAAM,oBAAoB,GAAG;AAClC,IAAA,KAAK,EAAE;AACL,QAAA,KAAK,EAAE,OAAO,CAAC,OAAO,CAAC;AACvB,QAAA,OAAO,EAAE,OAAO;AAChB,QAAA,IAAI,EAAE,aAAa;AACnB,QAAA,UAAU,EAAE;AACb,KAAA;AACD,IAAA,IAAI,EAAE;AACJ,QAAA,KAAK,EAAE,OAAO,CAAC,MAAM,CAAC;AACtB,QAAA,OAAO,EAAE,MAAM;AACf,QAAA,IAAI,EAAE,aAAa;AACnB,QAAA,UAAU,EAAE;AACb,KAAA;AACD,IAAA,OAAO,EAAE;AACP,QAAA,KAAK,EAAE,OAAO,CAAC,SAAS,CAAC;AACzB,QAAA,OAAO,EAAE,SAAS;AAClB,QAAA,IAAI,EAAE,SAAS;AACf,QAAA,UAAU,EAAE;AACb;;;MCZU,qBAAqB,CAAA;IAChC,WAAA,CACU,gBAAkC,EAClC,YAA0B,EAAA;QAD1B,IAAA,CAAA,gBAAgB,GAAhB,gBAAgB;QAChB,IAAA,CAAA,YAAY,GAAZ,YAAY;IACnB;AAEH;;;;AAIG;AACH,IAAA,yBAAyB,CAAC,OAAgB,EAAA;QACxC,OAAO,OAAO,CAAC,uBAAuB,EAAE,QAAQ,CAAC,oBAAoB,CAAC;IACxE;AAEA;;;;AAIG;AACH,IAAA,oBAAoB,CAAC,OAAgB,EAAA;QACnC,OAAO,OAAO,CAAC,4BAA4B,IAAI,GAAG,CAAC,oBAAoB,EAAE,SAAS,CAAC;IACrF;AAEA;;;;;AAKG;IACH,kBAAkB,CAAC,OAAgB,EAAE,OAAe,EAAA;QAClD,MAAM,iBAAiB,GAAG,IAAI,CAAC,oBAAoB,CAAC,OAAO,CAAC;AAC5D,QAAA,OAAO,iBA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