import trimStart from 'lodash/trimStart';
import * as v from 'valibot';

import { mrfSimpleSchema, type MRFSimple } from '@/schemas/pleroma';

import type { PleromaConfig } from 'pl-api';

type Policy = Record<string, any>;
type Config = PleromaConfig['configs'][0];

const find = (configs: PleromaConfig['configs'], group: string, key: string): Config | undefined =>
  configs.find((config) => config.group === group && config.key === key);

const toSimplePolicy = (configs: PleromaConfig['configs']): Partial<MRFSimple> => {
  const config = find(configs, ':pleroma', ':mrf_simple');

  const reducer = (acc: Record<string, any>, curr: Record<string, any>) => {
    const key = curr.tuple?.[0] as string;
    const hosts = curr.tuple?.[1] as Array<string>;
    acc[trimStart(key, ':')] = hosts;
    return acc;
  };

  if (config) {
    const value = config.value ?? [];
    const result = value.reduce(reducer, {});
    return v.parse(mrfSimpleSchema, result);
  }

  return {};
};

const fromSimplePolicy = (simplePolicy: Policy) => {
  const mapper = ([key, hosts]: [key: string, hosts: Array<[string, string]>]) => ({
    tuple: [`:${key}`, hosts.map((host) => ({ tuple: host }))],
  });

  const value = Object.entries(simplePolicy).map(mapper);

  return [
    {
      group: ':pleroma',
      key: ':mrf_simple',
      value: value,
    },
  ];
};

const ConfigDB = {
  find,
  toSimplePolicy,
  fromSimplePolicy,
};

export { type Config, ConfigDB as default };
