import { z } from 'zod';
import { globalOptionsZod } from '../../../../Command.js';
import { formatting } from '../../../../utils/formatting.js';
import { odata } from '../../../../utils/odata.js';
import GraphCommand from '../../../base/GraphCommand.js';
import commands from '../../commands.js';
import { optionsUtils } from '../../../../utils/optionsUtils.js';
import { zod } from '../../../../utils/zod.js';
const allowedTypes = { Member: 'Member', Guest: 'Guest' };
export const options = z.looseObject({
    ...globalOptionsZod.shape,
    type: zod.coercedEnum(allowedTypes).optional(),
    properties: z.string().optional().alias('p')
});
class EntraUserListCommand extends GraphCommand {
    get name() {
        return commands.USER_LIST;
    }
    get description() {
        return 'Lists users matching specified criteria';
    }
    allowUnknownOptions() {
        return true;
    }
    defaultProperties() {
        return ['id', 'displayName', 'mail', 'userPrincipalName'];
    }
    get schema() {
        return options;
    }
    async commandAction(logger, args) {
        try {
            let url = `${this.resource}/v1.0/users`;
            if (args.options.properties) {
                const selectProperties = args.options.properties;
                const allSelectProperties = selectProperties.split(',');
                const propertiesWithSlash = allSelectProperties.filter(item => item.includes('/'));
                const fieldExpand = propertiesWithSlash
                    .map(p => `${p.split('/')[0]}($select=${p.split('/')[1]})`)
                    .join(',');
                const expandParam = fieldExpand.length > 0 ? `&$expand=${fieldExpand}` : '';
                const selectParam = allSelectProperties.filter(item => !item.includes('/'));
                url += `?$select=${selectParam}${expandParam}`;
            }
            const filter = this.getFilter(args.options);
            if (filter) {
                url += `${args.options.properties ? '&' : '?'}${filter}`;
            }
            const users = await odata.getAllItems(url);
            await logger.log(users);
        }
        catch (err) {
            this.handleRejectedODataJsonPromise(err);
        }
    }
    getFilter(options) {
        const filters = [];
        const unknownOptions = optionsUtils.getUnknownOptions(options, zod.schemaToOptions(this.schema));
        Object.keys(unknownOptions).forEach(key => {
            if (typeof options[key] === 'boolean') {
                throw `Specify value for the ${key} property`;
            }
            filters.push(`startsWith(${key}, '${formatting.encodeQueryParameter(options[key].toString())}')`);
        });
        if (options.type) {
            filters.push(`userType eq '${options.type}'`);
        }
        if (filters.length > 0) {
            return `$filter=${filters.join(' and ')}`;
        }
        return null;
    }
}
export default new EntraUserListCommand();
//# sourceMappingURL=user-list.js.map