<commit_before>/**
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

import {events, BuildingBlock, BuildingMaterial, buildUIMode} from 'camelot-unchained';

import {selectFromMaterial, selectToMaterial, setBlockMode} from './materials-replace';
import requester from '../../../../../../services/session/requester';

const assign = require('object-assign');

const SELECT_MATERIAL = 'buildpanel/panes/SELECT_MATERIAL';
const SELECT_BLOCK = 'buildpanel/panes/SELECT_BLOCK';

const SET_MATERIALS = 'buildpanel/panes/SET_MATERIALS';

const DEFAULT_MATERIAL: BuildingMaterial = new BuildingMaterial({
  id: -1,
  icon: '',
  tags: ['default'],
  blocks: []
} as BuildingMaterial);

export function initialize(dispatch: (action: any) => void) {

  events.addListener(events.buildingEventTopics.handlesBlocks, (info: { materials: BuildingMaterial[] }) => {
    const mats: BuildingMaterial[] = info.materials;
    dispatch(setMaterials(mats))
    dispatch(selectFromMaterial(mats[0]))
    dispatch(selectToMaterial(mats[0]))
  });

  events.addListener(events.buildingEventTopics.handlesBlockSelect, (info: { material: BuildingMaterial, block: BuildingBlock }) => {
    dispatch(selectBlock(info.block));
    dispatch(selectFromMaterial(info.material));
  });

  events.addListener(events.buildingEventTopics.handlesBuildingMode, (info: { mode: buildUIMode }) => {
    dispatch(setBlockMode(info.mode==buildUIMode.BLOCKSELECTED));
  });

}

function selectBlock(block: BuildingBlock) {
  return {
    type: SELECT_BLOCK,
    selectedBlock: block
  }
}

function selectMaterial(material: BuildingMaterial) {
  return {
    type: SELECT_MATERIAL,
    selectedMaterial: material
  }
}

function setMaterials(materials: BuildingMaterial[]) {
  return {
    type: SET_MATERIALS,
    materials: materials
  }
}

function getMaterialById(matId: number, materials: BuildingMaterial[]) {
  for (let m in materials) {
    if (materials[m].id === matId) {
      return materials[m];
    }
  }
  return materials[0] || DEFAULT_MATERIAL;
}

export interface MaterialsState {
  materials?: BuildingMaterial[];
  selectedMaterial?: BuildingMaterial;
  selectedBlock: BuildingBlock;
}

const initialState: MaterialsState = {
  materials: [],
  selectedMaterial: DEFAULT_MATERIAL,
  selectedBlock: {} as BuildingBlock,
}

export default function reducer(state: MaterialsState = initialState, action: any = {}) {
  switch (action.type) {
    case SET_MATERIALS:
      return assign({}, state, {
        materials: action.materials,
        selectedMaterial: action.materials[0],
        selectedBlock: action.materials[0].blocks[0]
      });
    case SELECT_MATERIAL:
      return assign({}, state, {
        selectedMaterial: action.selectedMaterial,
      });
    case SELECT_BLOCK:
      const block: BuildingBlock = action.selectedBlock;
      const newState: MaterialsState = { selectedBlock: block } as MaterialsState
      if (state.selectedMaterial.id != block.materialId)
        newState.selectedMaterial = getMaterialById(block.materialId, state.materials);
      return assign({}, state, newState);
    default: return state;
  }<commit_msg>set default block when starting up<commit_after>/**
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

import {events, BuildingBlock, BuildingMaterial, buildUIMode} from 'camelot-unchained';

import {selectFromMaterial, selectToMaterial, setBlockMode} from './materials-replace';
import requester from '../../../../../../services/session/requester';

const assign = require('object-assign');

const SELECT_MATERIAL = 'buildpanel/panes/SELECT_MATERIAL';
const SELECT_BLOCK = 'buildpanel/panes/SELECT_BLOCK';

const SET_MATERIALS = 'buildpanel/panes/SET_MATERIALS';

export const DEFAULT_MATERIAL: BuildingMaterial = new BuildingMaterial({
  id: -1,
  icon: '',
  tags: ['default'],
  blocks: []
} as BuildingMaterial);

export function initialize(dispatch: (action: any) => void) {

  events.addListener(events.buildingEventTopics.handlesBlocks, (info: { materials: BuildingMaterial[] }) => {
    const mats: BuildingMaterial[] = info.materials;
    dispatch(setMaterials(mats))
    dispatch(selectFromMaterial(mats[0]))
    dispatch(selectToMaterial(mats[0]))
  });

  events.addListener(events.buildingEventTopics.handlesBlockSelect, (info: { material: BuildingMaterial, block: BuildingBlock }) => {
    dispatch(selectBlock(info.block));
    dispatch(selectFromMaterial(info.material));
  });

  events.addListener(events.buildingEventTopics.handlesBuildingMode, (info: { mode: buildUIMode }) => {
    dispatch(setBlockMode(info.mode==buildUIMode.BLOCKSELECTED));
  });

}

function selectBlock(block: BuildingBlock) {
  return {
    type: SELECT_BLOCK,
    selectedBlock: block
  }
}

function selectMaterial(material: BuildingMaterial) {
  return {
    type: SELECT_MATERIAL,
    selectedMaterial: material
  }
}

function setMaterials(materials: BuildingMaterial[]) {
  return {
    type: SET_MATERIALS,
    materials: materials
  }
}

function getMaterialById(matId: number, materials: BuildingMaterial[]) {
  for (let m in materials) {
    if (materials[m].id === matId) {
      return materials[m];
    }
  }
  return materials[0] || DEFAULT_MATERIAL;
}

export interface MaterialsState {
  materials?: BuildingMaterial[];
  selectedMaterial?: BuildingMaterial;
  selectedBlock: BuildingBlock;
}

const initialState: MaterialsState = {
  materials: [],
  selectedMaterial: DEFAULT_MATERIAL,
  selectedBlock: {} as BuildingBlock,
}

export default function reducer(state: MaterialsState = initialState, action: any = {}) {
  switch (action.type) {
    case SET_MATERIALS:
      requester.changeBlockSelection(action.materials[0].blocks[0]);
      return assign({}, state, {
        materials: action.materials,
        selectedMaterial: action.materials[0],
        selectedBlock: action.materials[0].blocks[0]
      });
    case SELECT_MATERIAL:
      return assign({}, state, {
        selectedMaterial: action.selectedMaterial,
      });
    case SELECT_BLOCK:
      const block: BuildingBlock = action.selectedBlock;
      const newState: MaterialsState = { selectedBlock: block } as MaterialsState
      if (state.selectedMaterial.id != block.materialId)
        newState.selectedMaterial = getMaterialById(block.materialId, state.materials);
      return assign({}, state, newState);
    default: return state;
  }
{'fraction_non_alphanumeric': 0.12115759484180537, 'fraction_numerical': 0.003598740440845704, 'mean_word_length': 4.3660498793242155, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2896420', 'n_tokens_mistral': 1957, 'n_tokens_neox': 1880, 'n_words': 449}