import { z } from 'zod';
import { globalOptionsZod } from '../../../../Command.js';
import request from '../../../../request.js';
import GraphCommand from '../../../base/GraphCommand.js';
import commands from '../../commands.js';
const policyEndPoints = {
    activitybasedtimeout: "activityBasedTimeoutPolicies",
    adminconsentrequest: "adminConsentRequestPolicy",
    appmanagement: "appManagementPolicies",
    authenticationflows: "authenticationFlowsPolicy",
    authenticationmethods: "authenticationMethodsPolicy",
    authenticationstrength: "authenticationStrengthPolicies",
    authorization: "authorizationPolicy",
    claimsmapping: "claimsMappingPolicies",
    conditionalaccess: "conditionalAccessPolicies",
    crosstenantaccess: "crossTenantAccessPolicy",
    defaultappmanagement: "defaultAppManagementPolicy",
    deviceregistration: "deviceRegistrationPolicy",
    featurerolloutpolicy: "featureRolloutPolicies",
    homerealmdiscovery: "homeRealmDiscoveryPolicies",
    identitysecuritydefaultsenforcement: "identitySecurityDefaultsEnforcementPolicy",
    permissiongrant: "permissionGrantPolicies",
    rolemanagement: "roleManagementPolicies",
    tokenissuance: "tokenIssuancePolicies",
    tokenlifetime: "tokenLifetimePolicies"
};
const supportedPolicyTypes = ['activityBasedTimeout', 'adminConsentRequest', 'appManagement', 'authenticationFlows', 'authenticationMethods', 'authenticationStrength', 'authorization', 'claimsMapping', 'conditionalAccess', 'crossTenantAccess', 'defaultAppManagement', 'deviceRegistration', 'featureRolloutPolicy', 'homeRealmDiscovery', 'identitySecurityDefaultsEnforcement', 'permissionGrant', 'roleManagement', 'tokenIssuance', 'tokenLifetime'];
export const options = z.strictObject({
    ...globalOptionsZod.shape,
    type: z.preprocess(val => {
        const target = String(val).toLowerCase();
        return supportedPolicyTypes.find(t => t.toLowerCase() === target) ?? val;
    }, z.enum(supportedPolicyTypes)).optional().alias('t')
});
class EntraPolicyListCommand extends GraphCommand {
    get name() {
        return commands.POLICY_LIST;
    }
    get description() {
        return 'Returns policies from Entra ID';
    }
    get schema() {
        return options;
    }
    defaultProperties() {
        return ['id', 'displayName', 'isOrganizationDefault'];
    }
    async commandAction(logger, args) {
        const policyType = args.options.type ? args.options.type.toLowerCase() : 'all';
        try {
            if (policyType && policyType !== "all") {
                const policies = await this.getPolicies(policyType);
                await logger.log(policies);
            }
            else {
                const policyTypes = Object.keys(policyEndPoints);
                const results = await Promise.all(policyTypes.map(policyType => this.getPolicies(policyType)));
                let allPolicies = [];
                results.forEach((policies) => {
                    allPolicies = allPolicies.concat(policies);
                });
                await logger.log(allPolicies);
            }
        }
        catch (err) {
            this.handleRejectedODataJsonPromise(err);
        }
    }
    async getPolicies(policyType) {
        const endpoint = policyEndPoints[policyType];
        let requestUrl = `${this.resource}/v1.0/policies/${endpoint}`;
        if (endpoint === policyEndPoints.rolemanagement) {
            // roleManagementPolicies endpoint requires $filter query parameter
            requestUrl += `?$filter=scopeId eq '/' and scopeType eq 'DirectoryRole'`;
        }
        const requestOptions = {
            url: requestUrl,
            headers: {
                accept: 'application/json;odata.metadata=none'
            },
            responseType: 'json'
        };
        const response = await request.get(requestOptions);
        if (endpoint === policyEndPoints.adminconsentrequest ||
            endpoint === policyEndPoints.authenticationflows ||
            endpoint === policyEndPoints.authenticationmethods ||
            endpoint === policyEndPoints.authorization ||
            endpoint === policyEndPoints.crosstenantaccess ||
            endpoint === policyEndPoints.defaultappmanagement ||
            endpoint === policyEndPoints.deviceregistration ||
            endpoint === policyEndPoints.identitysecuritydefaultsenforcement) {
            return response;
        }
        else {
            return response.value;
        }
    }
}
export default new EntraPolicyListCommand();
//# sourceMappingURL=policy-list.js.map