angular.module("c8y.smartRules").directive("smartRuleParamInputEscalationSteps",function(){return e.$inject=["$scope","smartRuleTemplatesSvc","gettext","gettextCatalog"],{require:"smartRuleParamInput",scope:{rule:"=",param:"=",ruleTemplate:"="},restrict:"E",templateUrl:":::PLUGIN_PATH:::/views/paramInputEscalationSteps.html",controller:e};function e(n,e,t,i){var a,o,p,u=[{label:t("Always"),ifFailedStepRef:null}];function r(t,n){n.params=n.params||[],n.associatedRuleTemplateName&&e.getTemplateByName(n.associatedRuleTemplateName).then(function(e){n._active=e._active;e=e.paramGroups,e=[].concat(e.input.params).concat(e.output.params);_.forEach(e,function(e){_.includes(t,e.property)||n.params.push(e)})})}function c(e){return e.ifFailed||0}function f(e){return{config:e,executionCondition:{ifFailed:e.ifFailed},expanded:!1,type:_.find(o,{associatedRuleTemplateName:e.ruleTemplateName})}}function l(e){u.push({ifFailedStepRef:e})}function d(t){_.isUndefined(t.executionCondition.ifFailed)?t.executionCondition=u[0]:t.executionCondition=_.find(u,function(e){return m(e.ifFailedStepRef)===t.executionCondition.ifFailed})}function s(t){return _.find(a,function(e){return e.executionCondition&&e.executionCondition.ifFailedStepRef===t})}function m(e){return a.indexOf(e)}function x(e){var t=_.assign({},e.config);return t.ruleTemplateName=e.type.associatedRuleTemplateName,e.executionCondition&&e.executionCondition.ifFailedStepRef&&(t.ifFailed=m(e.executionCondition.ifFailedStepRef)),t}n.executionConditions=u,n.icon=function(e){return e&&e.type&&e.type.icon},n.executionConditionsFilter=function(t){return function(e){return!(e.ifFailedStepRef&&e.ifFailedStepRef.type&&!e.ifFailedStepRef.type.failable||e.ifFailedStepRef===t||e.ifFailedStepRef&&e.ifFailedStepRef.executionCondition.ifFailedStepRef===t)}},n.addStep=function(){var e={type:o[0],expanded:!0,config:{},executionCondition:u[0]};a.push(e),l(e)},n.canRemoveStep=function(e){return!s(e)},n.removeStep=function(e){var t;t=e,u=_.filter(u,function(e){return e.ifFailedStepRef!==t}),n.executionConditions=u,a=_.without(a,e),n.steps=a},n.getStepNo=m,n.toggleExpand=function(e){e.expanded=!e.expanded},n.executionConditionLabel=function(e){var t=e.label;return t=e.ifFailedStepRef?i.getString("On step {{stepNo}} failed",{stepNo:m(e.ifFailedStepRef)+1}):t},n.canChangeStepType=function(e){return!(e.type.failable&&s(e))},n.canChangeExecutionCondition=function(e){var t=_.reject(a,"executionCondition.ifFailedStepRef");return!(1===t.length&&t[0]===e)},n.getStepTooltip=function(e){return e.expanded?t("Collapse"):t("Expand")},n.$watch("steps",function(e){e=_(e).map(x).sortBy(c).value(),n.rule.config[n.param.property]=e},!0),o=_.cloneDeep(n.param.stepTypes),n.stepTypes=o,p=n.ruleTemplate.paramGroups,p=[].concat(p.input.params).concat(p.output.params),p=_.map(p,"property"),_.forEach(o,_.partial(r,p)),a=_(n.rule.config[n.param.property]||[]).sortBy(c).map(f).value(),n.steps=a,_.forEach(a,l),_.forEach(a,d)}});