import { Dictionary } from "@insite/client-framework/Common/Types";
import { get } from "@insite/shell/Services/ServiceBase";
import { SelectBrandModel } from "@insite/shell/Store/PageEditor/PageEditorState";

export interface AdminOptions {
    environment: {
        commerceVersion: string;
        projectIdentifier: string;
    };
    optiDam: {
        url: string;
        isEnabled: boolean;
        organizationId?: string;
    };
    turnstile: {
        instanceId: string;
        masterCustomerId: string;
        organizationId: string;
        organizationName: string;
        productId: string;
    };
    userflow: {
        token?: string;
        features: string[];
    };
}

export interface UserProfileViewModel {
    id: string;
    userName: string;
    roles: string[];
    email: string;
    firstName: string;
    lastName: string;
    position: string;
    company: string;
    phone: string;
    extension: string;
    fax: string;
    languageId: string;
    timeZone: string;
    passwordChangedOn: string;
    lastLoginOn: string;
    activationStatus: string;
    defaultAdminConsoleLanguageCode: string;
}

export const getAdminOptions = () => get<AdminOptions>("/admin/options");

export const getCurrentUser = () =>
    get<{
        userProfile: UserProfileViewModel;
    }>("/admin/account/getuserdata");

export const getAdminBrands = (parameter?: AdminODataApiParameter) =>
    getOData<SelectBrandModel[]>("brands", { ...parameter });

export const getAdminSystemListValues = (parameter?: AdminODataApiParameter) =>
    getOData<SystemListValue[]>("systemListValues", { ...parameter });

export interface AdminODataApiParameter {
    archiveFilter?: ArchiveFilter;
    $filter?: string;
    $orderBy?: string;
    $skip?: string;
    $top?: string;
    $select?: string;
    $count?: boolean;
}
function getOData<T>(endpoint: string, parameter?: Dictionary<any>) {
    return new Promise<T>(resolve => {
        get<AdminODataApiResult<T>>(`/api/v1/admin/${endpoint}`, parameter).then(data => {
            resolve(data.value);
        });
    });
}

interface AdminODataApiResult<T> {
    value: T;
}

export enum ArchiveFilter {
    Active = 0,
    Archived = 1,
    Both = 2,
}

export interface SystemListValue {
    name: string;
}
