All files / src/components/Appointment ServiceFinder.vue

89.61% Statements 492/549
76.35% Branches 113/148
57.89% Functions 11/19
89.61% Lines 492/549

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726  1x 1x 1x   1x 1x     40x 40x 40x   40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x   40x 40x 40x   40x 40x 40x 40x 40x 40x 40x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 13x   13x 13x 13x 13x   13x 13x 13x 13x 13x 1x   1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 23x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x       1x             1x           1x 1x 1x 1x   1x     1x 1x 1x         1x         1x   1x               1x               1x 1x     1x 1x 1x         1x   1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 11x   11x 2x 2x   9x 3x 6x 1x   1x 11x 11x 11x 9x   1x   1x 5x 5x 5x 3x   1x   1x 1x 1x   1x 14x   14x 14x 2x 12x   14x 14x 4x 4x 4x 14x 14x   14x 1x         1x 1x 1x 1x 1x     1x 1x   1x 14x 14x 14x   14x 1x 1x 1x 1x 1x 1x                     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x   14x 14x 14x 14x 14x   14x 14x   1x 15x 15x 13x 5x 5x 5x 5x   5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x   5x 5x   5x 5x 5x 5x 5x 15x   15x 15x         1x                             1x 29x 29x 1x   1x 56x 1x         1x 27x 27x 18x 9x 1x   1x 1x 39x 39x 1x   1x 67x 67x 42x 42x 42x 67x 67x 67x   1x 14x 14x 5x 5x   5x     14x 14x 14x   1x                             1x   1x                 1x 43x   43x 43x 43x   43x 43x 27x 11x 43x 1x   1x 38x 13x 13x 13x     13x 13x 10x 3x 13x 13x 13x 13x 10x 35x       10x 10x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x   13x 13x 13x 13x 13x 38x 25x 25x 25x 25x 25x   25x 25x 25x 25x 25x     25x       25x 25x 25x   25x 14x 14x 14x 14x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 14x   25x 12x 12x 12x 12x 8x 8x 12x   25x 9x 9x 13x 3x 9x 9x 8x 8x 9x 25x 25x 1x   1x 1x   1x 40x 1x   1x 52x   39x 39x 39x   39x   39x 52x 22x 22x 22x 22x 22x 22x   39x 39x 1x   1x 4x   4x 4x 4x   4x 4x 4x 1x   1x 39x 39x   39x 39x 39x 39x 39x 39x   19x 1x   131x 131x 131x 131x 131x 131x 131x 131x 131x 131x 131x   1x 1x 1x 1x 1x 29x 29x 1x                                                                                
<template>
  <div class="m-content">
    <h2>{{ t("service") }}</h2>
  </div>
 
  <div
    v-if="
      !service && !errorStates.errorStateMap.apiErrorRateLimitExceeded.value
    "
    :hidden="!!preselectedServiceId"
    class="m-component"
    style="background-color: var(--color-neutrals-blue-xlight)"
  >
    <div class="container">
      <form class="m-form m-form--default">
        <muc-select
          id="service-search"
          v-model="service"
          :items="filteredServices"
          item-title="name"
          :label="t('serviceSearch')"
          :no-item-found-message="t('noServiceFound')"
          :placeholder="t('serviceSelectionPlaceholder')"
        />
      </form>
      <div class="m-linklist-inline">
        <h3 class="m-linklist-inline__title">
          {{ t("oftenSearchedService") }}
        </h3>
        <ul class="m-linklist-inline__list">
          <li
            v-for="searchedService in OFTEN_SEARCHED_SERVICES"
            :key="searchedService[0]"
            class="m-linklist-inline__list-item"
          >
            <a
              href="#"
              @click="setOftenSearchedService(searchedService[0])"
            >
              {{ t(searchedService[1]) }}</a
            >
          </li>
        </ul>
      </div>
    </div>
  </div>
  <div v-else-if="!errorStates.errorStateMap.apiErrorRateLimitExceeded.value">
    <div class="m-component">
      <muc-counter
        v-model="countOfService"
        :label="service?.name || ''"
        :link="getServiceBaseURL() + (baseServiceId || '')"
        :max="maxValueOfService"
        :min="1"
      />
      <div
        v-if="variantServices.length > 1"
        class="m-component"
      >
        <muc-radio-button-group
          v-model="selectedVariant"
          :heading="t('appointmentType')"
        >
          <muc-radio-button
            v-for="variant in variantServices"
            :key="variant.variantId"
            :id="'variant-' + variant.variantId"
            :value="variant.variantId.toString()"
            :label="t(`appointmentTypes.${variant.variantId}`)"
          />
        </muc-radio-button-group>
      </div>
    </div>
    <div v-if="showSubservices">
      <h3>{{ t("combinableServices") }}</h3>
      <p
        class="visually-hidden"
        tabindex="0"
        @click="skipSubservices"
      >
        {{ t("skipCombinableServices") }}
      </p>
      <div class="m-listing">
        <ul class="m-listing__list">
          <template
            v-for="subService in filteredSubServices"
            :key="subService.id"
          >
            <subservice-list-item
              :sub-service="subService"
              :current-slots="currentSlots"
              :max-slots-per-appointment="maxSlotsPerAppointment"
              @change="changeAppointmentCountOfSubservice"
            />
          </template>
        </ul>
        <div
          v-if="shouldShowMoreButton"
          class="m-button-group m-button-group--secondary"
        >
          <muc-button
            icon="chevron-down"
            variant="secondary"
            @click="showAllServices = true"
          >
            <template #default>{{ t("showAllServices") }}</template>
          </muc-button>
        </div>
        <div
          v-else-if="shouldShowLessButton"
          class="m-button-group m-button-group--secondary"
        >
          <muc-button
            icon="chevron-up"
            variant="secondary"
            @click="showAllServices = false"
          >
            <template #default>{{ t("showLessServices") }}</template>
          </muc-button>
        </div>
      </div>
    </div>
    <div
      class="m-component"
      v-if="showEstimatedDuration"
    >
      <div class="wrapper">
        <clock-svg
          aria-hidden="true"
          focusable="false"
        />
        <div ref="durationInfo">
          <strong>{{ t("estimatedDuration") }}</strong>
          <br />
          {{ estimatedDuration }} {{ t("minutes") }}
        </div>
      </div>
    </div>
    <div
      v-if="showCaptcha"
      style="margin: 2rem 0 2rem 0"
    >
      <AltchaCaptcha
        :t="t"
        :base-url="globalState.baseUrl"
        @validationResult="(valid: boolean) => (isCaptchaValid = valid)"
        @tokenChanged="
          (token: string | null) => emit('captchaTokenChanged', token)
        "
      />
    </div>
  </div>
  <div class="m-button-group">
    <muc-button
      v-if="service"
      :disabled="isNextDisabled"
      icon="arrow-right"
      @click="nextStep"
    >
      <template #default>{{ t("next") }}</template>
    </muc-button>
  </div>
</template>
 
<script setup lang="ts">
import {
  MucButton,
  MucCounter,
  MucRadioButton,
  MucRadioButtonGroup,
  MucSelect,
} from "@muenchen/muc-patternlab-vue";
import { computed, inject, onMounted, ref, watch } from "vue";
 
import { Combinable } from "@/api/models/Combinable";
import { Office } from "@/api/models/Office";
import { Relation } from "@/api/models/Relation";
import { Service } from "@/api/models/Service";
import { fetchServicesAndProviders } from "@/api/ZMSAppointmentAPI";
import AltchaCaptcha from "@/components/Appointment/ServiceFinder/AltchaCaptcha.vue";
import ClockSvg from "@/components/Appointment/ServiceFinder/ClockSvg.vue";
import SubserviceListItem from "@/components/Appointment/ServiceFinder/SubserviceListItem.vue";
import { GlobalState } from "@/types/GlobalState";
import { OfficeImpl } from "@/types/OfficeImpl";
import { SelectedServiceProvider } from "@/types/ProvideInjectTypes";
import { ServiceImpl } from "@/types/ServiceImpl";
import { handleApiResponseForDownTime } from "@/utils/apiStatusService";
import { calculateEstimatedDuration } from "@/utils/calculateEstimatedDuration";
import {
  getServiceBaseURL,
  MAX_SLOTS,
  OFTEN_SEARCHED_SERVICES,
} from "@/utils/Constants";
import {
  createErrorStates,
  handleApiResponse as handleErrorApiResponse,
} from "@/utils/errorHandler";
 
const isCaptchaValid = ref<boolean>(false);
 
const props = defineProps<{
  globalState: GlobalState;
  preselectedServiceId: string | undefined;
  preselectedOfficeId: string | undefined;
  exclusiveLocation: string | undefined;
  t: (key: string) => string;
}>();
 
const emit = defineEmits<{
  (e: "next"): void;
  (e: "captchaTokenChanged", token: string | null): void;
  (e: "invalidJumpinLink"): void;
  (e: "rateLimitError"): void;
}>();
 
// Error handling state
const errorStates = createErrorStates();
const currentErrorData = computed(() => errorStates.currentErrorData);
 
// Watch for rate limit errors and emit event
watch(
  () => errorStates.errorStateMap.apiErrorRateLimitExceeded.value,
  (isRateLimitError) => {
    if (isRateLimitError) {
      emit("rateLimitError");
    }
  }
);
 
const services = ref<Service[]>([]);
const relations = ref<Relation[]>([]);
const offices = ref<Office[]>([]);
 
const { selectedService, updateSelectedService } =
  inject<SelectedServiceProvider>(
    "selectedServiceProvider"
  ) as SelectedServiceProvider;
const service = ref<ServiceImpl | undefined>(selectedService.value);
const maxSlotsPerAppointment = ref<number>(25);
const currentSlots = ref<number>(0);
const showAllServices = ref<boolean>(false);
const countOfService = ref<number>(1);
 
const filteredSubServices = computed(() => {
  if (!service.value?.subServices) return [];
 
  if (service.value.subServices.length <= 5) {
    return service.value.subServices;
  }
 
  return showAllServices.value
    ? service.value.subServices
    : service.value.subServices.slice(0, 3);
});
 
const shouldShowMoreButton = computed(() => {
  return (
    service.value?.subServices &&
    service.value.subServices.length > 5 &&
    !showAllServices.value
  );
});
 
const shouldShowLessButton = computed(() => {
  return (
    service.value?.subServices &&
    service.value.subServices.length > 5 &&
    showAllServices.value
  );
});
 
const durationInfo = ref<HTMLElement | null>(null);
const baseServiceId = ref<number | string | null>(null);
const selectedVariant = ref("");
 
watch(service, (newService) => {
  if (!newService) return;
 
  baseServiceId.value =
    newService.parentId != null
      ? String(newService.parentId)
      : String(newService.id);
 
  const variantId = newService.variantId;
  if (typeof variantId === "number" && Number.isFinite(variantId)) {
    const next = String(variantId);
    if (selectedVariant.value !== next) selectedVariant.value = next;
  }
  setServiceData(newService);
  updateSelectedService(newService);
 
  countOfService.value = newService.count ?? 1;
});
 
/**
 * Calculation of the currently required slots by changing the count of the selected service.
 */
watch(countOfService, (newCountOfService) => {
  if (!service.value) return;
  if ((service.value.count || 0) < newCountOfService) {
    currentSlots.value += getMinSlotOfProvider(service.value.providers || []);
  } else if ((service.value.count || 0) > newCountOfService) {
    currentSlots.value -= getMinSlotOfProvider(service.value.providers || []);
  }
  service.value.count = newCountOfService;
});
 
const setServiceData = (selectedService: ServiceImpl) => {
  service.value!.providers = getProviders(selectedService.id, null);
  service.value!.count = Math.max(1, countOfService.value || 1);
  currentSlots.value = getMinSlotOfProvider(service.value!.providers);
 
  if (selectedService.combinable) {
    const combinable = selectedService.combinable;
    const selfEntry = Object.entries(combinable).find(
      ([_, serviceObj]) =>
        Object.keys(serviceObj)[0] === selectedService.id.toString()
    );
    if (selfEntry) {
      const combinableServices = combinable as unknown as Combinable;
      Object.keys(combinableServices).forEach((key: string) => {
        const serviceObj = combinableServices[key];
        const serviceId = Object.keys(serviceObj)[0];
        if (serviceId === selectedService.id.toString()) {
          delete combinableServices[key];
        }
      });
    }
 
    service.value!.subServices = Object.entries(combinable)
      .map(([_, serviceObj]) => {
        const [[subServiceId, providers]] = Object.entries(serviceObj);
        const subService = services.value.filter(
          (subService) => parseInt(subService.id) == parseInt(subServiceId)
        );
        if (subService && subService.length === 1) {
          return {
            id: subServiceId,
            name: subService[0].name,
            maxQuantity: subService[0].maxQuantity,
            providers: getProviders(subServiceId, providers.map(String)),
            count: 0,
          };
        }
      })
      .filter(
        (service): service is NonNullable<typeof service> =>
          service !== undefined &&
          (!props.preselectedOfficeId ||
            service.providers.some(
              (provider) => provider.id == props.preselectedOfficeId
            ))
      );
  }
 
  const maxSlotsOfProvider = getMaxSlotsPerAppointementOfProvider(
    service.value!.providers
  );
  maxSlotsPerAppointment.value =
    maxSlotsOfProvider > 0
      ? Math.min(maxSlotsOfProvider, MAX_SLOTS)
      : MAX_SLOTS;
};
 
const getProviders = (serviceId: string, providers: string[] | null) => {
  const officesAtService = new Array<OfficeImpl>();
  relations.value.forEach((relation) => {
    if (relation.serviceId == serviceId) {
      const office = offices.value.find(
        (office) => office.id == relation.officeId
      );
      if (!office) return;
 
      const foundOffice: OfficeImpl = new OfficeImpl(
        office.id,
        office.name,
        office.address,
        office.showAlternativeLocations,
        office.displayNameAlternatives,
        office.organization,
        office.organizationUnit,
        office.slotTimeInMinutes,
        office.disabledByServices,
        office.scope,
        office.maxSlotsPerAppointment,
        office.slots,
        office.priority || 1
      );
 
      if (
        !providers ||
        providers.find((prov) => prov == foundOffice.id.toString())
      ) {
        foundOffice.slots = relation.slots;
        officesAtService.push(foundOffice);
      }
    }
  });
 
  return officesAtService;
};
 
/**
 * Calculation of the currently required slots by changing the count of a subservice.
 */
const changeAppointmentCountOfSubservice = (id: string, count: number) => {
  const subservice = service.value?.subServices?.find(
    (subService) => subService.id == id
  );
 
  if (subservice != undefined) {
    if (subservice.count < count) {
      currentSlots.value += getMinSlotOfProvider(subservice.providers);
    } else if (subservice.count > count) {
      currentSlots.value -= getMinSlotOfProvider(subservice.providers);
    }
    subservice.count = count;
  }
};
 
const estimatedDuration = computed(() => {
  const provider = service.value?.providers?.[0];
  return calculateEstimatedDuration(service.value, provider);
});
 
const showEstimatedDuration = computed(() => {
  return !(variantServices.value.length > 1 && !selectedVariant.value);
});
 
/**
 * Calculates whether the count of selected service may be increased, depending on the maxQuantity of the service and the maxSlotsPerAppointment.
 */
const maxValueOfService = computed(() => {
  if (!service.value) return 0;
  return checkPlusEndabled.value
    ? service.value.maxQuantity
    : service.value.count || 0;
});
 
const checkPlusEndabled = computed(
  () =>
    currentSlots.value + getMinSlotOfProvider(service.value!.providers || []) <=
    maxSlotsPerAppointment.value
);
 
const getMinSlotOfProvider = (provider: OfficeImpl[]) => {
  let minSlot = MAX_SLOTS;
  provider.forEach((provider) => {
    if (provider.slots) {
      minSlot = Math.min(minSlot, provider.slots);
    }
  });
  return minSlot;
};
 
const getMaxSlotsPerAppointementOfProvider = (provider: OfficeImpl[]) => {
  let maxSlot = 0;
  provider.forEach((provider) => {
    if (
      provider.maxSlotsPerAppointment &&
      parseInt(provider.maxSlotsPerAppointment) > 0
    ) {
      maxSlot = Math.max(maxSlot, parseInt(provider.maxSlotsPerAppointment));
    }
  });
  return maxSlot;
};
 
const setOftenSearchedService = (serviceId: string) => {
  const foundService = services.value.find(
    (service) => service.id == serviceId
  );
  if (foundService) {
    service.value = {
      ...foundService,
      providers: [] as OfficeImpl[],
      count: 0,
      subServices: [],
      combinable: foundService.combinable as unknown as Combinable,
    } as ServiceImpl;
  }
};
 
const nextStep = () => emit("next");
 
const skipSubservices = () => {
  if (durationInfo.value) durationInfo.value.focus();
};
 
/**
 * Determines whether the captcha component should be shown.
 * It checks if the currently selected service is associated with
 * at least one office that has `captchaActivatedRequired` set to true.
 */
const showCaptcha = computed(() => {
  if (!service.value || !relations.value || !offices.value) return false;
 
  const relatedOfficeIds = relations.value
    .filter((relation) => relation.serviceId == service.value?.id)
    .map((relation) => relation.officeId);
 
  return offices.value.some(
    (office) =>
      relatedOfficeIds.includes(office.id) &&
      office.scope?.captchaActivatedRequired === true
  );
});
 
onMounted(() => {
  if (service.value) {
    baseServiceId.value = service.value.parentId ?? service.value.id;
    const variantId = (service.value as any)?.variantId;
    if (typeof variantId === "number" && Number.isFinite(variantId)) {
      selectedVariant.value = String(variantId);
    }
    let slots = 0;
    countOfService.value = service.value.count
      ? service.value.count
      : countOfService.value;
    slots =
      getMinSlotOfProvider(service.value.providers || []) *
      (service.value.count || 0);
    if (service.value.subServices) {
      service.value.subServices.forEach((subservice) => {
        if (subservice.count > 0) {
          slots +=
            getMinSlotOfProvider(subservice.providers) * subservice.count;
        }
      });
    }
    currentSlots.value = slots;
    if (services.value.length === 0) {
      fetchServicesAndProviders(
        props.preselectedServiceId ?? undefined,
        props.preselectedOfficeId ?? undefined,
        props.baseUrl ?? undefined
      ).then((data) => {
        handleErrorApiResponse(
          data,
          errorStates.errorStateMap,
          currentErrorData.value
        );
        if (handleApiResponseForDownTime(data, props.baseUrl)) return;
 
        services.value = (data as any).services.map(normalizeService);
        relations.value = (data as any).relations;
        offices.value = (data as any).offices;
      });
    }
  } else {
    fetchServicesAndProviders(
      props.preselectedServiceId ?? undefined,
      props.preselectedOfficeId ?? undefined,
      props.globalState.baseUrl ?? undefined
    ).then((data) => {
      // Handle normal errors (like rate limit) first
      handleErrorApiResponse(
        data,
        errorStates.errorStateMap,
        currentErrorData.value
      );
 
      // Check if any error state should be activated (maintenance/system failure)
      if (handleApiResponseForDownTime(data, props.globalState.baseUrl)) {
        return;
      }
 
      services.value = (data as any).services.map(normalizeService);
      relations.value = (data as any).relations;
      offices.value = (data as any).offices;
 
      if (props.preselectedServiceId) {
        const foundService = services.value.find(
          (service) => service.id == props.preselectedServiceId
        );
        if (foundService) {
          service.value = {
            ...foundService,
            providers: [] as OfficeImpl[],
            count: 0,
            subServices: [],
            combinable: foundService.combinable as unknown as Combinable,
          } as ServiceImpl;
        } else {
          emit("invalidJumpinLink");
        }
      }
 
      if (props.preselectedOfficeId) {
        const foundOffice = offices.value.find(
          (office) => office.id == props.preselectedOfficeId
        );
        if (!foundOffice) {
          emit("invalidJumpinLink");
        }
      }
 
      if (props.preselectedServiceId && props.preselectedOfficeId) {
        const hasValidRelation = relations.value.some(
          (relation) =>
            relation.serviceId == props.preselectedServiceId &&
            relation.officeId == props.preselectedOfficeId
        );
        if (!hasValidRelation) {
          emit("invalidJumpinLink");
        }
      }
    });
  }
});
 
const hasNoParent = (service: Service) => service.parentId === null;
const showOnStartPage = (service: Service) => service.showOnStartPage === true;
 
const filteredServices = computed(() => {
  return services.value.filter(hasNoParent).filter(showOnStartPage);
});
 
const variantServices = computed<Service[]>(() => {
  if (!baseServiceId.value) return [];
 
  const variants = services.value
    .filter((s) => s.parentId === baseServiceId.value)
    .filter((s) => typeof s.variantId === "number");
 
  const base = services.value.find((s) => s.id === baseServiceId.value);
 
  const hasVariant1 = variants.some((v) => v.variantId === 1);
  if (base && !hasVariant1) {
    variants.unshift({
      ...base,
      parentId: null,
      variantId: 1,
    });
  }
 
  variants.sort((a, b) => a.variantId! - b.variantId!);
  return variants;
});
 
watch(selectedVariant, (variantId) => {
  if (!variantId || !baseServiceId.value) return;
 
  const selectedServiceVariant = variantServices.value.find(
    (v) => String(v.variantId) === String(variantId)
  );
 
  if (selectedServiceVariant) {
    service.value = selectedServiceVariant as ServiceImpl;
  }
});
 
const showSubservices = computed(() => {
  const value = service.value;
  if (!value) return false;
 
  const hasSub =
    Array.isArray(value.subServices) && value.subServices.length > 0;
  if (!hasSub) return false;
  if (value.parentId != null && value.variantId !== 1) return false;
  if (variantServices.value.length > 1 && selectedVariant.value !== "1")
    return false;
 
  return true;
});
 
function normalizeService(raw: any): Service {
  return {
    id: String(raw.id),
    name: raw.name,
    maxQuantity: raw.maxQuantity,
    combinable: raw.combinable,
    parentId: raw.parent_id == null ? null : String(raw.parent_id),
    variantId: raw.variant_id == null ? null : Number(raw.variant_id),
    showOnStartPage: raw.showOnStartPage,
  };
}
 
const hasVariants = computed(() => variantServices.value.length > 1);
const needsVariantSelection = computed(
  () => hasVariants.value && !selectedVariant.value
);
const isNextDisabled = computed(() => {
  const captchaBlocks = showCaptcha.value && !isCaptchaValid.value;
  return captchaBlocks || needsVariantSelection.value;
});
</script>
 
<style lang="scss" scoped>
@use "@/styles/breakpoints.scss" as *;
 
.wrapper {
  display: flex;
}
 
.wrapper > * {
  margin: 0 8px;
}
 
.m-button-group--secondary {
  margin-top: 1rem;
}
 
:deep(.counter-btn--disabled) {
  background-color: transparent !important;
  border-color: #7a8d9f !important;
  color: #7a8d9f !important;
}
 
@include xs-down {
  .container {
    padding-left: 24px;
    padding-right: 24px;
  }
}
.grid {
  grid-template-columns: minmax(150px, auto) 1fr !important;
}
 
@include sm-down {
  .grid {
    grid-template-columns: 1fr !important;
  }
}
</style>