import ApiRequestService from "~/services/data/apiRequestService";
import {useAp2iRequestService} from "~/composables/data/useAp2iRequestService";


export default class SubdomainValidation {
    private apiRequestService: ApiRequestService

    public constructor(apiRequestService: ApiRequestService) {
        this.apiRequestService = apiRequestService
    }

    /**
     * Le sous-domaine est valide s'il contient entre 2 et 60 caractères, et pas de caractères spéciaux
     * @param subdomain
     */
    public static isValid(subdomain: string | null): boolean {
        return subdomain !== null && subdomain.match(/^[a-z0-9][a-z0-9\-]{0,61}[a-z0-9]$/) !== null
    }

    /**
     * Returns true if the given subdomain has not been registered yet or is not reserved
     * @param subdomain
     */
    public async isAvailable(subdomain: string): Promise<boolean> {
        const subdomainAvailability: any = await this.apiRequestService.get('/api/subdomains/is_available', {'subdomain': subdomain})
        return subdomainAvailability && subdomainAvailability['available'] === true
    }
}
