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ServicesPicker.mjs","sourcesContent":["import { useMemo, type ReactNode } from 'react';\nimport type { UseSchedulingAdminResult } from '../../../hooks/useSchedulingAdmin';\nimport type {\n  SchedulingResourceServiceLink,\n  SchedulingService,\n} from '../../../types/scheduling';\n\nexport interface SchedulingServicesPickerProps {\n  admin: UseSchedulingAdminResult;\n  /**\n   * Resource whose service-coverage we're editing. Required — site-wide\n   * resource_service rows aren't a thing (services either belong to a stylist\n   * or they don't).\n   */\n  resourceId: string;\n  className?: string;\n  chipClassName?: string;\n  /**\n   * Optional override for active-state styling. When omitted the same\n   * `chipClassName` is used for both states — consumers typically swap out\n   * the className via render-prop instead.\n   */\n  activeChipClassName?: string;\n  emptyState?: ReactNode;\n  errorClassName?: string;\n  /**\n   * Optional renderer for each chip. Receives the service, whether the\n   * resource currently performs it, and a `toggle` handler that handles the\n   * create-or-delete on the link table.\n   */\n  renderChip?: (\n    service: SchedulingService,\n    state: { active: boolean; toggle: () => void; pending: boolean },\n  ) => ReactNode;\n}\n\nexport function SchedulingServicesPicker({\n  admin,\n  resourceId,\n  className,\n  chipClassName,\n  activeChipClassName,\n  emptyState,\n  errorClassName,\n  renderChip,\n}: SchedulingServicesPickerProps) {\n  // Build a map: serviceId -> link row id (or undefined if not linked).\n  // Reused for the toggle's create/delete decision and for the per-chip\n  // active-state styling.\n  const linkByServiceId = useMemo(() => {\n    const map = new Map<string, string>();\n    for (const link of admin.resourceServices.list as Array<\n      SchedulingResourceServiceLink & { id: string }\n    >) {\n      if (link.resource_id === resourceId) {\n        map.set(link.service_id, link.id);\n      }\n    }\n    return map;\n  }, [admin.resourceServices.list, resourceId]);\n\n  const toggle = (serviceId: string) => {\n    const linkId = linkByServiceId.get(serviceId);\n    if (linkId) {\n      void admin.resourceServices.remove(linkId);\n    } else {\n      void admin.resourceServices.create({\n        resource_id: resourceId,\n        service_id: serviceId,\n      });\n    }\n  };\n\n  const activeServices = admin.services.list.filter(\n    (s) => s.active === undefined || s.active === true,\n  );\n\n  if (admin.services.list.length === 0) {\n    return (\n      <div className={className}>\n        {emptyState ?? <p>Add a service first — there's nothing to pick from.</p>}\n      </div>\n    );\n  }\n\n  // We treat the picker as pending while EITHER catalog is in flight; toggling\n  // before both lists resolve risks creating a row against a stale id pair.\n  const pending =\n    admin.resourceServices.loading || admin.services.loading;\n  const error = admin.resourceServices.error;\n\n  return (\n    <div className={className}>\n      {error ? (\n        <div className={errorClassName} role=\"alert\">\n          {error}\n        </div>\n      ) : null}\n      {activeServices.map((service) => {\n        const active = linkByServiceId.has(service.id);\n        const handle = () => toggle(service.id);\n        if (renderChip) return renderChip(service, { active, toggle: handle, pending });\n        return (\n          <button\n            key={service.id}\n            type=\"button\"\n            aria-pressed={active}\n            disabled={pending}\n            onClick={handle}\n            className={active ? activeChipClassName ?? chipClassName : chipClassName}\n          >\n            {service.name}\n          </button>\n        );\n      })}\n    </div>\n  );\n}\n"]}