import type { ShowToast } from './type-helpers/toast';
import type { OpenDialog } from './type-helpers/dialog';
import type { OpenAssetPicker } from './type-helpers/openAssetPicker';

// see https://github.com/krakenjs/zoid/blob/master/src/component/props.js#L53
export const reservedExtensionProps = [
  'timeout',
  'window',
  'close',
  'focus',
  'resize',
  'uid',
  'cspNonce',
  'getParent',
  'getParentDomain',
  'hide',
  'show',
  'export',
  'onDisplay',
  'onRendered',
  'onRender',
  'onClose',
  'onDestroy',
  'onResize',
  'onFocus',
  'onError',
  'onProps',
];

export enum ExtensionPermission {
  INPUT = 'INPUT',
  FORM = 'FORM',
  API = 'API',
}

type ConfigVariableName = string;

type ConfigVariableSettings = {
  type: 'string' | 'number' | 'boolean';
  displayName?: string;
  description?: string;
  required?: boolean;
  defaultValue?: any;
};

export type ConfigFields = Record<ConfigVariableName, ConfigVariableSettings>;

export type ConfigValue = Record<
  string,
  boolean | number | string | undefined | null
>;

export type FieldConfig = {
  tableConfig: ConfigValue;
  fieldConfig: ConfigValue;
};

export type FormSidebarConfig = { sidebarConfig: ConfigValue };

export type Project = {
  id: string;
  name: string;
  mgmtApi: string;
  mgmtToken: string;
};

export type Environment = {
  id: string;
  name: string;
  endpoint: string;
  authToken: string;
};

export type Context = {
  project: Project;
  environment: Environment;
};

export interface ExtensionPropsBase {
  context: Context;
  openDialog: OpenDialog;
  showToast: ShowToast;
  openAssetPicker: OpenAssetPicker;
  redirectParent: (location: string | Location) => Promise<void>;
  historyReplace: (url: string | HistoryCallback) => Promise<void>;
  historyPush: (url: string | HistoryCallback) => Promise<void>;
}

export interface ExtensionDialogProps extends ExtensionPropsBase {
  onCloseDialog: (value: any) => void;
}

type HistoryCallback = (href: Location['href']) => string;
