export function convertLoadBalancerToFormValues(item) {
  if (!item) {
    return {};
  }
  const listeners = [...item.listeners];
  const lastListener = listeners.pop();
  const formListeners = listeners.map(listener => {
    const rules = listener.rules.map(rule => {
      const denormalisedRule = {
        type: rule.type,
      };
      switch (rule.type) {
        case 'cookie':
          denormalisedRule.cookieValue = [];
          Object.keys(rule.cookieValue).forEach(k => {
            denormalisedRule.cookieValue.push({
              name: k,
              values: rule.cookieValue[k],
            });
          });
          break;
        case 'header':
          denormalisedRule.headerValue = [];
          Object.keys(rule.headerValue).forEach(k => {
            denormalisedRule.headerValue.push({
              name: k,
              values: rule.headerValue[k],
            });
          });
          break;
        case 'host':
          denormalisedRule.hostValue = rule.hostValue.map(v => ({ value: v }));
          break;
        case 'path':
          denormalisedRule.pathValue = rule.pathValue.map(v => ({ value: v }));
          break;
      }
      return denormalisedRule;
    });
    return {
      rules,
      targetGroup: listener.forwardAction.targets[0].targetGroup._id,
    };
  });
  return {
    listeners: formListeners,
    name: item.name,
    defaultTargetGroup: lastListener.forwardAction.targets[0].targetGroup._id,
  };
}
export function convertFormValuesToLoadBalancer(values) {
  var _a;
  const listeners = ((_a = values.listeners) === null || _a === void 0 ? void 0 : _a.map(listener => ({
    port: 443,
    forwardAction: {
      targets: [
        {
          weight: 1,
          targetGroup: {
            _id: listener.targetGroup,
          }
        }
      ],
    },
    rules: listener.rules.map(rule => {
      var _a;
      const normalisedRule = {
        type: rule.type,
      };
      switch (rule.type) {
        case 'cookie':
          normalisedRule.cookieValue = {};
          rule.cookieValue.forEach(v => {
            normalisedRule.cookieValue[v.name] = v.values || [''];
          });
          break;
        case 'header':
          normalisedRule.headerValue = {};
          rule.headerValue.forEach(v => {
            normalisedRule.headerValue[v.name] = v.values || [''];
          });
          break;
        case 'host':
          normalisedRule.hostValue = (_a = rule.hostValue) === null || _a === void 0 ? void 0 : _a.map(v => (v === null || v === void 0 ? void 0 : v.value) || '');
          break;
        case 'path':
          normalisedRule.pathValue = rule.pathValue.map(v => (v === null || v === void 0 ? void 0 : v.value) || '');
          break;
      }
      return normalisedRule;
    })
  }))) || [];
  listeners.push({
    port: 443,
    forwardAction: { targets: [{ weight: 1, targetGroup: { _id: values.defaultTargetGroup } }] }
  });
  return {
    name: values.name,
    listeners,
  };
}
