import { makeAutoObservable } from 'mobx';
import type { AdminServiceCategory } from '../../api/index.ts';

export class ServiceCategoryModel {
  readonly id: string;
  name = '';
  slug = '';

  constructor(dto: AdminServiceCategory) {
    this.id = String(dto.id);
    this.hydrate(dto);

    makeAutoObservable(this, { id: false }, { autoBind: true });
  }

  update(dto: Partial<AdminServiceCategory>) {
    this.hydrate(dto);
  }

  toJSON(): AdminServiceCategory {
    return {
      id: this.id,
      name: this.name,
      slug: this.slug,
    };
  }

  private hydrate(dto: Partial<AdminServiceCategory>) {
    if (dto.name !== undefined) {
      this.name = dto.name;
    }

    if (dto.slug !== undefined) {
      this.slug = dto.slug;
    }
  }
}
