"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DeviceSchema = exports.schema = void 0;
const yup_1 = require("yup");
const DeviceTypes_1 = require("../../models/device/DeviceTypes");
const NumberOptionSchema = (0, yup_1.object)().shape({
    option: (0, yup_1.number)().required().min(0),
    connectTo: (0, yup_1.string)().required(),
});
const StringOptionSchema = (0, yup_1.object)().shape({
    option: (0, yup_1.string)().required(),
    connectTo: (0, yup_1.string)().required(),
});
const EndpointSchema = {
    ringTime: (0, yup_1.number)().min(5).max(180),
    maxCallTime: (0, yup_1.number)().min(60).max(14400),
    present: (0, yup_1.string)().required(),
    record: (0, yup_1.boolean)().required(),
};
const CallHandlerSchema = {
    onComplete: (0, yup_1.string)().required(),
    onNoAnswer: (0, yup_1.string)().required(),
    onFail: (0, yup_1.string)().required(),
    onBusy: (0, yup_1.string)().required(),
};
const NumberPlanSchema = (0, yup_1.object)().shape({
    text: (0, yup_1.string)().required(),
    timeout: (0, yup_1.number)().min(2).max(60),
    maxDigits: (0, yup_1.number)().min(1).max(10),
    finishOnKey: (0, yup_1.string)().required(),
    connectTo: (0, yup_1.string)().required(),
    options: (0, yup_1.array)().of(NumberOptionSchema),
});
const ZoneRouterSchema = (0, yup_1.object)().shape({
    connectTo: (0, yup_1.string)().required(),
    options: (0, yup_1.array)().of(StringOptionSchema).min(1).required(),
});
const ANIRouterSchema = (0, yup_1.object)().shape({
    connectTo: (0, yup_1.string)().required(),
    options: (0, yup_1.array)().of(NumberOptionSchema).min(1).required(),
});
const DNISRouterSchema = (0, yup_1.object)().shape({
    connectTo: (0, yup_1.string)().required(),
    options: (0, yup_1.array)().of(NumberOptionSchema).min(1).required(),
});
const DeviceSchema = {
    name: (0, yup_1.string)()
        .min(4, 'Name must be 4-50 characters')
        .max(50, 'Name must be 4-50 characters')
        .required('A name is required'),
    extension: (0, yup_1.string)()
        .min(3, 'Extension must be 3-6 digits')
        .max(6, 'Extension must be 3-6 digits')
        .required('An extension number is required'),
    type: (0, yup_1.string)().oneOf([
        DeviceTypes_1.ANI_ROUTER,
        DeviceTypes_1.CLIENT,
        DeviceTypes_1.DATE_RANGE_ROUTER,
        DeviceTypes_1.DAY_OF_WEEK_ROUTER,
        DeviceTypes_1.DNIS_ROUTER,
        DeviceTypes_1.MATCH_START,
        DeviceTypes_1.NUMBER_PLAN,
        DeviceTypes_1.HUNT_GROUP,
        DeviceTypes_1.PLAY,
        DeviceTypes_1.PLUGIN,
        DeviceTypes_1.REMOTE,
        DeviceTypes_1.ROUTE_START,
        DeviceTypes_1.SAY,
        DeviceTypes_1.SCRIPT,
        DeviceTypes_1.SIP,
        DeviceTypes_1.SIP_GATEWAY,
        DeviceTypes_1.TEAM,
        DeviceTypes_1.TIME_RANGE_ROUTER,
        DeviceTypes_1.ZONE_ROUTER,
        DeviceTypes_1.QUEUE,
        DeviceTypes_1.VOICEMAIL,
        DeviceTypes_1.MAPPER,
    ]),
};
exports.DeviceSchema = DeviceSchema;
const SaySchema = (0, yup_1.object)().shape({
    connectTo: (0, yup_1.string)().required(),
    text: (0, yup_1.string)()
        .min(10, 'Say text is must be above 10 characters')
        .required('Say text is required')
        .test('contains-speak', 'text should not contains <speak/> tag', function (value) {
        return !value || value === '' || value.indexOf('<speak') === -1;
    }),
});
const PlaySchema = (0, yup_1.object)().shape({
    url: (0, yup_1.string)().url(),
    connectTo: (0, yup_1.string)().required(),
});
const VoicemailSchema = (0, yup_1.object)().shape({
    text: (0, yup_1.string)().min(10).required(),
    emailRecipients: (0, yup_1.array)().min(1).max(10),
    ttsLanguage: (0, yup_1.string)().required(),
    finishOnKey: (0, yup_1.string)().required(),
    maxLength: (0, yup_1.number)().min(10).max(60).required(),
    timeout: (0, yup_1.number)().min(3).max(20).required(),
    connectTo: (0, yup_1.string)().required(),
});
const ClientSchema = (0, yup_1.object)().shape(Object.assign(Object.assign({ username: (0, yup_1.string)().min(6).required('Username is required'), password: (0, yup_1.string)()
        .required('Please Enter your password')
        .matches(/^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{8,}$/, 'Must Contain 8 Characters, One Uppercase, One Lowercase, One Number and one special case Character'), 
    // user: DBReferenceSchema,
    queues: (0, yup_1.array)().required(), contacts: (0, yup_1.array)().required(), parkExtension: (0, yup_1.string)() }, EndpointSchema), CallHandlerSchema));
const QueueSchema = (0, yup_1.object)().shape({
    queueName: (0, yup_1.string)().min(4).required('Queue Name is required'),
    items: (0, yup_1.array)()
        .of((0, yup_1.object)().shape({
        type: (0, yup_1.string)().required().oneOf(['say', 'play', 'pause']),
        text: (0, yup_1.string)().required(),
    }))
        .required(),
    loop: (0, yup_1.number)().min(1).max(20).required(),
    connectTo: (0, yup_1.string)().required(),
    ttsLanguage: (0, yup_1.string)().required(),
});
const TeamsSchema = (0, yup_1.object)().shape(Object.assign(Object.assign({ to: (0, yup_1.string)()
        .required('Please Enter your teams tel:')
        .matches(/(^\+[1-9]\d{1,14}$|\[request.to\])/, 'Must be valid E164+ format') }, EndpointSchema), CallHandlerSchema));
const RemoteSchema = (0, yup_1.object)().shape(Object.assign(Object.assign({ destination: (0, yup_1.string)().required('Please Enter your remote destination') }, EndpointSchema), CallHandlerSchema));
const PluginSchema = (0, yup_1.object)().shape({
    plugin: (0, yup_1.string)().required('Plugin requires webhook id'),
    countryCode: (0, yup_1.string)().required(),
    onTransfer: (0, yup_1.string)().required(),
    onError: (0, yup_1.string)().required(),
    data: (0, yup_1.string)().required(),
});
const SipGatewaySchema = (0, yup_1.object)().shape(Object.assign(Object.assign({ trunks: (0, yup_1.array)().min(1, 'At least one trunk must be selected').required('Trunks are required') }, EndpointSchema), CallHandlerSchema));
const SipExtensionSchema = (0, yup_1.object)().shape(Object.assign(Object.assign({ gateway: (0, yup_1.string)().required('Gateway is required') }, EndpointSchema), CallHandlerSchema));
const HuntGroupSchema = (0, yup_1.object)().shape(Object.assign(Object.assign({ devices: (0, yup_1.array)().min(1, 'At least one device must be selected').required() }, EndpointSchema), CallHandlerSchema));
//todo better validation
const ScriptSchema = (0, yup_1.object)().shape({
    code: (0, yup_1.string)().required('Code is required'),
    connectTo: (0, yup_1.string)().required(),
});
const StartRouteSchema = (0, yup_1.object)().shape({
    ddis: (0, yup_1.array)().required(),
    connectTo: (0, yup_1.string)().required(),
});
const RegexRouteSchema = (0, yup_1.object)().shape({
    regExp: (0, yup_1.string)().required('Regex route selector must be present'),
    connectTo: (0, yup_1.string)().required(),
});
const DayOfWeekRouteSchema = (0, yup_1.object)().shape({
    days: (0, yup_1.array)().min(1, 'At least one day must be selected').required('days are required'),
    onMatch: (0, yup_1.string)().required(),
    connectTo: (0, yup_1.string)().required(),
});
const TimeRangeRouteSchema = (0, yup_1.object)().shape({
    start: (0, yup_1.string)().required('start time required'),
    end: (0, yup_1.string)().required('end time required'),
    onMatch: (0, yup_1.string)().required(),
    connectTo: (0, yup_1.string)().required(),
});
const DateRangeRouteSchema = (0, yup_1.object)().shape({
    start: (0, yup_1.date)().required('start date required'),
    end: (0, yup_1.date)().required('end date required'),
    onMatch: (0, yup_1.string)().required(),
    connectTo: (0, yup_1.string)().required(),
});
const schemas = {
    [DeviceTypes_1.SAY]: { field: 'say', schema: SaySchema },
    [DeviceTypes_1.PLAY]: { field: 'play', schema: PlaySchema },
    [DeviceTypes_1.TEAM]: { field: 'teams', schema: TeamsSchema },
    [DeviceTypes_1.REMOTE]: { field: 'remote', schema: RemoteSchema },
    [DeviceTypes_1.SCRIPT]: { field: 'script', schema: ScriptSchema },
    [DeviceTypes_1.CLIENT]: { field: 'client', schema: ClientSchema },
    [DeviceTypes_1.PLUGIN]: { field: 'plugin', schema: PluginSchema },
    [DeviceTypes_1.VOICEMAIL]: {
        field: 'voicemail',
        schema: VoicemailSchema,
    },
    [DeviceTypes_1.SIP]: { field: 'sipExtension', schema: SipExtensionSchema },
    [DeviceTypes_1.HUNT_GROUP]: { field: 'huntGroup', schema: HuntGroupSchema },
    [DeviceTypes_1.ROUTE_START]: { field: 'startRoute', schema: StartRouteSchema },
    [DeviceTypes_1.MATCH_START]: { field: 'regexRoute', schema: RegexRouteSchema },
    [DeviceTypes_1.SIP_GATEWAY]: { field: 'sipGateway', schema: SipGatewaySchema },
    [DeviceTypes_1.NUMBER_PLAN]: { field: 'numberPlan', schema: NumberPlanSchema },
    [DeviceTypes_1.ZONE_ROUTER]: { field: 'zoneRouter', schema: ZoneRouterSchema },
    [DeviceTypes_1.ANI_ROUTER]: { field: 'aniRouter', schema: ANIRouterSchema },
    [DeviceTypes_1.DNIS_ROUTER]: { field: 'dnisRouter', schema: DNISRouterSchema },
    [DeviceTypes_1.DAY_OF_WEEK_ROUTER]: {
        field: 'dayOfWeekRoute',
        schema: DayOfWeekRouteSchema,
    },
    [DeviceTypes_1.TIME_RANGE_ROUTER]: {
        field: 'timeRangeRoute',
        schema: TimeRangeRouteSchema,
    },
    [DeviceTypes_1.DATE_RANGE_ROUTER]: {
        field: 'dateRangeRoute',
        schema: DateRangeRouteSchema,
    },
    [DeviceTypes_1.QUEUE]: {
        field: 'queue',
        schema: QueueSchema,
    },
};
const schema = (type) => {
    const { schema, field } = schemas[type];
    return (0, yup_1.object)().shape(Object.assign(Object.assign({}, DeviceSchema), { [field]: schema }));
};
exports.schema = schema;
