export function transformFlatObjectToNested(model, separator = '_', defaultValue = null) {
  return Object.entries(model).reduce((model, [key, value]) => {
    const keys = key.split(separator).reverse();
    let current = model;
    while (keys.length) {
      const k = keys.pop();
      current = current[k] = keys.length ? (current[k] || {}) : (value || defaultValue);
    }
    return model;
  }, {})
}

export function transformNestedObjectToFlat(model, separator = '_') {
  const result = {};
  const stack = [{obj: model, parentKey: ''}];

  while (stack.length > 0) {
    const {obj, parentKey} = stack.pop();
    for (const [key, value] of Object.entries(obj)) {
      const k = parentKey ? `${parentKey}${separator}${key}` : key;
      if (value && typeof value === 'object' && !Array.isArray(value)) {
        stack.push({obj: value, parentKey: k});
      } else {
        result[k] = value;
      }
    }
  }

  return result;
}

export function withResolvers() {
  let resolve, reject;
  const promise = new Promise((res, rej) => {
    resolve = res;
    reject = rej;
  });
  return {promise, resolve, reject};
}
