import * as React from "react";
import { createLogger } from "../logger";
import { createConfig } from "../manifestUtils/createConfig";
import { getAllFields, getConfigurationDiff } from "./_internals";

export const logger = createLogger({
  category: "useValidatePlayerConfig",
  subsystem: "useValidatePlayerConfig",
});

/** Default Player Configuration */
const {
  styles,
  general,
  localizations,
  custom_configuration_fields,
}: DefaultConfiguration = createConfig(
  () => {
    return {};
  },
  { extend: "player" }
) as any;

const QBPlayerConfigFields = getAllFields(
  styles,
  general,
  localizations,
  custom_configuration_fields
);

export const useValidatePlayerConfig = (config) => {
  React.useEffect(() => {
    try {
      const diff = getConfigurationDiff(QBPlayerConfigFields, config);

      logger.log_info(
        "Missing following configuration properties. Some elements of the player may not work correctly. Check QuickBrickPlayerPlugin for the configuration reference https://github.com/applicaster/QuickBrick/tree/main/plugins/zapp-react-native-default-player/manifests",
        diff
      );
    } catch (err) {
      logger.log_error(
        `Failed to validate player configuration: ${err.message}`,
        { err }
      );
    }
  }, []);
};
