let inject = ['uiGridValidateService', 'StGridDefaults', '$timeout', '$q'];
export class StGridSetValidators {
    static get $inject(){
        return inject
    }
    constructor(...injectables){
        injectNg(injectables, inject, this);
    }
    setCustomValidateServices(){
        this.uiGridValidateService.runValidatorsCustom = (rowEntity, colDef, oldValue, grid) => {
            if (!colDef.name) throw new Error('colDef.name is required to perform validation');
            this.uiGridValidateService.setValid(rowEntity, colDef);
            let validateClosureFactory = (rowEntity, colDef, validatorName) => {
                return value => {
                    if (!value) {
                        this.uiGridValidateService.setInvalid(rowEntity, colDef);
                        this.uiGridValidateService.setError(rowEntity, colDef, validatorName);
                        if (grid) grid.api.validate.raise.validationFailed(rowEntity, colDef, oldValue, oldValue);
                    }
                };
            };
            let promises = [];
            for (let validatorName in colDef.validators) {
                this.uiGridValidateService.clearError(rowEntity, colDef, validatorName);
                let validatorFunction = this.uiGridValidateService.getValidator(validatorName, colDef.validators[validatorName]),
                promise = this.$q.when(validatorFunction(oldValue, oldValue, rowEntity, colDef)).then(validateClosureFactory(rowEntity, colDef, validatorName));
                promises.push(promise);
            }
            return this.$q.all(promises);
        }
        this.uiGridValidateService.runValidatorsSpecific = (rowEntity, colDef, oldValue, grid, validators) => {
            if (isNU0(validators)) validators = Object.keys(colDef.validators);
            if (!colDef.name) throw new Error('colDef.name is required to perform validation');
            this.uiGridValidateService.setValid(rowEntity, colDef);
            let validateClosureFactory = (rowEntity, colDef, validatorName) => {
                return value => {
                    if (!value) {
                        this.uiGridValidateService.setInvalid(rowEntity, colDef);
                        this.uiGridValidateService.setError(rowEntity, colDef, validatorName);
                        if (grid) grid.api.validate.raise.validationFailed(rowEntity, colDef, oldValue, oldValue);
                    }
                };
            };
            let promises = [];
            validators.forEach(validator => {
                this.uiGridValidateService.clearError(rowEntity, colDef, validator);
                let validatorFunction = this.uiGridValidateService.getValidator(validator, colDef.validators[validator]),
                promise = this.$q.when(validatorFunction(oldValue, oldValue, rowEntity, colDef)).then(validateClosureFactory(rowEntity, colDef, validator));
                promises.push(promise);
            })
            return this.$q.all(promises);
        }
        this.uiGridValidateService.validateColDef = (grid, colDef, validators) => {
            if (isNU0(colDef)) resolve();
            let promises = [];
            grid._options.data.forEach(rowEntity => {
                let value = getSafe(rowEntity, colDef.name);
                if (value) {
                    let validation = this.uiGridValidateService.runValidatorsSpecific(rowEntity, colDef, value, grid, validators);
                    promises.push(validation);
                }
            });
            return this.$q.all(promises).then(() => {
                return grid.hasErrors;
            });
        }
        this.uiGridValidateService.validateUpdated = (grid, validators) => {
            let promises = [];
            grid._columns.filter(o => o.validators).forEach(colDef => {
                grid.getUpdated.forEach(rowEntity => {
                    let value = getSafe(rowEntity, colDef.name),
                    validation = this.uiGridValidateService.runValidatorsSpecific(rowEntity, colDef, value, grid, validators);
                    promises.push(validation);
                });
            });
            return this.$q.all(promises).then(() => {
                return grid.hasErrors;
            });
        }
        this.uiGridValidateService.validateAll = (grid) => {
            let promises = [];
            grid._columns.filter(o => o.validators).forEach(colDef => {
                grid._options.data.forEach(rowEntity => {
                    let value = getSafe(rowEntity, colDef.name),
                    validation = this.uiGridValidateService.runValidatorsCustom(rowEntity, colDef, value, grid);
                    promises.push(validation);
                });
            });
            return this.$q.all(promises).then(() => {
                return grid.hasErrors;
            });
        }
    }
    setDefaultsValidators(){
        if (isNU0(this.StGridDefaults.validators)) return;
        angular.forEach(this.StGridDefaults.validators, (value, key) => {
            if (!value.perGrid) {
                this.uiGridValidateService.setValidator(key, argument => {
                    return (oldValue, newValue, rowEntity, colDef) => {
                        return value.func(argument, oldValue, newValue, rowEntity, colDef);
                    }
                }, args => value.msg(args));
            }
        })
    }
}